From bdf7e971600b572df5660161144333d3088d0cc6 Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Wed, 9 Nov 2016 11:33:40 -0800 Subject: [PATCH 1/2] Exclude global augmentation from module resolution logic --- src/compiler/parser.ts | 2 ++ src/compiler/program.ts | 6 ++++-- .../reference/globalAugmentationModuleResolution.js | 10 ++++++++++ .../globalAugmentationModuleResolution.symbols | 10 ++++++++++ .../globalAugmentationModuleResolution.trace.json | 1 + .../reference/globalAugmentationModuleResolution.types | 10 ++++++++++ .../globalAugmentationModuleResolution.ts | 8 ++++++++ 7 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 tests/baselines/reference/globalAugmentationModuleResolution.js create mode 100644 tests/baselines/reference/globalAugmentationModuleResolution.symbols create mode 100644 tests/baselines/reference/globalAugmentationModuleResolution.trace.json create mode 100644 tests/baselines/reference/globalAugmentationModuleResolution.types create mode 100644 tests/cases/conformance/externalModules/globalAugmentationModuleResolution.ts diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index a5b02f98fd382..35ffd8e17e256 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -5418,6 +5418,8 @@ namespace ts { // parse 'global' as name of global scope augmentation node.name = parseIdentifier(); node.flags |= NodeFlags.GlobalAugmentation; + // We need to store flags at the name as well so we can use it to filter out global augmentation from going through module resolution. + node.name.flags |= NodeFlags.GlobalAugmentation; } else { node.name = parseLiteralNode(/*internName*/ true); diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 7a16bccd54486..457709d9f3a47 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -526,7 +526,8 @@ namespace ts { const newSourceFilePath = getNormalizedAbsolutePath(newSourceFile.fileName, currentDirectory); if (resolveModuleNamesWorker) { - const moduleNames = map(concatenate(newSourceFile.imports, newSourceFile.moduleAugmentations), getTextOfLiteral); + const nonGlobalAugmentation = filter(newSourceFile.moduleAugmentations, (moduleAugmentation) => !(moduleAugmentation.flags & NodeFlags.GlobalAugmentation)); + const moduleNames = map(concatenate(newSourceFile.imports, nonGlobalAugmentation), getTextOfLiteral); const resolutions = resolveModuleNamesWorker(moduleNames, newSourceFilePath); // ensure that module resolution results are still correct const resolutionsChanged = hasChangesInResolutions(moduleNames, resolutions, oldSourceFile.resolvedModules, moduleResolutionIsEqualTo); @@ -1290,7 +1291,8 @@ namespace ts { collectExternalModuleReferences(file); if (file.imports.length || file.moduleAugmentations.length) { file.resolvedModules = createMap(); - const moduleNames = map(concatenate(file.imports, file.moduleAugmentations), getTextOfLiteral); + const nonGlobalAugmentation = filter(file.moduleAugmentations, (moduleAugmentation) => !(moduleAugmentation.flags & NodeFlags.GlobalAugmentation)); + const moduleNames = map(concatenate(file.imports, nonGlobalAugmentation), getTextOfLiteral); const resolutions = resolveModuleNamesWorker(moduleNames, getNormalizedAbsolutePath(file.fileName, currentDirectory)); for (let i = 0; i < moduleNames.length; i++) { const resolution = resolutions[i]; diff --git a/tests/baselines/reference/globalAugmentationModuleResolution.js b/tests/baselines/reference/globalAugmentationModuleResolution.js new file mode 100644 index 0000000000000..e1a4ebce8c5de --- /dev/null +++ b/tests/baselines/reference/globalAugmentationModuleResolution.js @@ -0,0 +1,10 @@ +//// [a.ts] + +export { }; + +declare global { + var x: number; +} + +//// [a.js] +"use strict"; diff --git a/tests/baselines/reference/globalAugmentationModuleResolution.symbols b/tests/baselines/reference/globalAugmentationModuleResolution.symbols new file mode 100644 index 0000000000000..614860d654e97 --- /dev/null +++ b/tests/baselines/reference/globalAugmentationModuleResolution.symbols @@ -0,0 +1,10 @@ +=== tests/cases/conformance/externalModules/a.ts === + +export { }; + +declare global { +>global : Symbol(global, Decl(a.ts, 1, 11)) + + var x: number; +>x : Symbol(x, Decl(a.ts, 4, 5)) +} diff --git a/tests/baselines/reference/globalAugmentationModuleResolution.trace.json b/tests/baselines/reference/globalAugmentationModuleResolution.trace.json new file mode 100644 index 0000000000000..0637a088a01e8 --- /dev/null +++ b/tests/baselines/reference/globalAugmentationModuleResolution.trace.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/tests/baselines/reference/globalAugmentationModuleResolution.types b/tests/baselines/reference/globalAugmentationModuleResolution.types new file mode 100644 index 0000000000000..c057ea2943db4 --- /dev/null +++ b/tests/baselines/reference/globalAugmentationModuleResolution.types @@ -0,0 +1,10 @@ +=== tests/cases/conformance/externalModules/a.ts === + +export { }; + +declare global { +>global : any + + var x: number; +>x : number +} diff --git a/tests/cases/conformance/externalModules/globalAugmentationModuleResolution.ts b/tests/cases/conformance/externalModules/globalAugmentationModuleResolution.ts new file mode 100644 index 0000000000000..4d87ffcab1398 --- /dev/null +++ b/tests/cases/conformance/externalModules/globalAugmentationModuleResolution.ts @@ -0,0 +1,8 @@ +// @traceResolution: true + +// @fileName: a.ts +export { }; + +declare global { + var x: number; +} \ No newline at end of file From 9bbe05bd39340fba15621d89275874d3fdb77af4 Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Wed, 9 Nov 2016 15:23:51 -0800 Subject: [PATCH 2/2] Address PR: check using string literal instead of NodeFlags.globalAugmentation --- src/compiler/parser.ts | 2 -- src/compiler/program.ts | 6 ++++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 35ffd8e17e256..a5b02f98fd382 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -5418,8 +5418,6 @@ namespace ts { // parse 'global' as name of global scope augmentation node.name = parseIdentifier(); node.flags |= NodeFlags.GlobalAugmentation; - // We need to store flags at the name as well so we can use it to filter out global augmentation from going through module resolution. - node.name.flags |= NodeFlags.GlobalAugmentation; } else { node.name = parseLiteralNode(/*internName*/ true); diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 457709d9f3a47..1e12ad52d88f0 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -526,7 +526,8 @@ namespace ts { const newSourceFilePath = getNormalizedAbsolutePath(newSourceFile.fileName, currentDirectory); if (resolveModuleNamesWorker) { - const nonGlobalAugmentation = filter(newSourceFile.moduleAugmentations, (moduleAugmentation) => !(moduleAugmentation.flags & NodeFlags.GlobalAugmentation)); + // Because global augmentation doesn't have string literal name, we can check for global augmentation as such. + const nonGlobalAugmentation = filter(newSourceFile.moduleAugmentations, (moduleAugmentation) => moduleAugmentation.kind === SyntaxKind.StringLiteral); const moduleNames = map(concatenate(newSourceFile.imports, nonGlobalAugmentation), getTextOfLiteral); const resolutions = resolveModuleNamesWorker(moduleNames, newSourceFilePath); // ensure that module resolution results are still correct @@ -1291,7 +1292,8 @@ namespace ts { collectExternalModuleReferences(file); if (file.imports.length || file.moduleAugmentations.length) { file.resolvedModules = createMap(); - const nonGlobalAugmentation = filter(file.moduleAugmentations, (moduleAugmentation) => !(moduleAugmentation.flags & NodeFlags.GlobalAugmentation)); + // Because global augmentation doesn't have string literal name, we can check for global augmentation as such. + const nonGlobalAugmentation = filter(file.moduleAugmentations, (moduleAugmentation) => moduleAugmentation.kind === SyntaxKind.StringLiteral); const moduleNames = map(concatenate(file.imports, nonGlobalAugmentation), getTextOfLiteral); const resolutions = resolveModuleNamesWorker(moduleNames, getNormalizedAbsolutePath(file.fileName, currentDirectory)); for (let i = 0; i < moduleNames.length; i++) {