mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-11 19:27:35 -06:00
Merge pull request #2339 from Microsoft/exportDefaultType
Support an optional type annotation on export default statement
This commit is contained in:
commit
2240fbf390
@ -505,7 +505,7 @@ module ts {
|
||||
bindChildren(node, 0, /*isBlockScopeContainer*/ false);
|
||||
break;
|
||||
case SyntaxKind.ExportAssignment:
|
||||
if ((<ExportAssignment>node).expression.kind === SyntaxKind.Identifier) {
|
||||
if ((<ExportAssignment>node).expression && (<ExportAssignment>node).expression.kind === SyntaxKind.Identifier) {
|
||||
// An export default clause with an identifier exports all meanings of that identifier
|
||||
declareSymbol(container.symbol.exports, container.symbol, <Declaration>node, SymbolFlags.Alias, SymbolFlags.AliasExcludes);
|
||||
}
|
||||
|
||||
@ -567,7 +567,7 @@ module ts {
|
||||
}
|
||||
|
||||
function getTargetOfExportAssignment(node: ExportAssignment): Symbol {
|
||||
return resolveEntityName(<Identifier>node.expression, SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace);
|
||||
return node.expression && resolveEntityName(<Identifier>node.expression, SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace);
|
||||
}
|
||||
|
||||
function getTargetOfImportDeclaration(node: Declaration): Symbol {
|
||||
@ -623,7 +623,7 @@ module ts {
|
||||
if (!links.referenced) {
|
||||
links.referenced = true;
|
||||
let node = getDeclarationOfAliasSymbol(symbol);
|
||||
if (node.kind === SyntaxKind.ExportAssignment) {
|
||||
if (node.kind === SyntaxKind.ExportAssignment && (<ExportAssignment>node).expression) {
|
||||
// export default <symbol>
|
||||
checkExpressionCached((<ExportAssignment>node).expression);
|
||||
}
|
||||
@ -2073,7 +2073,16 @@ module ts {
|
||||
}
|
||||
// Handle export default expressions
|
||||
if (declaration.kind === SyntaxKind.ExportAssignment) {
|
||||
return links.type = checkExpression((<ExportAssignment>declaration).expression);
|
||||
var exportAssignment = <ExportAssignment>declaration;
|
||||
if (exportAssignment.expression) {
|
||||
return links.type = checkExpression(exportAssignment.expression);
|
||||
}
|
||||
else if (exportAssignment.type) {
|
||||
return links.type = getTypeFromTypeNode(exportAssignment.type);
|
||||
}
|
||||
else {
|
||||
return links.type = anyType;
|
||||
}
|
||||
}
|
||||
// Handle variable, parameter or property
|
||||
links.type = resolvingType;
|
||||
@ -10075,12 +10084,21 @@ module ts {
|
||||
if (!checkGrammarModifiers(node) && (node.flags & NodeFlags.Modifier)) {
|
||||
grammarErrorOnFirstToken(node, Diagnostics.An_export_assignment_cannot_have_modifiers);
|
||||
}
|
||||
if (node.expression.kind === SyntaxKind.Identifier) {
|
||||
markExportAsReferenced(node);
|
||||
if (node.expression) {
|
||||
if (node.expression.kind === SyntaxKind.Identifier) {
|
||||
markExportAsReferenced(node);
|
||||
}
|
||||
else {
|
||||
checkExpressionCached(node.expression);
|
||||
}
|
||||
}
|
||||
else {
|
||||
checkExpressionCached(node.expression);
|
||||
if (node.type) {
|
||||
checkSourceElement(node.type);
|
||||
if (!isInAmbientContext(node)) {
|
||||
grammarErrorOnFirstToken(node.type, Diagnostics.A_type_annotation_on_an_export_statement_is_only_allowed_in_an_ambient_external_module_declaration);
|
||||
}
|
||||
}
|
||||
|
||||
checkExternalModuleExports(container);
|
||||
}
|
||||
|
||||
@ -10914,7 +10932,7 @@ module ts {
|
||||
}
|
||||
|
||||
function generateNameForExportAssignment(node: ExportAssignment) {
|
||||
if (node.expression.kind !== SyntaxKind.Identifier) {
|
||||
if (node.expression && node.expression.kind !== SyntaxKind.Identifier) {
|
||||
assignGeneratedName(node, makeUniqueName("default"));
|
||||
}
|
||||
}
|
||||
|
||||
@ -158,6 +158,7 @@ module ts {
|
||||
An_extended_Unicode_escape_value_must_be_between_0x0_and_0x10FFFF_inclusive: { code: 1198, category: DiagnosticCategory.Error, key: "An extended Unicode escape value must be between 0x0 and 0x10FFFF inclusive." },
|
||||
Unterminated_Unicode_escape_sequence: { code: 1199, category: DiagnosticCategory.Error, key: "Unterminated Unicode escape sequence." },
|
||||
Line_terminator_not_permitted_before_arrow: { code: 1200, category: DiagnosticCategory.Error, key: "Line terminator not permitted before arrow." },
|
||||
A_type_annotation_on_an_export_statement_is_only_allowed_in_an_ambient_external_module_declaration: { code: 1201, category: DiagnosticCategory.Error, key: "A type annotation on an export statement is only allowed in an ambient external module declaration." },
|
||||
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
|
||||
Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." },
|
||||
Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." },
|
||||
|
||||
@ -617,12 +617,17 @@
|
||||
},
|
||||
"Unterminated Unicode escape sequence.": {
|
||||
"category": "Error",
|
||||
"code": 1199
|
||||
"code": 1199
|
||||
},
|
||||
"Line terminator not permitted before arrow.": {
|
||||
"category": "Error",
|
||||
"code": 1200
|
||||
},
|
||||
"A type annotation on an export statement is only allowed in an ambient external module declaration.": {
|
||||
"category": "Error",
|
||||
"code": 1201
|
||||
},
|
||||
|
||||
"Duplicate identifier '{0}'.": {
|
||||
"category": "Error",
|
||||
"code": 2300
|
||||
|
||||
@ -283,7 +283,8 @@ module ts {
|
||||
visitNode(cbNode, (<ImportOrExportSpecifier>node).name);
|
||||
case SyntaxKind.ExportAssignment:
|
||||
return visitNodes(cbNodes, node.modifiers) ||
|
||||
visitNode(cbNode, (<ExportAssignment>node).expression);
|
||||
visitNode(cbNode, (<ExportAssignment>node).expression) ||
|
||||
visitNode(cbNode, (<ExportAssignment>node).type);
|
||||
case SyntaxKind.TemplateExpression:
|
||||
return visitNode(cbNode, (<TemplateExpression>node).head) || visitNodes(cbNodes, (<TemplateExpression>node).templateSpans);
|
||||
case SyntaxKind.TemplateSpan:
|
||||
@ -4867,12 +4868,17 @@ module ts {
|
||||
setModifiers(node, modifiers);
|
||||
if (parseOptional(SyntaxKind.EqualsToken)) {
|
||||
node.isExportEquals = true;
|
||||
node.expression = parseAssignmentExpressionOrHigher();
|
||||
}
|
||||
else {
|
||||
parseExpected(SyntaxKind.DefaultKeyword);
|
||||
if (parseOptional(SyntaxKind.ColonToken)) {
|
||||
node.type = parseType();
|
||||
}
|
||||
else {
|
||||
node.expression = parseAssignmentExpressionOrHigher();
|
||||
}
|
||||
}
|
||||
//node.exportName = parseIdentifier();
|
||||
node.expression = parseAssignmentExpressionOrHigher();
|
||||
parseSemicolon();
|
||||
return finishNode(node);
|
||||
}
|
||||
|
||||
@ -944,7 +944,8 @@ module ts {
|
||||
|
||||
export interface ExportAssignment extends Declaration, ModuleElement {
|
||||
isExportEquals?: boolean;
|
||||
expression: Expression;
|
||||
expression?: Expression;
|
||||
type?: TypeNode;
|
||||
}
|
||||
|
||||
export interface FileReference extends TextRange {
|
||||
|
||||
@ -173,6 +173,10 @@ module ts.BreakpointResolver {
|
||||
return textSpan(node, (<ThrowStatement>node).expression);
|
||||
|
||||
case SyntaxKind.ExportAssignment:
|
||||
if (!(<ExportAssignment>node).expression) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// span on export = id
|
||||
return textSpan(node, (<ExportAssignment>node).expression);
|
||||
|
||||
|
||||
@ -765,7 +765,8 @@ declare module "typescript" {
|
||||
type ExportSpecifier = ImportOrExportSpecifier;
|
||||
interface ExportAssignment extends Declaration, ModuleElement {
|
||||
isExportEquals?: boolean;
|
||||
expression: Expression;
|
||||
expression?: Expression;
|
||||
type?: TypeNode;
|
||||
}
|
||||
interface FileReference extends TextRange {
|
||||
fileName: string;
|
||||
|
||||
@ -2333,9 +2333,13 @@ declare module "typescript" {
|
||||
isExportEquals?: boolean;
|
||||
>isExportEquals : boolean
|
||||
|
||||
expression: Expression;
|
||||
expression?: Expression;
|
||||
>expression : Expression
|
||||
>Expression : Expression
|
||||
|
||||
type?: TypeNode;
|
||||
>type : TypeNode
|
||||
>TypeNode : TypeNode
|
||||
}
|
||||
interface FileReference extends TextRange {
|
||||
>FileReference : FileReference
|
||||
|
||||
@ -796,7 +796,8 @@ declare module "typescript" {
|
||||
type ExportSpecifier = ImportOrExportSpecifier;
|
||||
interface ExportAssignment extends Declaration, ModuleElement {
|
||||
isExportEquals?: boolean;
|
||||
expression: Expression;
|
||||
expression?: Expression;
|
||||
type?: TypeNode;
|
||||
}
|
||||
interface FileReference extends TextRange {
|
||||
fileName: string;
|
||||
|
||||
@ -2479,9 +2479,13 @@ declare module "typescript" {
|
||||
isExportEquals?: boolean;
|
||||
>isExportEquals : boolean
|
||||
|
||||
expression: Expression;
|
||||
expression?: Expression;
|
||||
>expression : Expression
|
||||
>Expression : Expression
|
||||
|
||||
type?: TypeNode;
|
||||
>type : TypeNode
|
||||
>TypeNode : TypeNode
|
||||
}
|
||||
interface FileReference extends TextRange {
|
||||
>FileReference : FileReference
|
||||
|
||||
@ -797,7 +797,8 @@ declare module "typescript" {
|
||||
type ExportSpecifier = ImportOrExportSpecifier;
|
||||
interface ExportAssignment extends Declaration, ModuleElement {
|
||||
isExportEquals?: boolean;
|
||||
expression: Expression;
|
||||
expression?: Expression;
|
||||
type?: TypeNode;
|
||||
}
|
||||
interface FileReference extends TextRange {
|
||||
fileName: string;
|
||||
|
||||
@ -2429,9 +2429,13 @@ declare module "typescript" {
|
||||
isExportEquals?: boolean;
|
||||
>isExportEquals : boolean
|
||||
|
||||
expression: Expression;
|
||||
expression?: Expression;
|
||||
>expression : Expression
|
||||
>Expression : Expression
|
||||
|
||||
type?: TypeNode;
|
||||
>type : TypeNode
|
||||
>TypeNode : TypeNode
|
||||
}
|
||||
interface FileReference extends TextRange {
|
||||
>FileReference : FileReference
|
||||
|
||||
@ -834,7 +834,8 @@ declare module "typescript" {
|
||||
type ExportSpecifier = ImportOrExportSpecifier;
|
||||
interface ExportAssignment extends Declaration, ModuleElement {
|
||||
isExportEquals?: boolean;
|
||||
expression: Expression;
|
||||
expression?: Expression;
|
||||
type?: TypeNode;
|
||||
}
|
||||
interface FileReference extends TextRange {
|
||||
fileName: string;
|
||||
|
||||
@ -2602,9 +2602,13 @@ declare module "typescript" {
|
||||
isExportEquals?: boolean;
|
||||
>isExportEquals : boolean
|
||||
|
||||
expression: Expression;
|
||||
expression?: Expression;
|
||||
>expression : Expression
|
||||
>Expression : Expression
|
||||
|
||||
type?: TypeNode;
|
||||
>type : TypeNode
|
||||
>TypeNode : TypeNode
|
||||
}
|
||||
interface FileReference extends TextRange {
|
||||
>FileReference : FileReference
|
||||
|
||||
@ -0,0 +1,8 @@
|
||||
tests/cases/compiler/exportDefaultTypeAnnoation.ts(2,18): error TS1201: A type annotation on an export statement is only allowed in an ambient external module declaration.
|
||||
|
||||
|
||||
==== tests/cases/compiler/exportDefaultTypeAnnoation.ts (1 errors) ====
|
||||
|
||||
export default : number;
|
||||
~~~~~~
|
||||
!!! error TS1201: A type annotation on an export statement is only allowed in an ambient external module declaration.
|
||||
6
tests/baselines/reference/exportDefaultTypeAnnoation.js
Normal file
6
tests/baselines/reference/exportDefaultTypeAnnoation.js
Normal file
@ -0,0 +1,6 @@
|
||||
//// [exportDefaultTypeAnnoation.ts]
|
||||
|
||||
export default : number;
|
||||
|
||||
//// [exportDefaultTypeAnnoation.js]
|
||||
module.exports = ;
|
||||
7
tests/baselines/reference/exportDefaultTypeAnnoation2.js
Normal file
7
tests/baselines/reference/exportDefaultTypeAnnoation2.js
Normal file
@ -0,0 +1,7 @@
|
||||
//// [exportDefaultTypeAnnoation2.ts]
|
||||
|
||||
declare module "mod" {
|
||||
export default : number;
|
||||
}
|
||||
|
||||
//// [exportDefaultTypeAnnoation2.js]
|
||||
@ -0,0 +1,6 @@
|
||||
=== tests/cases/compiler/exportDefaultTypeAnnoation2.ts ===
|
||||
|
||||
No type information for this code.declare module "mod" {
|
||||
No type information for this code. export default : number;
|
||||
No type information for this code.}
|
||||
No type information for this code.
|
||||
@ -0,0 +1,21 @@
|
||||
tests/cases/compiler/reference1.ts(2,5): error TS2322: Type 'number' is not assignable to type 'string'.
|
||||
tests/cases/compiler/reference2.ts(2,5): error TS2322: Type 'number' is not assignable to type 'string'.
|
||||
|
||||
|
||||
==== tests/cases/compiler/mod.d.ts (0 errors) ====
|
||||
|
||||
declare module "mod" {
|
||||
export default : number;
|
||||
}
|
||||
|
||||
==== tests/cases/compiler/reference1.ts (1 errors) ====
|
||||
import d from "mod";
|
||||
var s: string = d; // Error
|
||||
~
|
||||
!!! error TS2322: Type 'number' is not assignable to type 'string'.
|
||||
|
||||
==== tests/cases/compiler/reference2.ts (1 errors) ====
|
||||
import { default as d } from "mod";
|
||||
var s: string = d; // Error
|
||||
~
|
||||
!!! error TS2322: Type 'number' is not assignable to type 'string'.
|
||||
22
tests/baselines/reference/exportDefaultTypeAnnoation3.js
Normal file
22
tests/baselines/reference/exportDefaultTypeAnnoation3.js
Normal file
@ -0,0 +1,22 @@
|
||||
//// [tests/cases/compiler/exportDefaultTypeAnnoation3.ts] ////
|
||||
|
||||
//// [mod.d.ts]
|
||||
|
||||
declare module "mod" {
|
||||
export default : number;
|
||||
}
|
||||
|
||||
//// [reference1.ts]
|
||||
import d from "mod";
|
||||
var s: string = d; // Error
|
||||
|
||||
//// [reference2.ts]
|
||||
import { default as d } from "mod";
|
||||
var s: string = d; // Error
|
||||
|
||||
//// [reference1.js]
|
||||
var d = require("mod");
|
||||
var s = d; // Error
|
||||
//// [reference2.js]
|
||||
var _mod = require("mod");
|
||||
var s = _mod.default; // Error
|
||||
4
tests/cases/compiler/exportDefaultTypeAnnoation.ts
Normal file
4
tests/cases/compiler/exportDefaultTypeAnnoation.ts
Normal file
@ -0,0 +1,4 @@
|
||||
// @target: es5
|
||||
// @module: commonjs
|
||||
|
||||
export default : number;
|
||||
6
tests/cases/compiler/exportDefaultTypeAnnoation2.ts
Normal file
6
tests/cases/compiler/exportDefaultTypeAnnoation2.ts
Normal file
@ -0,0 +1,6 @@
|
||||
// @target: es5
|
||||
// @module: commonjs
|
||||
|
||||
declare module "mod" {
|
||||
export default : number;
|
||||
}
|
||||
15
tests/cases/compiler/exportDefaultTypeAnnoation3.ts
Normal file
15
tests/cases/compiler/exportDefaultTypeAnnoation3.ts
Normal file
@ -0,0 +1,15 @@
|
||||
// @target: es5
|
||||
// @module: commonjs
|
||||
|
||||
// @fileName: mod.d.ts
|
||||
declare module "mod" {
|
||||
export default : number;
|
||||
}
|
||||
|
||||
// @fileName: reference1.ts
|
||||
import d from "mod";
|
||||
var s: string = d; // Error
|
||||
|
||||
// @fileName: reference2.ts
|
||||
import { default as d } from "mod";
|
||||
var s: string = d; // Error
|
||||
Loading…
x
Reference in New Issue
Block a user