@@ -1574,10 +1574,10 @@ namespace ts {
15741574 return a . fileName === b . fileName ;
15751575 }
15761576
1577- function moduleNameIsEqualTo ( a : StringLiteral | Identifier , b : StringLiteral | Identifier ) : boolean {
1578- return a . kind === SyntaxKind . StringLiteral
1579- ? b . kind === SyntaxKind . StringLiteral && a . text === b . text
1580- : b . kind === SyntaxKind . Identifier && a . escapedText === b . escapedText ;
1577+ function moduleNameIsEqualTo ( a : StringLiteralLike | Identifier , b : StringLiteralLike | Identifier ) : boolean {
1578+ return a . kind === SyntaxKind . Identifier
1579+ ? b . kind === SyntaxKind . Identifier && a . escapedText === b . escapedText
1580+ : b . kind === SyntaxKind . StringLiteral && a . text === b . text ;
15811581 }
15821582
15831583 function collectExternalModuleReferences ( file : SourceFile ) : void {
@@ -1589,7 +1589,7 @@ namespace ts {
15891589 const isExternalModuleFile = isExternalModule ( file ) ;
15901590
15911591 // file.imports may not be undefined if there exists dynamic import
1592- let imports : StringLiteral [ ] ;
1592+ let imports : StringLiteralLike [ ] | undefined ;
15931593 let moduleAugmentations : ( StringLiteral | Identifier ) [ ] ;
15941594 let ambientModules : string [ ] ;
15951595
@@ -1600,7 +1600,7 @@ namespace ts {
16001600 && ! file . isDeclarationFile ) {
16011601 // synthesize 'import "tslib"' declaration
16021602 const externalHelpersModuleReference = createLiteral ( externalHelpersModuleNameText ) ;
1603- const importDecl = createImportDeclaration ( /*decorators*/ undefined , /*modifiers*/ undefined , /*importClause*/ undefined ) ;
1603+ const importDecl = createImportDeclaration ( /*decorators*/ undefined , /*modifiers*/ undefined , /*importClause*/ undefined , externalHelpersModuleReference ) ;
16041604 addEmitFlags ( importDecl , EmitFlags . NeverApplyImportHelper ) ;
16051605 externalHelpersModuleReference . parent = importDecl ;
16061606 importDecl . parent = file ;
@@ -1621,66 +1621,55 @@ namespace ts {
16211621 return ;
16221622
16231623 function collectModuleReferences ( node : Statement , inAmbientModule : boolean ) : void {
1624- switch ( node . kind ) {
1625- case SyntaxKind . ImportDeclaration :
1626- case SyntaxKind . ImportEqualsDeclaration :
1627- case SyntaxKind . ExportDeclaration :
1628- const moduleNameExpr = getExternalModuleName ( node ) ;
1629- if ( ! moduleNameExpr || ! isStringLiteral ( moduleNameExpr ) ) {
1630- break ;
1631- }
1632- if ( ! moduleNameExpr . text ) {
1633- break ;
1634- }
1635-
1636- // TypeScript 1.0 spec (April 2014): 12.1.6
1637- // An ExternalImportDeclaration in an AmbientExternalModuleDeclaration may reference other external modules
1638- // only through top - level external module names. Relative external module names are not permitted.
1639- if ( ! inAmbientModule || ! isExternalModuleNameRelative ( moduleNameExpr . text ) ) {
1640- ( imports || ( imports = [ ] ) ) . push ( moduleNameExpr ) ;
1624+ if ( isAnyImportOrReExport ( node ) ) {
1625+ const moduleNameExpr = getExternalModuleName ( node ) ;
1626+ // TypeScript 1.0 spec (April 2014): 12.1.6
1627+ // An ExternalImportDeclaration in an AmbientExternalModuleDeclaration may reference other external modules
1628+ // only through top - level external module names. Relative external module names are not permitted.
1629+ if ( moduleNameExpr && isStringLiteral ( moduleNameExpr ) && moduleNameExpr . text && ( ! inAmbientModule || ! isExternalModuleNameRelative ( moduleNameExpr . text ) ) ) {
1630+ imports = append ( imports , moduleNameExpr ) ;
1631+ }
1632+ }
1633+ else if ( isModuleDeclaration ( node ) ) {
1634+ if ( isAmbientModule ( node ) && ( inAmbientModule || hasModifier ( node , ModifierFlags . Ambient ) || file . isDeclarationFile ) ) {
1635+ const nameText = getTextOfIdentifierOrLiteral ( node . name ) ;
1636+ // Ambient module declarations can be interpreted as augmentations for some existing external modules.
1637+ // This will happen in two cases:
1638+ // - if current file is external module then module augmentation is a ambient module declaration defined in the top level scope
1639+ // - if current file is not external module then module augmentation is an ambient module declaration with non-relative module name
1640+ // immediately nested in top level ambient module declaration .
1641+ if ( isExternalModuleFile || ( inAmbientModule && ! isExternalModuleNameRelative ( nameText ) ) ) {
1642+ ( moduleAugmentations || ( moduleAugmentations = [ ] ) ) . push ( node . name ) ;
16411643 }
1642- break ;
1643- case SyntaxKind . ModuleDeclaration :
1644- if ( isAmbientModule ( < ModuleDeclaration > node ) && ( inAmbientModule || hasModifier ( node , ModifierFlags . Ambient ) || file . isDeclarationFile ) ) {
1645- const moduleName = ( < ModuleDeclaration > node ) . name ;
1646- const nameText = getTextOfIdentifierOrLiteral ( moduleName ) ;
1647- // Ambient module declarations can be interpreted as augmentations for some existing external modules.
1648- // This will happen in two cases:
1649- // - if current file is external module then module augmentation is a ambient module declaration defined in the top level scope
1650- // - if current file is not external module then module augmentation is an ambient module declaration with non-relative module name
1651- // immediately nested in top level ambient module declaration .
1652- if ( isExternalModuleFile || ( inAmbientModule && ! isExternalModuleNameRelative ( nameText ) ) ) {
1653- ( moduleAugmentations || ( moduleAugmentations = [ ] ) ) . push ( moduleName ) ;
1644+ else if ( ! inAmbientModule ) {
1645+ if ( file . isDeclarationFile ) {
1646+ // for global .d.ts files record name of ambient module
1647+ ( ambientModules || ( ambientModules = [ ] ) ) . push ( nameText ) ;
16541648 }
1655- else if ( ! inAmbientModule ) {
1656- if ( file . isDeclarationFile ) {
1657- // for global .d.ts files record name of ambient module
1658- ( ambientModules || ( ambientModules = [ ] ) ) . push ( nameText ) ;
1659- }
1660- // An AmbientExternalModuleDeclaration declares an external module.
1661- // This type of declaration is permitted only in the global module.
1662- // The StringLiteral must specify a top - level external module name.
1663- // Relative external module names are not permitted
1664-
1665- // NOTE: body of ambient module is always a module block, if it exists
1666- const body = < ModuleBlock > ( < ModuleDeclaration > node ) . body ;
1667- if ( body ) {
1668- for ( const statement of body . statements ) {
1669- collectModuleReferences ( statement , /*inAmbientModule*/ true ) ;
1670- }
1649+ // An AmbientExternalModuleDeclaration declares an external module.
1650+ // This type of declaration is permitted only in the global module.
1651+ // The StringLiteral must specify a top - level external module name.
1652+ // Relative external module names are not permitted
1653+
1654+ // NOTE: body of ambient module is always a module block, if it exists
1655+ const body = < ModuleBlock > ( < ModuleDeclaration > node ) . body ;
1656+ if ( body ) {
1657+ for ( const statement of body . statements ) {
1658+ collectModuleReferences ( statement , /*inAmbientModule*/ true ) ;
16711659 }
16721660 }
16731661 }
1662+ }
16741663 }
16751664 }
16761665
16771666 function collectDynamicImportOrRequireCalls ( node : Node ) : void {
1678- if ( isRequireCall ( node , /*checkArgumentIsStringLiteral */ true ) ) {
1679- ( imports || ( imports = [ ] ) ) . push ( < StringLiteral > ( < CallExpression > node ) . arguments [ 0 ] ) ;
1667+ if ( isRequireCall ( node , /*checkArgumentIsStringLiteralLike */ true ) ) {
1668+ imports = append ( imports , node . arguments [ 0 ] ) ;
16801669 }
16811670 // we have to check the argument list has length of 1. We will still have to process these even though we have parsing error.
1682- else if ( isImportCall ( node ) && node . arguments . length === 1 && node . arguments [ 0 ] . kind === SyntaxKind . StringLiteral ) {
1683- ( imports || ( imports = [ ] ) ) . push ( < StringLiteral > ( < CallExpression > node ) . arguments [ 0 ] ) ;
1671+ else if ( isImportCall ( node ) && node . arguments . length === 1 && isStringLiteralLike ( node . arguments [ 0 ] ) ) {
1672+ imports = append ( imports , node . arguments [ 0 ] as StringLiteralLike ) ;
16841673 }
16851674 else {
16861675 forEachChild ( node , collectDynamicImportOrRequireCalls ) ;
0 commit comments