Skip to content

Commit f7c40d3

Browse files
yuitmhegazy
authored andcommitted
[Release 2.1] fix11754 global augmentation (#12133)
* Exclude global augmentation from module resolution logic * Address PR: check using string literal instead of NodeFlags.globalAugmentation
1 parent 6ba4b87 commit f7c40d3

6 files changed

+45
-2
lines changed

src/compiler/program.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,9 @@ namespace ts {
526526

527527
const newSourceFilePath = getNormalizedAbsolutePath(newSourceFile.fileName, currentDirectory);
528528
if (resolveModuleNamesWorker) {
529-
const moduleNames = map(concatenate(newSourceFile.imports, newSourceFile.moduleAugmentations), getTextOfLiteral);
529+
// Because global augmentation doesn't have string literal name, we can check for global augmentation as such.
530+
const nonGlobalAugmentation = filter(newSourceFile.moduleAugmentations, (moduleAugmentation) => moduleAugmentation.kind === SyntaxKind.StringLiteral);
531+
const moduleNames = map(concatenate(newSourceFile.imports, nonGlobalAugmentation), getTextOfLiteral);
530532
const resolutions = resolveModuleNamesWorker(moduleNames, newSourceFilePath);
531533
// ensure that module resolution results are still correct
532534
const resolutionsChanged = hasChangesInResolutions(moduleNames, resolutions, oldSourceFile.resolvedModules, moduleResolutionIsEqualTo);
@@ -1290,7 +1292,9 @@ namespace ts {
12901292
collectExternalModuleReferences(file);
12911293
if (file.imports.length || file.moduleAugmentations.length) {
12921294
file.resolvedModules = createMap<ResolvedModule>();
1293-
const moduleNames = map(concatenate(file.imports, file.moduleAugmentations), getTextOfLiteral);
1295+
// Because global augmentation doesn't have string literal name, we can check for global augmentation as such.
1296+
const nonGlobalAugmentation = filter(file.moduleAugmentations, (moduleAugmentation) => moduleAugmentation.kind === SyntaxKind.StringLiteral);
1297+
const moduleNames = map(concatenate(file.imports, nonGlobalAugmentation), getTextOfLiteral);
12941298
const resolutions = resolveModuleNamesWorker(moduleNames, getNormalizedAbsolutePath(file.fileName, currentDirectory));
12951299
for (let i = 0; i < moduleNames.length; i++) {
12961300
const resolution = resolutions[i];
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [a.ts]
2+
3+
export { };
4+
5+
declare global {
6+
var x: number;
7+
}
8+
9+
//// [a.js]
10+
"use strict";
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/conformance/externalModules/a.ts ===
2+
3+
export { };
4+
5+
declare global {
6+
>global : Symbol(global, Decl(a.ts, 1, 11))
7+
8+
var x: number;
9+
>x : Symbol(x, Decl(a.ts, 4, 5))
10+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/conformance/externalModules/a.ts ===
2+
3+
export { };
4+
5+
declare global {
6+
>global : any
7+
8+
var x: number;
9+
>x : number
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @traceResolution: true
2+
3+
// @fileName: a.ts
4+
export { };
5+
6+
declare global {
7+
var x: number;
8+
}

0 commit comments

Comments
 (0)