Skip to content

Commit 69fef6e

Browse files
committed
Parsing for default binding import syntax
1 parent 5eb0094 commit 69fef6e

20 files changed

+252
-10
lines changed

src/compiler/parser.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4523,11 +4523,18 @@ module ts {
45234523
return nextToken() === SyntaxKind.OpenParenToken;
45244524
}
45254525

4526+
function nextTokenIsCommaOrFromKeyword() {
4527+
nextToken();
4528+
return token === SyntaxKind.CommaToken ||
4529+
token === SyntaxKind.FromKeyword;
4530+
}
4531+
45264532
function parseImportDeclarationOrStatement(fullStart: number, modifiers: ModifiersArray): ImportEqualsDeclaration | ImportStatement {
45274533
parseExpected(SyntaxKind.ImportKeyword);
45284534
if (token === SyntaxKind.StringLiteral ||
45294535
token === SyntaxKind.AsteriskToken ||
4530-
token === SyntaxKind.OpenBraceToken) {
4536+
token === SyntaxKind.OpenBraceToken ||
4537+
(isIdentifier() && lookAhead(nextTokenIsCommaOrFromKeyword))) {
45314538
return parseImportStatement(fullStart, modifiers);
45324539
}
45334540

@@ -4604,7 +4611,19 @@ module ts {
46044611
// ImportedDefaultBinding, NamedImports from
46054612

46064613
var importClause = <ImportClause>createNode(SyntaxKind.ImportClause);
4607-
importClause.bindings = token === SyntaxKind.AsteriskToken ? parseNamespaceImport() : parseNamedImports();
4614+
if (isIdentifier()) {
4615+
// ImportedDefaultBinding:
4616+
// ImportedBinding
4617+
importClause.defaultBinding = parseIdentifier();
4618+
}
4619+
4620+
// If there was no default import or if there is comma token after default import
4621+
// parse namespace or named imports
4622+
if (!importClause.defaultBinding ||
4623+
parseOptional(SyntaxKind.CommaToken)) {
4624+
importClause.bindings = token === SyntaxKind.AsteriskToken ? parseNamespaceImport() : parseNamedImports();
4625+
}
4626+
46084627
parseExpected(SyntaxKind.FromKeyword);
46094628
return finishNode(importClause);
46104629
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [tests/cases/compiler/es6ImportDefaultBinding.ts] ////
2+
3+
//// [es6ImportDefaultBinding_0.ts]
4+
5+
export var a = 10;
6+
7+
//// [es6ImportDefaultBinding_1.ts]
8+
import defaultBinding from "es6ImportDefaultBinding_0";
9+
10+
//// [es6ImportDefaultBinding_0.js]
11+
exports.a = 10;
12+
//// [es6ImportDefaultBinding_1.js]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/es6ImportDefaultBinding_0.ts ===
2+
3+
export var a = 10;
4+
>a : number
5+
6+
=== tests/cases/compiler/es6ImportDefaultBinding_1.ts ===
7+
import defaultBinding from "es6ImportDefaultBinding_0";
8+
No type information for this code.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamedImport.ts] ////
2+
3+
//// [es6ImportDefaultBindingFollowedWithNamedImport_0.ts]
4+
5+
export var a = 10;
6+
export var x = a;
7+
export var m = a;
8+
9+
//// [es6ImportDefaultBindingFollowedWithNamedImport_1.ts]
10+
import defaultBinding, { } from "es6ImportDefaultBindingFollowedWithNamedImport_0";
11+
import defaultBinding, { a } from "es6ImportDefaultBindingFollowedWithNamedImport_0";
12+
import defaultBinding, { a as b } from "es6ImportDefaultBindingFollowedWithNamedImport_0";
13+
import defaultBinding, { x, a as y } from "es6ImportDefaultBindingFollowedWithNamedImport_0";
14+
import defaultBinding, { x as z, } from "es6ImportDefaultBindingFollowedWithNamedImport_0";
15+
import defaultBinding, { m, } from "es6ImportDefaultBindingFollowedWithNamedImport_0";
16+
17+
//// [es6ImportDefaultBindingFollowedWithNamedImport_0.js]
18+
exports.a = 10;
19+
exports.x = exports.a;
20+
exports.m = exports.a;
21+
//// [es6ImportDefaultBindingFollowedWithNamedImport_1.js]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamedImport_0.ts ===
2+
3+
export var a = 10;
4+
>a : number
5+
6+
export var x = a;
7+
>x : number
8+
>a : number
9+
10+
export var m = a;
11+
>m : number
12+
>a : number
13+
14+
=== tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamedImport_1.ts ===
15+
import defaultBinding, { } from "es6ImportDefaultBindingFollowedWithNamedImport_0";
16+
No type information for this code.import defaultBinding, { a } from "es6ImportDefaultBindingFollowedWithNamedImport_0";
17+
No type information for this code.import defaultBinding, { a as b } from "es6ImportDefaultBindingFollowedWithNamedImport_0";
18+
No type information for this code.import defaultBinding, { x, a as y } from "es6ImportDefaultBindingFollowedWithNamedImport_0";
19+
No type information for this code.import defaultBinding, { x as z, } from "es6ImportDefaultBindingFollowedWithNamedImport_0";
20+
No type information for this code.import defaultBinding, { m, } from "es6ImportDefaultBindingFollowedWithNamedImport_0";
21+
No type information for this code.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamedImportInEs5.ts] ////
2+
3+
//// [es6ImportDefaultBindingFollowedWithNamedImportInEs5_0.ts]
4+
5+
export var a = 10;
6+
export var x = a;
7+
export var m = a;
8+
9+
//// [es6ImportDefaultBindingFollowedWithNamedImportInEs5_1.ts]
10+
import defaultBinding, { } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
11+
import defaultBinding, { a } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
12+
import defaultBinding, { a as b } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
13+
import defaultBinding, { x, a as y } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
14+
import defaultBinding, { x as z, } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
15+
import defaultBinding, { m, } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
16+
17+
//// [es6ImportDefaultBindingFollowedWithNamedImportInEs5_0.js]
18+
exports.a = 10;
19+
exports.x = exports.a;
20+
exports.m = exports.a;
21+
//// [es6ImportDefaultBindingFollowedWithNamedImportInEs5_1.js]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamedImportInEs5_0.ts ===
2+
3+
export var a = 10;
4+
>a : number
5+
6+
export var x = a;
7+
>x : number
8+
>a : number
9+
10+
export var m = a;
11+
>m : number
12+
>a : number
13+
14+
=== tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamedImportInEs5_1.ts ===
15+
import defaultBinding, { } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
16+
No type information for this code.import defaultBinding, { a } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
17+
No type information for this code.import defaultBinding, { a as b } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
18+
No type information for this code.import defaultBinding, { x, a as y } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
19+
No type information for this code.import defaultBinding, { x as z, } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
20+
No type information for this code.import defaultBinding, { m, } from "es6ImportDefaultBindingFollowedWithNamedImportInEs5_0";
21+
No type information for this code.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamespaceBinding.ts] ////
2+
3+
//// [es6ImportDefaultBindingFollowedWithNamespaceBinding_0.ts]
4+
5+
export var a = 10;
6+
7+
//// [es6ImportDefaultBindingFollowedWithNamespaceBinding_1.ts]
8+
import defaultBinding, * as nameSpaceBinding from "es6ImportDefaultBindingFollowedWithNamespaceBinding_0";
9+
10+
//// [es6ImportDefaultBindingFollowedWithNamespaceBinding_0.js]
11+
exports.a = 10;
12+
//// [es6ImportDefaultBindingFollowedWithNamespaceBinding_1.js]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamespaceBinding_0.ts ===
2+
3+
export var a = 10;
4+
>a : number
5+
6+
=== tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamespaceBinding_1.ts ===
7+
import defaultBinding, * as nameSpaceBinding from "es6ImportDefaultBindingFollowedWithNamespaceBinding_0";
8+
No type information for this code.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5.ts] ////
2+
3+
//// [es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_0.ts]
4+
5+
export var a = 10;
6+
7+
//// [es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_1.ts]
8+
import defaultBinding, * as nameSpaceBinding from "es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_0";
9+
10+
//// [es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_0.js]
11+
exports.a = 10;
12+
//// [es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_1.js]

0 commit comments

Comments
 (0)