@@ -890,7 +890,7 @@ namespace ts {
890
890
for ( const { oldFile : oldSourceFile , newFile : newSourceFile } of modifiedSourceFiles ) {
891
891
const newSourceFilePath = getNormalizedAbsolutePath ( newSourceFile . fileName , currentDirectory ) ;
892
892
if ( resolveModuleNamesWorker ) {
893
- const moduleNames = map ( concatenate ( newSourceFile . imports , newSourceFile . moduleAugmentations ) , getTextOfLiteral ) ;
893
+ const moduleNames = getModuleNames ( newSourceFile ) ;
894
894
const oldProgramState = { program : oldProgram , file : oldSourceFile , modifiedFilePaths } ;
895
895
const resolutions = resolveModuleNamesReusingOldState ( moduleNames , newSourceFilePath , newSourceFile , oldProgramState ) ;
896
896
// ensure that module resolution results are still correct
@@ -1434,12 +1434,10 @@ namespace ts {
1434
1434
return a . fileName === b . fileName ;
1435
1435
}
1436
1436
1437
- function moduleNameIsEqualTo ( a : LiteralExpression , b : LiteralExpression ) : boolean {
1438
- return a . text === b . text ;
1439
- }
1440
-
1441
- function getTextOfLiteral ( literal : LiteralExpression ) : string {
1442
- return literal . text ;
1437
+ function moduleNameIsEqualTo ( a : StringLiteral | Identifier , b : StringLiteral | Identifier ) : boolean {
1438
+ return a . kind === SyntaxKind . StringLiteral
1439
+ ? b . kind === SyntaxKind . StringLiteral && a . text === b . text
1440
+ : b . kind === SyntaxKind . Identifier && a . escapedText === b . escapedText ;
1443
1441
}
1444
1442
1445
1443
function collectExternalModuleReferences ( file : SourceFile ) : void {
@@ -1452,7 +1450,7 @@ namespace ts {
1452
1450
1453
1451
// file.imports may not be undefined if there exists dynamic import
1454
1452
let imports : StringLiteral [ ] ;
1455
- let moduleAugmentations : StringLiteral [ ] ;
1453
+ let moduleAugmentations : Array < StringLiteral | Identifier > ;
1456
1454
let ambientModules : string [ ] ;
1457
1455
1458
1456
// If we are importing helpers, we need to add a synthetic reference to resolve the
@@ -1481,7 +1479,7 @@ namespace ts {
1481
1479
1482
1480
return ;
1483
1481
1484
- function collectModuleReferences ( node : Node , inAmbientModule : boolean ) : void {
1482
+ function collectModuleReferences ( node : Statement , inAmbientModule : boolean ) : void {
1485
1483
switch ( node . kind ) {
1486
1484
case SyntaxKind . ImportDeclaration :
1487
1485
case SyntaxKind . ImportEqualsDeclaration :
@@ -1503,8 +1501,8 @@ namespace ts {
1503
1501
break ;
1504
1502
case SyntaxKind . ModuleDeclaration :
1505
1503
if ( isAmbientModule ( < ModuleDeclaration > node ) && ( inAmbientModule || hasModifier ( node , ModifierFlags . Ambient ) || file . isDeclarationFile ) ) {
1506
- const moduleName = < StringLiteral > ( < ModuleDeclaration > node ) . name ; // TODO: GH#17347
1507
- const nameText = ts . getTextOfIdentifierOrLiteral ( moduleName ) ;
1504
+ const moduleName = ( < ModuleDeclaration > node ) . name ;
1505
+ const nameText = getTextOfIdentifierOrLiteral ( moduleName ) ;
1508
1506
// Ambient module declarations can be interpreted as augmentations for some existing external modules.
1509
1507
// This will happen in two cases:
1510
1508
// - if current file is external module then module augmentation is a ambient module declaration defined in the top level scope
@@ -1821,8 +1819,7 @@ namespace ts {
1821
1819
collectExternalModuleReferences ( file ) ;
1822
1820
if ( file . imports . length || file . moduleAugmentations . length ) {
1823
1821
// Because global augmentation doesn't have string literal name, we can check for global augmentation as such.
1824
- const nonGlobalAugmentation = filter ( file . moduleAugmentations , ( moduleAugmentation ) => moduleAugmentation . kind === SyntaxKind . StringLiteral ) ;
1825
- const moduleNames = map ( concatenate ( file . imports , nonGlobalAugmentation ) , getTextOfLiteral ) ;
1822
+ const moduleNames = getModuleNames ( file ) ;
1826
1823
const oldProgramState = { program : oldProgram , file, modifiedFilePaths } ;
1827
1824
const resolutions = resolveModuleNamesReusingOldState ( moduleNames , getNormalizedAbsolutePath ( file . fileName , currentDirectory ) , file , oldProgramState ) ;
1828
1825
Debug . assert ( resolutions . length === moduleNames . length ) ;
@@ -2233,4 +2230,15 @@ namespace ts {
2233
2230
Debug . assert ( names . every ( name => name !== undefined ) , "A name is undefined." , ( ) => JSON . stringify ( names ) ) ;
2234
2231
return names ;
2235
2232
}
2233
+
2234
+ function getModuleNames ( { imports, moduleAugmentations } : SourceFile ) : string [ ] {
2235
+ const res = imports . map ( i => i . text ) ;
2236
+ for ( const aug of moduleAugmentations ) {
2237
+ if ( aug . kind === SyntaxKind . StringLiteral ) {
2238
+ res . push ( aug . text ) ;
2239
+ }
2240
+ // Do nothing if it's an Identifier; we don't need to do module resolution for `declare global`.
2241
+ }
2242
+ return res ;
2243
+ }
2236
2244
}
0 commit comments