diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b0c0305670eae..119b60f71cfa1 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 0000000000000..983dc004acf31 --- /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 0000000000000..5a29a01cec75e --- /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 0000000000000..1366f52e7f8b4 --- /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 0000000000000..75aba0e2d0e75 --- /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 0000000000000..f8fa117c0241e --- /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 0000000000000..c11b36f643a30 --- /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 0000000000000..c61f3d4dd9d73 --- /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 0000000000000..b4df2e0ae080f --- /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 0000000000000..5e548809e9c35 --- /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 0000000000000..0995dcbb9c0b6 --- /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/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt6.js b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt6.js new file mode 100644 index 0000000000000..51b2d4f89f959 --- /dev/null +++ b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt6.js @@ -0,0 +1,32 @@ +//// [moduleSharesNameWithImportDeclarationInsideIt6.ts] +module Z.M { + export function bar() { + return ""; + } +} +module A.M { + import M = Z.M; + export function bar() { + } +} + +//// [moduleSharesNameWithImportDeclarationInsideIt6.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 = A.M || (A.M = {})); +})(A || (A = {})); diff --git a/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt6.types b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt6.types new file mode 100644 index 0000000000000..01879d2db117f --- /dev/null +++ b/tests/baselines/reference/moduleSharesNameWithImportDeclarationInsideIt6.types @@ -0,0 +1,24 @@ +=== tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt6.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 + } +} diff --git a/tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt.ts b/tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt.ts new file mode 100644 index 0000000000000..dfb21cccbe724 --- /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 0000000000000..f81fe17a624b8 --- /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 0000000000000..417d6aad8fd82 --- /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 0000000000000..1c7993c596ea9 --- /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 0000000000000..fa224b1b7f978 --- /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 diff --git a/tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt6.ts b/tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt6.ts new file mode 100644 index 0000000000000..f488a5de070ab --- /dev/null +++ b/tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt6.ts @@ -0,0 +1,10 @@ +module Z.M { + export function bar() { + return ""; + } +} +module A.M { + import M = Z.M; + export function bar() { + } +} \ No newline at end of file