From 946a14f3f3d1ddb65dea0b379ed1e350e2848809 Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Tue, 30 Jul 2019 15:42:37 -0400 Subject: [PATCH 1/3] Allow expando additions to an imported object in JavaScript - fixes #31312 --- src/compiler/checker.ts | 6 ++++-- src/compiler/debug.ts | 2 +- .../propertyAssignmentOnImportedSymbol.errors.txt | 11 ----------- 3 files changed, 5 insertions(+), 14 deletions(-) delete mode 100644 tests/baselines/reference/propertyAssignmentOnImportedSymbol.errors.txt diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3fa9c2da66ef7..007be810e489a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -29974,7 +29974,7 @@ namespace ts { const symbol = getSymbolOfNode(node); const target = resolveAlias(symbol); if (target !== unknownSymbol) { - // For external modules symbol represent local symbol for an alias. + // For external modules symbol represents local symbol for an alias. // This local symbol will merge any other local declarations (excluding other aliases) // and symbol.flags will contains combined representation for all merged declaration. // Based on symbol.flags we can compute a set of excluded meanings (meaning that resolved alias should not have, @@ -30004,7 +30004,9 @@ namespace ts { function checkImportBinding(node: ImportEqualsDeclaration | ImportClause | NamespaceImport | ImportSpecifier) { checkCollisionWithRequireExportsInGeneratedCode(node, node.name!); checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name!); - checkAliasSymbol(node); + if (!isInJSFile(node)) { + checkAliasSymbol(node); + } } function checkImportDeclaration(node: ImportDeclaration) { diff --git a/src/compiler/debug.ts b/src/compiler/debug.ts index ee6847133c114..c90f4d1fb01f5 100644 --- a/src/compiler/debug.ts +++ b/src/compiler/debug.ts @@ -258,4 +258,4 @@ namespace ts { isDebugInfoEnabled = true; } } -} \ No newline at end of file +} diff --git a/tests/baselines/reference/propertyAssignmentOnImportedSymbol.errors.txt b/tests/baselines/reference/propertyAssignmentOnImportedSymbol.errors.txt deleted file mode 100644 index ee17bd087a118..0000000000000 --- a/tests/baselines/reference/propertyAssignmentOnImportedSymbol.errors.txt +++ /dev/null @@ -1,11 +0,0 @@ -tests/cases/conformance/salsa/bug24658.js(1,10): error TS2440: Import declaration conflicts with local declaration of 'hurk'. - - -==== tests/cases/conformance/salsa/mod1.js (0 errors) ==== - export var hurk = {} -==== tests/cases/conformance/salsa/bug24658.js (1 errors) ==== - import { hurk } from './mod1' - ~~~~ -!!! error TS2440: Import declaration conflicts with local declaration of 'hurk'. - hurk.expando = 4 - \ No newline at end of file From 8dd57061e1361bee525267c53ddc9bbca4cf79c3 Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Tue, 30 Jul 2019 15:51:53 -0400 Subject: [PATCH 2/3] Ensure that when import/export are used natively in the JS runtime that the check for alias symbol is re-applied - re comment in #26912 --- src/compiler/checker.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 007be810e489a..02b6e9913e79f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -29973,7 +29973,10 @@ namespace ts { function checkAliasSymbol(node: ImportEqualsDeclaration | ImportClause | NamespaceImport | ImportSpecifier | ExportSpecifier) { const symbol = getSymbolOfNode(node); const target = resolveAlias(symbol); - if (target !== unknownSymbol) { + + const shouldSkipWithJSRequireTargets = !isInJSFile(node) && moduleKind !== ModuleKind.ES2015; + + if (shouldSkipWithJSRequireTargets && target !== unknownSymbol) { // For external modules symbol represents local symbol for an alias. // This local symbol will merge any other local declarations (excluding other aliases) // and symbol.flags will contains combined representation for all merged declaration. @@ -30004,9 +30007,7 @@ namespace ts { function checkImportBinding(node: ImportEqualsDeclaration | ImportClause | NamespaceImport | ImportSpecifier) { checkCollisionWithRequireExportsInGeneratedCode(node, node.name!); checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name!); - if (!isInJSFile(node)) { - checkAliasSymbol(node); - } + checkAliasSymbol(node); } function checkImportDeclaration(node: ImportDeclaration) { From 44b13ee475cb9dac0a773d6db7593e759cb4156b Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Wed, 31 Jul 2019 14:34:21 -0400 Subject: [PATCH 3/3] Don't exclusively check for just JS but scope down the check to not include the expando'd objects --- src/compiler/checker.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 02b6e9913e79f..c823b066d4b69 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -29974,9 +29974,8 @@ namespace ts { const symbol = getSymbolOfNode(node); const target = resolveAlias(symbol); - const shouldSkipWithJSRequireTargets = !isInJSFile(node) && moduleKind !== ModuleKind.ES2015; - - if (shouldSkipWithJSRequireTargets && target !== unknownSymbol) { + const shouldSkipWithJSExpandoTargets = symbol.flags & SymbolFlags.Assignment; + if (!shouldSkipWithJSExpandoTargets && target !== unknownSymbol) { // For external modules symbol represents local symbol for an alias. // This local symbol will merge any other local declarations (excluding other aliases) // and symbol.flags will contains combined representation for all merged declaration.