diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index c890ba86ee0..57b53d81a0e 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -4523,11 +4523,18 @@ module ts { return nextToken() === SyntaxKind.OpenParenToken; } + function nextTokenIsCommaOrFromKeyword() { + nextToken(); + return token === SyntaxKind.CommaToken || + token === SyntaxKind.FromKeyword; + } + function parseImportDeclarationOrStatement(fullStart: number, modifiers: ModifiersArray): ImportEqualsDeclaration | ImportStatement { parseExpected(SyntaxKind.ImportKeyword); if (token === SyntaxKind.StringLiteral || token === SyntaxKind.AsteriskToken || - token === SyntaxKind.OpenBraceToken) { + token === SyntaxKind.OpenBraceToken || + (isIdentifier() && lookAhead(nextTokenIsCommaOrFromKeyword))) { return parseImportStatement(fullStart, modifiers); } @@ -4604,7 +4611,19 @@ module ts { // ImportedDefaultBinding, NamedImports from var importClause = createNode(SyntaxKind.ImportClause); - importClause.bindings = token === SyntaxKind.AsteriskToken ? parseNamespaceImport() : parseNamedImports(); + if (isIdentifier()) { + // ImportedDefaultBinding: + // ImportedBinding + importClause.defaultBinding = parseIdentifier(); + } + + // If there was no default import or if there is comma token after default import + // parse namespace or named imports + if (!importClause.defaultBinding || + parseOptional(SyntaxKind.CommaToken)) { + importClause.bindings = token === SyntaxKind.AsteriskToken ? parseNamespaceImport() : parseNamedImports(); + } + parseExpected(SyntaxKind.FromKeyword); return finishNode(importClause); } diff --git a/tests/baselines/reference/es6ImportDefaultBinding.js b/tests/baselines/reference/es6ImportDefaultBinding.js new file mode 100644 index 00000000000..d8d1fa8111d --- /dev/null +++ b/tests/baselines/reference/es6ImportDefaultBinding.js @@ -0,0 +1,12 @@ +//// [tests/cases/compiler/es6ImportDefaultBinding.ts] //// + +//// [es6ImportDefaultBinding_0.ts] + +export var a = 10; + +//// [es6ImportDefaultBinding_1.ts] +import defaultBinding from "es6ImportDefaultBinding_0"; + +//// [es6ImportDefaultBinding_0.js] +exports.a = 10; +//// [es6ImportDefaultBinding_1.js] diff --git a/tests/baselines/reference/es6ImportDefaultBinding.types b/tests/baselines/reference/es6ImportDefaultBinding.types new file mode 100644 index 00000000000..262ce5966b3 --- /dev/null +++ b/tests/baselines/reference/es6ImportDefaultBinding.types @@ -0,0 +1,8 @@ +=== tests/cases/compiler/es6ImportDefaultBinding_0.ts === + +export var a = 10; +>a : number + +=== tests/cases/compiler/es6ImportDefaultBinding_1.ts === +import defaultBinding from "es6ImportDefaultBinding_0"; +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamedImport.js b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamedImport.js new file mode 100644 index 00000000000..a7402bfeb6a --- /dev/null +++ b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamedImport.js @@ -0,0 +1,21 @@ +//// [tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamedImport.ts] //// + +//// [es6ImportDefaultBindingFollowedWithNamedImport_0.ts] + +export var a = 10; +export var x = a; +export var m = a; + +//// [es6ImportDefaultBindingFollowedWithNamedImport_1.ts] +import defaultBinding, { } from "es6ImportDefaultBindingFollowedWithNamedImport_0"; +import defaultBinding, { a } from "es6ImportDefaultBindingFollowedWithNamedImport_0"; +import defaultBinding, { a as b } from "es6ImportDefaultBindingFollowedWithNamedImport_0"; +import defaultBinding, { x, a as y } from "es6ImportDefaultBindingFollowedWithNamedImport_0"; +import defaultBinding, { x as z, } from "es6ImportDefaultBindingFollowedWithNamedImport_0"; +import defaultBinding, { m, } from "es6ImportDefaultBindingFollowedWithNamedImport_0"; + +//// [es6ImportDefaultBindingFollowedWithNamedImport_0.js] +exports.a = 10; +exports.x = exports.a; +exports.m = exports.a; +//// [es6ImportDefaultBindingFollowedWithNamedImport_1.js] diff --git a/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamedImport.types b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamedImport.types new file mode 100644 index 00000000000..35dacc7d93a --- /dev/null +++ b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamedImport.types @@ -0,0 +1,21 @@ +=== tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamedImport_0.ts === + +export var a = 10; +>a : number + +export var x = a; +>x : number +>a : number + +export var m = a; +>m : number +>a : number + +=== tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamedImport_1.ts === +import defaultBinding, { } from "es6ImportDefaultBindingFollowedWithNamedImport_0"; +No type information for this code.import defaultBinding, { a } from "es6ImportDefaultBindingFollowedWithNamedImport_0"; +No type information for this code.import defaultBinding, { a as b } from "es6ImportDefaultBindingFollowedWithNamedImport_0"; +No type information for this code.import defaultBinding, { x, a as y } from "es6ImportDefaultBindingFollowedWithNamedImport_0"; +No type information for this code.import defaultBinding, { x as z, } from "es6ImportDefaultBindingFollowedWithNamedImport_0"; +No type information for this code.import defaultBinding, { m, } from "es6ImportDefaultBindingFollowedWithNamedImport_0"; +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamedImportInEs5.js b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamedImportInEs5.js new file mode 100644 index 00000000000..4cf3046532a --- /dev/null +++ b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamedImportInEs5.js @@ -0,0 +1,21 @@ +//// [tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamedImportInEs5.ts] //// + +//// [es6ImportDefaultBindingFollowedWithNamedImportInEs5_0.ts] + +export var a = 10; +export var x = a; +export var m = a; + +//// [es6ImportDefaultBindingFollowedWithNamedImportInEs5_1.ts] +import defaultBinding, { } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"; +import defaultBinding, { a } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"; +import defaultBinding, { a as b } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"; +import defaultBinding, { x, a as y } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"; +import defaultBinding, { x as z, } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"; +import defaultBinding, { m, } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"; + +//// [es6ImportDefaultBindingFollowedWithNamedImportInEs5_0.js] +exports.a = 10; +exports.x = exports.a; +exports.m = exports.a; +//// [es6ImportDefaultBindingFollowedWithNamedImportInEs5_1.js] diff --git a/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamedImportInEs5.types b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamedImportInEs5.types new file mode 100644 index 00000000000..a98cca1edcf --- /dev/null +++ b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamedImportInEs5.types @@ -0,0 +1,21 @@ +=== tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamedImportInEs5_0.ts === + +export var a = 10; +>a : number + +export var x = a; +>x : number +>a : number + +export var m = a; +>m : number +>a : number + +=== tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamedImportInEs5_1.ts === +import defaultBinding, { } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"; +No type information for this code.import defaultBinding, { a } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"; +No type information for this code.import defaultBinding, { a as b } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"; +No type information for this code.import defaultBinding, { x, a as y } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"; +No type information for this code.import defaultBinding, { x as z, } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"; +No type information for this code.import defaultBinding, { m, } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"; +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBinding.js b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBinding.js new file mode 100644 index 00000000000..5ec91279306 --- /dev/null +++ b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBinding.js @@ -0,0 +1,12 @@ +//// [tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamespaceBinding.ts] //// + +//// [es6ImportDefaultBindingFollowedWithNamespaceBinding_0.ts] + +export var a = 10; + +//// [es6ImportDefaultBindingFollowedWithNamespaceBinding_1.ts] +import defaultBinding, * as nameSpaceBinding from "es6ImportDefaultBindingFollowedWithNamespaceBinding_0"; + +//// [es6ImportDefaultBindingFollowedWithNamespaceBinding_0.js] +exports.a = 10; +//// [es6ImportDefaultBindingFollowedWithNamespaceBinding_1.js] diff --git a/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBinding.types b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBinding.types new file mode 100644 index 00000000000..9f9db16a6f4 --- /dev/null +++ b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBinding.types @@ -0,0 +1,8 @@ +=== tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamespaceBinding_0.ts === + +export var a = 10; +>a : number + +=== tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamespaceBinding_1.ts === +import defaultBinding, * as nameSpaceBinding from "es6ImportDefaultBindingFollowedWithNamespaceBinding_0"; +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5.js b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5.js new file mode 100644 index 00000000000..abd4736fca7 --- /dev/null +++ b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5.js @@ -0,0 +1,12 @@ +//// [tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5.ts] //// + +//// [es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_0.ts] + +export var a = 10; + +//// [es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_1.ts] +import defaultBinding, * as nameSpaceBinding from "es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_0"; + +//// [es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_0.js] +exports.a = 10; +//// [es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_1.js] diff --git a/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5.types b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5.types new file mode 100644 index 00000000000..841bb9d68b5 --- /dev/null +++ b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5.types @@ -0,0 +1,8 @@ +=== tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_0.ts === + +export var a = 10; +>a : number + +=== tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_1.ts === +import defaultBinding, * as nameSpaceBinding from "es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_0"; +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/es6ImportDefaultBindingInEs5.js b/tests/baselines/reference/es6ImportDefaultBindingInEs5.js new file mode 100644 index 00000000000..b6d293225fd --- /dev/null +++ b/tests/baselines/reference/es6ImportDefaultBindingInEs5.js @@ -0,0 +1,12 @@ +//// [tests/cases/compiler/es6ImportDefaultBindingInEs5.ts] //// + +//// [es6ImportDefaultBindingInEs5_0.ts] + +export var a = 10; + +//// [es6ImportDefaultBindingInEs5_1.ts] +import defaultBinding from "es6ImportDefaultBindingInEs5_0"; + +//// [es6ImportDefaultBindingInEs5_0.js] +exports.a = 10; +//// [es6ImportDefaultBindingInEs5_1.js] diff --git a/tests/baselines/reference/es6ImportDefaultBindingInEs5.types b/tests/baselines/reference/es6ImportDefaultBindingInEs5.types new file mode 100644 index 00000000000..1f3c4141f5b --- /dev/null +++ b/tests/baselines/reference/es6ImportDefaultBindingInEs5.types @@ -0,0 +1,8 @@ +=== tests/cases/compiler/es6ImportDefaultBindingInEs5_0.ts === + +export var a = 10; +>a : number + +=== tests/cases/compiler/es6ImportDefaultBindingInEs5_1.ts === +import defaultBinding from "es6ImportDefaultBindingInEs5_0"; +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/es6ImportNamedImportParsingError.errors.txt b/tests/baselines/reference/es6ImportNamedImportParsingError.errors.txt index ae1fba6b038..f33c218afcd 100644 --- a/tests/baselines/reference/es6ImportNamedImportParsingError.errors.txt +++ b/tests/baselines/reference/es6ImportNamedImportParsingError.errors.txt @@ -2,9 +2,8 @@ tests/cases/compiler/es6ImportNamedImportParsingError_1.ts(1,10): error TS1003: tests/cases/compiler/es6ImportNamedImportParsingError_1.ts(1,12): error TS1109: Expression expected. tests/cases/compiler/es6ImportNamedImportParsingError_1.ts(1,14): error TS2304: Cannot find name 'from'. tests/cases/compiler/es6ImportNamedImportParsingError_1.ts(1,19): error TS1005: ';' expected. -tests/cases/compiler/es6ImportNamedImportParsingError_1.ts(2,22): error TS1005: '=' expected. -tests/cases/compiler/es6ImportNamedImportParsingError_1.ts(2,24): error TS2304: Cannot find name 'from'. -tests/cases/compiler/es6ImportNamedImportParsingError_1.ts(2,29): error TS1005: ';' expected. +tests/cases/compiler/es6ImportNamedImportParsingError_1.ts(2,24): error TS1005: '{' expected. +tests/cases/compiler/es6ImportNamedImportParsingError_1.ts(2,29): error TS1005: ',' expected. ==== tests/cases/compiler/es6ImportNamedImportParsingError_0.ts (0 errors) ==== @@ -13,7 +12,7 @@ tests/cases/compiler/es6ImportNamedImportParsingError_1.ts(2,29): error TS1005: export var x = a; export var m = a; -==== tests/cases/compiler/es6ImportNamedImportParsingError_1.ts (7 errors) ==== +==== tests/cases/compiler/es6ImportNamedImportParsingError_1.ts (6 errors) ==== import { * } from "es6ImportNamedImportParsingError_0"; ~ !!! error TS1003: Identifier expected. @@ -24,9 +23,7 @@ tests/cases/compiler/es6ImportNamedImportParsingError_1.ts(2,29): error TS1005: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS1005: ';' expected. import defaultBinding, from "es6ImportNamedImportParsingError_0"; - ~ -!!! error TS1005: '=' expected. ~~~~ -!!! error TS2304: Cannot find name 'from'. +!!! error TS1005: '{' expected. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS1005: ';' expected. \ No newline at end of file +!!! error TS1005: ',' expected. \ No newline at end of file diff --git a/tests/cases/compiler/es6ImportDefaultBinding.ts b/tests/cases/compiler/es6ImportDefaultBinding.ts new file mode 100644 index 00000000000..dc5b4ab98d6 --- /dev/null +++ b/tests/cases/compiler/es6ImportDefaultBinding.ts @@ -0,0 +1,8 @@ +// @target: es6 +// @module: commonjs + +// @filename: es6ImportDefaultBinding_0.ts +export var a = 10; + +// @filename: es6ImportDefaultBinding_1.ts +import defaultBinding from "es6ImportDefaultBinding_0"; \ No newline at end of file diff --git a/tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamedImport.ts b/tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamedImport.ts new file mode 100644 index 00000000000..96b277d6640 --- /dev/null +++ b/tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamedImport.ts @@ -0,0 +1,15 @@ +// @target: es6 +// @module: commonjs + +// @filename: es6ImportDefaultBindingFollowedWithNamedImport_0.ts +export var a = 10; +export var x = a; +export var m = a; + +// @filename: es6ImportDefaultBindingFollowedWithNamedImport_1.ts +import defaultBinding, { } from "es6ImportDefaultBindingFollowedWithNamedImport_0"; +import defaultBinding, { a } from "es6ImportDefaultBindingFollowedWithNamedImport_0"; +import defaultBinding, { a as b } from "es6ImportDefaultBindingFollowedWithNamedImport_0"; +import defaultBinding, { x, a as y } from "es6ImportDefaultBindingFollowedWithNamedImport_0"; +import defaultBinding, { x as z, } from "es6ImportDefaultBindingFollowedWithNamedImport_0"; +import defaultBinding, { m, } from "es6ImportDefaultBindingFollowedWithNamedImport_0"; \ No newline at end of file diff --git a/tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamedImportInEs5.ts b/tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamedImportInEs5.ts new file mode 100644 index 00000000000..bf34687f791 --- /dev/null +++ b/tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamedImportInEs5.ts @@ -0,0 +1,15 @@ +// @target: es5 +// @module: commonjs + +// @filename: es6ImportDefaultBindingFollowedWithNamedImportInEs5_0.ts +export var a = 10; +export var x = a; +export var m = a; + +// @filename: es6ImportDefaultBindingFollowedWithNamedImportInEs5_1.ts +import defaultBinding, { } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"; +import defaultBinding, { a } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"; +import defaultBinding, { a as b } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"; +import defaultBinding, { x, a as y } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"; +import defaultBinding, { x as z, } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"; +import defaultBinding, { m, } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0"; \ No newline at end of file diff --git a/tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamespaceBinding.ts b/tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamespaceBinding.ts new file mode 100644 index 00000000000..3d029e28738 --- /dev/null +++ b/tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamespaceBinding.ts @@ -0,0 +1,8 @@ +// @target: es6 +// @module: commonjs + +// @filename: es6ImportDefaultBindingFollowedWithNamespaceBinding_0.ts +export var a = 10; + +// @filename: es6ImportDefaultBindingFollowedWithNamespaceBinding_1.ts +import defaultBinding, * as nameSpaceBinding from "es6ImportDefaultBindingFollowedWithNamespaceBinding_0"; \ No newline at end of file diff --git a/tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5.ts b/tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5.ts new file mode 100644 index 00000000000..5943fa8bfc4 --- /dev/null +++ b/tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5.ts @@ -0,0 +1,8 @@ +// @target: es5 +// @module: commonjs + +// @filename: es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_0.ts +export var a = 10; + +// @filename: es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_1.ts +import defaultBinding, * as nameSpaceBinding from "es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_0"; \ No newline at end of file diff --git a/tests/cases/compiler/es6ImportDefaultBindingInEs5.ts b/tests/cases/compiler/es6ImportDefaultBindingInEs5.ts new file mode 100644 index 00000000000..c037d283dfa --- /dev/null +++ b/tests/cases/compiler/es6ImportDefaultBindingInEs5.ts @@ -0,0 +1,8 @@ +// @target: es5 +// @module: commonjs + +// @filename: es6ImportDefaultBindingInEs5_0.ts +export var a = 10; + +// @filename: es6ImportDefaultBindingInEs5_1.ts +import defaultBinding from "es6ImportDefaultBindingInEs5_0"; \ No newline at end of file