@@ -1574,10 +1574,10 @@ namespace ts {
1574
1574
return a . fileName === b . fileName ;
1575
1575
}
1576
1576
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 ;
1581
1581
}
1582
1582
1583
1583
function collectExternalModuleReferences ( file : SourceFile ) : void {
@@ -1589,7 +1589,7 @@ namespace ts {
1589
1589
const isExternalModuleFile = isExternalModule ( file ) ;
1590
1590
1591
1591
// file.imports may not be undefined if there exists dynamic import
1592
- let imports : StringLiteral [ ] ;
1592
+ let imports : StringLiteralLike [ ] | undefined ;
1593
1593
let moduleAugmentations : ( StringLiteral | Identifier ) [ ] ;
1594
1594
let ambientModules : string [ ] ;
1595
1595
@@ -1600,7 +1600,7 @@ namespace ts {
1600
1600
&& ! file . isDeclarationFile ) {
1601
1601
// synthesize 'import "tslib"' declaration
1602
1602
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 ) ;
1604
1604
addEmitFlags ( importDecl , EmitFlags . NeverApplyImportHelper ) ;
1605
1605
externalHelpersModuleReference . parent = importDecl ;
1606
1606
importDecl . parent = file ;
@@ -1621,66 +1621,55 @@ namespace ts {
1621
1621
return ;
1622
1622
1623
1623
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 ) ;
1641
1643
}
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 ) ;
1654
1648
}
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 ) ;
1671
1659
}
1672
1660
}
1673
1661
}
1662
+ }
1674
1663
}
1675
1664
}
1676
1665
1677
1666
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 ] ) ;
1680
1669
}
1681
1670
// 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 ) ;
1684
1673
}
1685
1674
else {
1686
1675
forEachChild ( node , collectDynamicImportOrRequireCalls ) ;
0 commit comments