Skip to content

Commit b0f2265

Browse files
committed
Code review feedback
1 parent d296a10 commit b0f2265

File tree

3 files changed

+68
-65
lines changed

3 files changed

+68
-65
lines changed

src/compiler/parser.ts

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4531,20 +4531,61 @@ module ts {
45314531

45324532
function parseImportDeclarationOrStatement(fullStart: number, modifiers: ModifiersArray): ImportEqualsDeclaration | ImportStatement {
45334533
parseExpected(SyntaxKind.ImportKeyword);
4534-
if (token === SyntaxKind.StringLiteral ||
4535-
token === SyntaxKind.AsteriskToken ||
4536-
token === SyntaxKind.OpenBraceToken ||
4537-
(isIdentifier() && lookAhead(nextTokenIsCommaOrFromKeyword))) {
4538-
return parseImportStatement(fullStart, modifiers);
4534+
var identifier: Identifier;
4535+
if (isIdentifier()) {
4536+
identifier = parseIdentifier();
4537+
if (token !== SyntaxKind.CommaToken && token !== SyntaxKind.FromKeyword) {
4538+
// ImportEquals declaration of type:
4539+
// import x = require("mod"); or
4540+
// import x = M.x;
4541+
var importEqualsDeclaration = <ImportEqualsDeclaration>createNode(SyntaxKind.ImportEqualsDeclaration, fullStart);
4542+
setModifiers(importEqualsDeclaration, modifiers);
4543+
importEqualsDeclaration.name = identifier;
4544+
parseExpected(SyntaxKind.EqualsToken);
4545+
importEqualsDeclaration.moduleReference = parseModuleReference();
4546+
parseSemicolon();
4547+
return finishNode(importEqualsDeclaration);
4548+
}
45394549
}
45404550

4541-
var node = <ImportEqualsDeclaration>createNode(SyntaxKind.ImportEqualsDeclaration, fullStart);
4542-
setModifiers(node, modifiers);
4543-
node.name = parseIdentifier();
4544-
parseExpected(SyntaxKind.EqualsToken);
4545-
node.moduleReference = parseModuleReference();
4551+
// Import statement
4552+
var importStatement = <ImportStatement>createNode(SyntaxKind.ImportStatement, fullStart);
4553+
setModifiers(importStatement, modifiers);
4554+
4555+
// ImportDeclaration:
4556+
// import ImportClause from ModuleSpecifier ;
4557+
// import ModuleSpecifier;
4558+
if (identifier || // import id
4559+
token === SyntaxKind.AsteriskToken || // import *
4560+
token === SyntaxKind.OpenBraceToken) { // import {
4561+
//ImportClause:
4562+
// ImportedDefaultBinding
4563+
// NameSpaceImport
4564+
// NamedImports
4565+
// ImportedDefaultBinding, NameSpaceImport
4566+
// ImportedDefaultBinding, NamedImports
4567+
4568+
var importClause = <ImportClause>createNode(SyntaxKind.ImportClause);
4569+
if (identifier) {
4570+
// ImportedDefaultBinding:
4571+
// ImportedBinding
4572+
importClause.defaultBinding = identifier;
4573+
}
4574+
4575+
// If there was no default import or if there is comma token after default import
4576+
// parse namespace or named imports
4577+
if (!importClause.defaultBinding ||
4578+
parseOptional(SyntaxKind.CommaToken)) {
4579+
importClause.namedBindings = token === SyntaxKind.AsteriskToken ? parseNamespaceImport() : parseNamedImports();
4580+
}
4581+
4582+
importStatement.importClause = finishNode(importClause);
4583+
parseExpected(SyntaxKind.FromKeyword);
4584+
}
4585+
4586+
importStatement.moduleSpecifier = parseModuleSpecifier();
45464587
parseSemicolon();
4547-
return finishNode(node);
4588+
return finishNode(importStatement);
45484589
}
45494590

45504591
function parseModuleReference() {
@@ -4573,61 +4614,18 @@ module ts {
45734614
return finishNode(node);
45744615
}
45754616

4576-
function parseImportStatement(fullStart: number, modifiers: ModifiersArray): ImportStatement {
4577-
var node = <ImportStatement>createNode(SyntaxKind.ImportStatement, fullStart);
4578-
setModifiers(node, modifiers);
4579-
4580-
// ImportDeclaration:
4581-
// import ImportClause ModuleSpecifier ;
4582-
// import ModuleSpecifier;
4583-
if (token !== SyntaxKind.StringLiteral) {
4584-
// ImportDeclaration:
4585-
node.importClause = parseImportClause();
4586-
}
4587-
node.moduleSpecifier = parseModuleSpecifier();
4588-
parseSemicolon();
4589-
return finishNode(node);
4590-
}
4591-
45924617
function parseModuleSpecifier(): StringLiteralExpression {
45934618
// ModuleSpecifier:
45944619
// StringLiteral
45954620
if (token === SyntaxKind.StringLiteral) {
45964621
// Ensure the string being required is in our 'identifier' table. This will ensure
45974622
// that features like 'find refs' will look inside this file when search for its name.
4598-
var moduleSpecifier = <StringLiteralExpression>parseLiteralNode(/*internName*/ true);
4599-
return moduleSpecifier;
4623+
return <StringLiteralExpression>parseLiteralNode(/*internName*/ true);
46004624
}
46014625

46024626
parseErrorAtCurrentToken(Diagnostics.String_literal_expected);
46034627
}
46044628

4605-
function parseImportClause(): ImportClause {
4606-
//ImportClause:
4607-
// ImportedDefaultBinding from
4608-
// NameSpaceImport from
4609-
// NamedImports from
4610-
// ImportedDefaultBinding, NameSpaceImport from
4611-
// ImportedDefaultBinding, NamedImports from
4612-
4613-
var importClause = <ImportClause>createNode(SyntaxKind.ImportClause);
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.namedBindings = token === SyntaxKind.AsteriskToken ? parseNamespaceImport() : parseNamedImports();
4625-
}
4626-
4627-
parseExpected(SyntaxKind.FromKeyword);
4628-
return finishNode(importClause);
4629-
}
4630-
46314629
function parseNamespaceImport(): NamespaceImport {
46324630
// NameSpaceImport:
46334631
// * as ImportedBinding
@@ -4649,9 +4647,7 @@ module ts {
46494647
// ImportsList:
46504648
// ImportSpecifier
46514649
// ImportsList, ImportSpecifier
4652-
parseExpected(SyntaxKind.OpenBraceToken);
4653-
namedImports.elements = parseDelimitedList(ParsingContext.ImportSpecifiers, parseImportSpecifier);
4654-
parseExpected(SyntaxKind.CloseBraceToken);
4650+
namedImports.elements = parseBracketedList(ParsingContext.ImportSpecifiers, parseImportSpecifier, SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken);
46554651
return finishNode(namedImports);
46564652
}
46574653

@@ -4698,7 +4694,7 @@ module ts {
46984694
return lookAhead(nextTokenIsIdentifierOrKeyword);
46994695
case SyntaxKind.ImportKeyword:
47004696
// Not true keywords so ensure an identifier follows or is string literal or asterisk or open brace
4701-
return lookAhead(nextTokenIsIdentifierOrKeywordOrStringLiteralOrAsteriskOrOpenBrace) ;
4697+
return lookAhead(nextTokenCanFollowImportKeyword);
47024698
case SyntaxKind.ModuleKeyword:
47034699
// Not a true keyword so ensure an identifier or string literal follows
47044700
return lookAhead(nextTokenIsIdentifierOrKeywordOrStringLiteral);
@@ -4729,7 +4725,7 @@ module ts {
47294725
return isIdentifierOrKeyword() || token === SyntaxKind.StringLiteral;
47304726
}
47314727

4732-
function nextTokenIsIdentifierOrKeywordOrStringLiteralOrAsteriskOrOpenBrace() {
4728+
function nextTokenCanFollowImportKeyword() {
47334729
nextToken();
47344730
return isIdentifierOrKeyword() || token === SyntaxKind.StringLiteral ||
47354731
token === SyntaxKind.AsteriskToken || token === SyntaxKind.OpenBraceToken;

src/compiler/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,11 +868,21 @@ module ts {
868868
expression?: Expression;
869869
}
870870

871+
// In case of:
872+
// import "mod" => importClause = undefined, moduleSpecifier = "mod"
873+
// In rest of the cases, module specifier is string literal corresponding to module
874+
// ImportClause information is shown at its declaration below.
871875
export interface ImportStatement extends Statement, ModuleElement {
872876
importClause?: ImportClause;
873877
moduleSpecifier: StringLiteralExpression;
874878
}
875879

880+
// In case of:
881+
// import d from "mod" => defaultBinding = d, namedBinding = undefined
882+
// import * as ns from "mod" => defaultBinding = undefined, namedBinding: NamespaceImport = { name: ns }
883+
// import d, * as ns from "mod" => defaultBinding = d, namedBinding: NamespaceImport = { name: ns }
884+
// import { a, b as x } from "mod" => defaultBinding = undefined, namedBinding: NamedImports = { elements: [{ name: a }, { name: x, propertyName: b}]}
885+
// import d, { a, b as x } from "mod" => defaultBinding = d, namedBinding: NamedImports = { elements: [{ name: a }, { name: x, propertyName: b}]}
876886
export interface ImportClause extends Node {
877887
defaultBinding?: Identifier;
878888
namedBindings?: NamespaceImport | NamedImports;

tests/baselines/reference/es6ImportNamedImportParsingError.errors.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ tests/cases/compiler/es6ImportNamedImportParsingError_1.ts(1,12): error TS1109:
33
tests/cases/compiler/es6ImportNamedImportParsingError_1.ts(1,14): error TS2304: Cannot find name 'from'.
44
tests/cases/compiler/es6ImportNamedImportParsingError_1.ts(1,19): error TS1005: ';' expected.
55
tests/cases/compiler/es6ImportNamedImportParsingError_1.ts(2,24): error TS1005: '{' expected.
6-
tests/cases/compiler/es6ImportNamedImportParsingError_1.ts(2,29): error TS1005: ',' expected.
76

87

98
==== tests/cases/compiler/es6ImportNamedImportParsingError_0.ts (0 errors) ====
@@ -12,7 +11,7 @@ tests/cases/compiler/es6ImportNamedImportParsingError_1.ts(2,29): error TS1005:
1211
export var x = a;
1312
export var m = a;
1413

15-
==== tests/cases/compiler/es6ImportNamedImportParsingError_1.ts (6 errors) ====
14+
==== tests/cases/compiler/es6ImportNamedImportParsingError_1.ts (5 errors) ====
1615
import { * } from "es6ImportNamedImportParsingError_0";
1716
~
1817
!!! error TS1003: Identifier expected.
@@ -24,6 +23,4 @@ tests/cases/compiler/es6ImportNamedImportParsingError_1.ts(2,29): error TS1005:
2423
!!! error TS1005: ';' expected.
2524
import defaultBinding, from "es6ImportNamedImportParsingError_0";
2625
~~~~
27-
!!! error TS1005: '{' expected.
28-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29-
!!! error TS1005: ',' expected.
26+
!!! error TS1005: '{' expected.

0 commit comments

Comments
 (0)