@@ -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 ;
0 commit comments