From ab33a65d30e2d4a1a989033ab49d3d89a33a7431 Mon Sep 17 00:00:00 2001 From: Jason Freeman Date: Tue, 16 Dec 2014 14:59:21 -0800 Subject: [PATCH] Fix issue #1503 with modules and imports sharing a name --- src/compiler/checker.ts | 16 +++++++- ...SharesNameWithImportDeclarationInsideIt.js | 35 ++++++++++++++++ ...resNameWithImportDeclarationInsideIt.types | 29 ++++++++++++++ ...haresNameWithImportDeclarationInsideIt2.js | 35 ++++++++++++++++ ...esNameWithImportDeclarationInsideIt2.types | 29 ++++++++++++++ ...eWithImportDeclarationInsideIt3.errors.txt | 25 ++++++++++++ ...haresNameWithImportDeclarationInsideIt3.js | 40 +++++++++++++++++++ ...haresNameWithImportDeclarationInsideIt4.js | 36 +++++++++++++++++ ...esNameWithImportDeclarationInsideIt4.types | 32 +++++++++++++++ ...eWithImportDeclarationInsideIt5.errors.txt | 25 ++++++++++++ ...haresNameWithImportDeclarationInsideIt5.js | 39 ++++++++++++++++++ ...SharesNameWithImportDeclarationInsideIt.ts | 11 +++++ ...haresNameWithImportDeclarationInsideIt2.ts | 11 +++++ ...haresNameWithImportDeclarationInsideIt3.ts | 16 ++++++++ ...haresNameWithImportDeclarationInsideIt4.ts | 12 ++++++ ...haresNameWithImportDeclarationInsideIt5.ts | 16 ++++++++ 16 files changed, 405 insertions(+), 2 deletions(-) create mode 100644 tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt.js create mode 100644 tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt.types create mode 100644 tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt2.js create mode 100644 tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt2.types create mode 100644 tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt3.errors.txt create mode 100644 tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt3.js create mode 100644 tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt4.js create mode 100644 tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt4.types create mode 100644 tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt5.errors.txt create mode 100644 tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt5.js create mode 100644 tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt.ts create mode 100644 tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt2.ts create mode 100644 tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt3.ts create mode 100644 tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt4.ts create mode 100644 tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt5.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b0c0305670..119b60f71c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -9178,8 +9178,20 @@ module ts { function isUniqueLocalName(name: string, container: Node): boolean { for (var node = container; isNodeDescendentOf(node, container); node = node.nextContainer) { - if (node.locals && hasProperty(node.locals, name) && node.locals[name].flags & (SymbolFlags.Value | SymbolFlags.ExportValue)) { - return false; + if (node.locals && hasProperty(node.locals, name)) { + var symbolWithRelevantName = node.locals[name]; + if (symbolWithRelevantName.flags & (SymbolFlags.Value | SymbolFlags.ExportValue)) { + return false; + } + + // An import can be emitted too, if it is referenced as a value. + // Make sure the name in question does not collide with an import. + if (symbolWithRelevantName.flags & SymbolFlags.Import) { + var importDeclarationWithRelevantName = getDeclarationOfKind(symbolWithRelevantName, SyntaxKind.ImportDeclaration); + if (isReferencedImportDeclaration(importDeclarationWithRelevantName)) { + return false; + } + } } } return true; diff --git a/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt.js b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt.js new file mode 100644 index 0000000000..983dc004ac --- /dev/null +++ b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt.js @@ -0,0 +1,35 @@ +//// [moduleSharesNameWithImportDeclarationInsideIt.ts] +module Z.M { + export function bar() { + return ""; + } +} +module A.M { + import M = Z.M; + export function bar() { + } + M.bar(); // Should call Z.M.bar +} + +//// [moduleSharesNameWithImportDeclarationInsideIt.js] +var Z; +(function (Z) { + var M; + (function (M) { + function bar() { + return ""; + } + M.bar = bar; + })(M = Z.M || (Z.M = {})); +})(Z || (Z = {})); +var A; +(function (A) { + var M; + (function (_M) { + var M = Z.M; + function bar() { + } + _M.bar = bar; + M.bar(); // Should call Z.M.bar + })(M = A.M || (A.M = {})); +})(A || (A = {})); diff --git a/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt.types b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt.types new file mode 100644 index 0000000000..5a29a01cec --- /dev/null +++ b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt.types @@ -0,0 +1,29 @@ +=== tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt.ts === +module Z.M { +>Z : typeof Z +>M : typeof M + + export function bar() { +>bar : () => string + + return ""; + } +} +module A.M { +>A : typeof A +>M : typeof A.M + + import M = Z.M; +>M : typeof M +>Z : typeof Z +>M : typeof M + + export function bar() { +>bar : () => void + } + M.bar(); // Should call Z.M.bar +>M.bar() : string +>M.bar : () => string +>M : typeof M +>bar : () => string +} diff --git a/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt2.js b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt2.js new file mode 100644 index 0000000000..1366f52e7f --- /dev/null +++ b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt2.js @@ -0,0 +1,35 @@ +//// [moduleSharesNameWithImportDeclarationInsideIt2.ts] +module Z.M { + export function bar() { + return ""; + } +} +module A.M { + export import M = Z.M; + export function bar() { + } + M.bar(); // Should call Z.M.bar +} + +//// [moduleSharesNameWithImportDeclarationInsideIt2.js] +var Z; +(function (Z) { + var M; + (function (M) { + function bar() { + return ""; + } + M.bar = bar; + })(M = Z.M || (Z.M = {})); +})(Z || (Z = {})); +var A; +(function (A) { + var M; + (function (M) { + M.M = Z.M; + function bar() { + } + M.bar = bar; + M.M.bar(); // Should call Z.M.bar + })(M = A.M || (A.M = {})); +})(A || (A = {})); diff --git a/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt2.types b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt2.types new file mode 100644 index 0000000000..75aba0e2d0 --- /dev/null +++ b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt2.types @@ -0,0 +1,29 @@ +=== tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt2.ts === +module Z.M { +>Z : typeof Z +>M : typeof M + + export function bar() { +>bar : () => string + + return ""; + } +} +module A.M { +>A : typeof A +>M : typeof A.M + + export import M = Z.M; +>M : typeof M +>Z : typeof Z +>M : typeof M + + export function bar() { +>bar : () => void + } + M.bar(); // Should call Z.M.bar +>M.bar() : string +>M.bar : () => string +>M : typeof M +>bar : () => string +} diff --git a/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt3.errors.txt b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt3.errors.txt new file mode 100644 index 0000000000..f8fa117c02 --- /dev/null +++ b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt3.errors.txt @@ -0,0 +1,25 @@ +tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt3.ts(10,12): error TS2300: Duplicate identifier 'M'. +tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt3.ts(11,12): error TS2300: Duplicate identifier 'M'. + + +==== tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt3.ts (2 errors) ==== + module Z { + export module M { + export function bar() { + return ""; + } + } + export interface I { } + } + module A.M { + import M = Z.M; + ~ +!!! error TS2300: Duplicate identifier 'M'. + import M = Z.I; + ~ +!!! error TS2300: Duplicate identifier 'M'. + + export function bar() { + } + M.bar(); // Should call Z.M.bar + } \ No newline at end of file diff --git a/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt3.js b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt3.js new file mode 100644 index 0000000000..c11b36f643 --- /dev/null +++ b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt3.js @@ -0,0 +1,40 @@ +//// [moduleSharesNameWithImportDeclarationInsideIt3.ts] +module Z { + export module M { + export function bar() { + return ""; + } + } + export interface I { } +} +module A.M { + import M = Z.M; + import M = Z.I; + + export function bar() { + } + M.bar(); // Should call Z.M.bar +} + +//// [moduleSharesNameWithImportDeclarationInsideIt3.js] +var Z; +(function (Z) { + var M; + (function (M) { + function bar() { + return ""; + } + M.bar = bar; + })(M = Z.M || (Z.M = {})); +})(Z || (Z = {})); +var A; +(function (A) { + var M; + (function (_M) { + var M = Z.M; + function bar() { + } + _M.bar = bar; + M.bar(); // Should call Z.M.bar + })(M = A.M || (A.M = {})); +})(A || (A = {})); diff --git a/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt4.js b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt4.js new file mode 100644 index 0000000000..c61f3d4dd9 --- /dev/null +++ b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt4.js @@ -0,0 +1,36 @@ +//// [moduleSharesNameWithImportDeclarationInsideIt4.ts] +module Z.M { + export function bar() { + return ""; + } +} +module A.M { + interface M { } + import M = Z.M; + export function bar() { + } + M.bar(); // Should call Z.M.bar +} + +//// [moduleSharesNameWithImportDeclarationInsideIt4.js] +var Z; +(function (Z) { + var M; + (function (M) { + function bar() { + return ""; + } + M.bar = bar; + })(M = Z.M || (Z.M = {})); +})(Z || (Z = {})); +var A; +(function (A) { + var M; + (function (_M) { + var M = Z.M; + function bar() { + } + _M.bar = bar; + M.bar(); // Should call Z.M.bar + })(M = A.M || (A.M = {})); +})(A || (A = {})); diff --git a/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt4.types b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt4.types new file mode 100644 index 0000000000..b4df2e0ae0 --- /dev/null +++ b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt4.types @@ -0,0 +1,32 @@ +=== tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt4.ts === +module Z.M { +>Z : typeof Z +>M : typeof M + + export function bar() { +>bar : () => string + + return ""; + } +} +module A.M { +>A : typeof A +>M : typeof A.M + + interface M { } +>M : M + + import M = Z.M; +>M : typeof M +>Z : typeof Z +>M : typeof M + + export function bar() { +>bar : () => void + } + M.bar(); // Should call Z.M.bar +>M.bar() : string +>M.bar : () => string +>M : typeof M +>bar : () => string +} diff --git a/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt5.errors.txt b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt5.errors.txt new file mode 100644 index 0000000000..5e548809e9 --- /dev/null +++ b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt5.errors.txt @@ -0,0 +1,25 @@ +tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt5.ts(10,12): error TS2300: Duplicate identifier 'M'. +tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt5.ts(11,12): error TS2300: Duplicate identifier 'M'. + + +==== tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt5.ts (2 errors) ==== + module Z { + export module M { + export function bar() { + return ""; + } + } + export interface I { } + } + module A.M { + import M = Z.I; + ~ +!!! error TS2300: Duplicate identifier 'M'. + import M = Z.M; + ~ +!!! error TS2300: Duplicate identifier 'M'. + + export function bar() { + } + M.bar(); // Should call Z.M.bar + } \ No newline at end of file diff --git a/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt5.js b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt5.js new file mode 100644 index 0000000000..0995dcbb9c --- /dev/null +++ b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt5.js @@ -0,0 +1,39 @@ +//// [moduleSharesNameWithImportDeclarationInsideIt5.ts] +module Z { + export module M { + export function bar() { + return ""; + } + } + export interface I { } +} +module A.M { + import M = Z.I; + import M = Z.M; + + export function bar() { + } + M.bar(); // Should call Z.M.bar +} + +//// [moduleSharesNameWithImportDeclarationInsideIt5.js] +var Z; +(function (Z) { + var M; + (function (M) { + function bar() { + return ""; + } + M.bar = bar; + })(M = Z.M || (Z.M = {})); +})(Z || (Z = {})); +var A; +(function (A) { + var M; + (function (M) { + function bar() { + } + M.bar = bar; + M.bar(); // Should call Z.M.bar + })(M = A.M || (A.M = {})); +})(A || (A = {})); diff --git a/tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt.ts b/tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt.ts new file mode 100644 index 0000000000..dfb21cccbe --- /dev/null +++ b/tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt.ts @@ -0,0 +1,11 @@ +module Z.M { + export function bar() { + return ""; + } +} +module A.M { + import M = Z.M; + export function bar() { + } + M.bar(); // Should call Z.M.bar +} \ No newline at end of file diff --git a/tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt2.ts b/tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt2.ts new file mode 100644 index 0000000000..f81fe17a62 --- /dev/null +++ b/tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt2.ts @@ -0,0 +1,11 @@ +module Z.M { + export function bar() { + return ""; + } +} +module A.M { + export import M = Z.M; + export function bar() { + } + M.bar(); // Should call Z.M.bar +} \ No newline at end of file diff --git a/tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt3.ts b/tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt3.ts new file mode 100644 index 0000000000..417d6aad8f --- /dev/null +++ b/tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt3.ts @@ -0,0 +1,16 @@ +module Z { + export module M { + export function bar() { + return ""; + } + } + export interface I { } +} +module A.M { + import M = Z.M; + import M = Z.I; + + export function bar() { + } + M.bar(); // Should call Z.M.bar +} \ No newline at end of file diff --git a/tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt4.ts b/tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt4.ts new file mode 100644 index 0000000000..1c7993c596 --- /dev/null +++ b/tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt4.ts @@ -0,0 +1,12 @@ +module Z.M { + export function bar() { + return ""; + } +} +module A.M { + interface M { } + import M = Z.M; + export function bar() { + } + M.bar(); // Should call Z.M.bar +} \ No newline at end of file diff --git a/tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt5.ts b/tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt5.ts new file mode 100644 index 0000000000..fa224b1b7f --- /dev/null +++ b/tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt5.ts @@ -0,0 +1,16 @@ +module Z { + export module M { + export function bar() { + return ""; + } + } + export interface I { } +} +module A.M { + import M = Z.I; + import M = Z.M; + + export function bar() { + } + M.bar(); // Should call Z.M.bar +} \ No newline at end of file