@@ -4531,20 +4531,61 @@ module ts {
4531
4531
4532
4532
function parseImportDeclarationOrStatement ( fullStart : number , modifiers : ModifiersArray ) : ImportEqualsDeclaration | ImportStatement {
4533
4533
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
+ }
4539
4549
}
4540
4550
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 ( ) ;
4546
4587
parseSemicolon ( ) ;
4547
- return finishNode ( node ) ;
4588
+ return finishNode ( importStatement ) ;
4548
4589
}
4549
4590
4550
4591
function parseModuleReference ( ) {
@@ -4573,61 +4614,18 @@ module ts {
4573
4614
return finishNode ( node ) ;
4574
4615
}
4575
4616
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
-
4592
4617
function parseModuleSpecifier ( ) : StringLiteralExpression {
4593
4618
// ModuleSpecifier:
4594
4619
// StringLiteral
4595
4620
if ( token === SyntaxKind . StringLiteral ) {
4596
4621
// Ensure the string being required is in our 'identifier' table. This will ensure
4597
4622
// 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 ) ;
4600
4624
}
4601
4625
4602
4626
parseErrorAtCurrentToken ( Diagnostics . String_literal_expected ) ;
4603
4627
}
4604
4628
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
-
4631
4629
function parseNamespaceImport ( ) : NamespaceImport {
4632
4630
// NameSpaceImport:
4633
4631
// * as ImportedBinding
@@ -4649,9 +4647,7 @@ module ts {
4649
4647
// ImportsList:
4650
4648
// ImportSpecifier
4651
4649
// 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 ) ;
4655
4651
return finishNode ( namedImports ) ;
4656
4652
}
4657
4653
@@ -4698,7 +4694,7 @@ module ts {
4698
4694
return lookAhead ( nextTokenIsIdentifierOrKeyword ) ;
4699
4695
case SyntaxKind . ImportKeyword :
4700
4696
// 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 ) ;
4702
4698
case SyntaxKind . ModuleKeyword :
4703
4699
// Not a true keyword so ensure an identifier or string literal follows
4704
4700
return lookAhead ( nextTokenIsIdentifierOrKeywordOrStringLiteral ) ;
@@ -4729,7 +4725,7 @@ module ts {
4729
4725
return isIdentifierOrKeyword ( ) || token === SyntaxKind . StringLiteral ;
4730
4726
}
4731
4727
4732
- function nextTokenIsIdentifierOrKeywordOrStringLiteralOrAsteriskOrOpenBrace ( ) {
4728
+ function nextTokenCanFollowImportKeyword ( ) {
4733
4729
nextToken ( ) ;
4734
4730
return isIdentifierOrKeyword ( ) || token === SyntaxKind . StringLiteral ||
4735
4731
token === SyntaxKind . AsteriskToken || token === SyntaxKind . OpenBraceToken ;
0 commit comments