Skip to content

Commit de062ae

Browse files
committed
Merge pull request #1515 from Microsoft/moduleSharesNameWithImportInside
Fix issue #1503 with modules and imports sharing a name
2 parents c5b702d + 29dfa3d commit de062ae

19 files changed

+471
-2
lines changed

src/compiler/checker.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9178,8 +9178,20 @@ module ts {
91789178

91799179
function isUniqueLocalName(name: string, container: Node): boolean {
91809180
for (var node = container; isNodeDescendentOf(node, container); node = node.nextContainer) {
9181-
if (node.locals && hasProperty(node.locals, name) && node.locals[name].flags & (SymbolFlags.Value | SymbolFlags.ExportValue)) {
9182-
return false;
9181+
if (node.locals && hasProperty(node.locals, name)) {
9182+
var symbolWithRelevantName = node.locals[name];
9183+
if (symbolWithRelevantName.flags & (SymbolFlags.Value | SymbolFlags.ExportValue)) {
9184+
return false;
9185+
}
9186+
9187+
// An import can be emitted too, if it is referenced as a value.
9188+
// Make sure the name in question does not collide with an import.
9189+
if (symbolWithRelevantName.flags & SymbolFlags.Import) {
9190+
var importDeclarationWithRelevantName = <ImportDeclaration>getDeclarationOfKind(symbolWithRelevantName, SyntaxKind.ImportDeclaration);
9191+
if (isReferencedImportDeclaration(importDeclarationWithRelevantName)) {
9192+
return false;
9193+
}
9194+
}
91839195
}
91849196
}
91859197
return true;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//// [moduleSharesNameWithImportDeclarationInsideIt.ts]
2+
module Z.M {
3+
export function bar() {
4+
return "";
5+
}
6+
}
7+
module A.M {
8+
import M = Z.M;
9+
export function bar() {
10+
}
11+
M.bar(); // Should call Z.M.bar
12+
}
13+
14+
//// [moduleSharesNameWithImportDeclarationInsideIt.js]
15+
var Z;
16+
(function (Z) {
17+
var M;
18+
(function (M) {
19+
function bar() {
20+
return "";
21+
}
22+
M.bar = bar;
23+
})(M = Z.M || (Z.M = {}));
24+
})(Z || (Z = {}));
25+
var A;
26+
(function (A) {
27+
var M;
28+
(function (_M) {
29+
var M = Z.M;
30+
function bar() {
31+
}
32+
_M.bar = bar;
33+
M.bar(); // Should call Z.M.bar
34+
})(M = A.M || (A.M = {}));
35+
})(A || (A = {}));
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt.ts ===
2+
module Z.M {
3+
>Z : typeof Z
4+
>M : typeof M
5+
6+
export function bar() {
7+
>bar : () => string
8+
9+
return "";
10+
}
11+
}
12+
module A.M {
13+
>A : typeof A
14+
>M : typeof A.M
15+
16+
import M = Z.M;
17+
>M : typeof M
18+
>Z : typeof Z
19+
>M : typeof M
20+
21+
export function bar() {
22+
>bar : () => void
23+
}
24+
M.bar(); // Should call Z.M.bar
25+
>M.bar() : string
26+
>M.bar : () => string
27+
>M : typeof M
28+
>bar : () => string
29+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//// [moduleSharesNameWithImportDeclarationInsideIt2.ts]
2+
module Z.M {
3+
export function bar() {
4+
return "";
5+
}
6+
}
7+
module A.M {
8+
export import M = Z.M;
9+
export function bar() {
10+
}
11+
M.bar(); // Should call Z.M.bar
12+
}
13+
14+
//// [moduleSharesNameWithImportDeclarationInsideIt2.js]
15+
var Z;
16+
(function (Z) {
17+
var M;
18+
(function (M) {
19+
function bar() {
20+
return "";
21+
}
22+
M.bar = bar;
23+
})(M = Z.M || (Z.M = {}));
24+
})(Z || (Z = {}));
25+
var A;
26+
(function (A) {
27+
var M;
28+
(function (M) {
29+
M.M = Z.M;
30+
function bar() {
31+
}
32+
M.bar = bar;
33+
M.M.bar(); // Should call Z.M.bar
34+
})(M = A.M || (A.M = {}));
35+
})(A || (A = {}));
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt2.ts ===
2+
module Z.M {
3+
>Z : typeof Z
4+
>M : typeof M
5+
6+
export function bar() {
7+
>bar : () => string
8+
9+
return "";
10+
}
11+
}
12+
module A.M {
13+
>A : typeof A
14+
>M : typeof A.M
15+
16+
export import M = Z.M;
17+
>M : typeof M
18+
>Z : typeof Z
19+
>M : typeof M
20+
21+
export function bar() {
22+
>bar : () => void
23+
}
24+
M.bar(); // Should call Z.M.bar
25+
>M.bar() : string
26+
>M.bar : () => string
27+
>M : typeof M
28+
>bar : () => string
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt3.ts(10,12): error TS2300: Duplicate identifier 'M'.
2+
tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt3.ts(11,12): error TS2300: Duplicate identifier 'M'.
3+
4+
5+
==== tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt3.ts (2 errors) ====
6+
module Z {
7+
export module M {
8+
export function bar() {
9+
return "";
10+
}
11+
}
12+
export interface I { }
13+
}
14+
module A.M {
15+
import M = Z.M;
16+
~
17+
!!! error TS2300: Duplicate identifier 'M'.
18+
import M = Z.I;
19+
~
20+
!!! error TS2300: Duplicate identifier 'M'.
21+
22+
export function bar() {
23+
}
24+
M.bar(); // Should call Z.M.bar
25+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//// [moduleSharesNameWithImportDeclarationInsideIt3.ts]
2+
module Z {
3+
export module M {
4+
export function bar() {
5+
return "";
6+
}
7+
}
8+
export interface I { }
9+
}
10+
module A.M {
11+
import M = Z.M;
12+
import M = Z.I;
13+
14+
export function bar() {
15+
}
16+
M.bar(); // Should call Z.M.bar
17+
}
18+
19+
//// [moduleSharesNameWithImportDeclarationInsideIt3.js]
20+
var Z;
21+
(function (Z) {
22+
var M;
23+
(function (M) {
24+
function bar() {
25+
return "";
26+
}
27+
M.bar = bar;
28+
})(M = Z.M || (Z.M = {}));
29+
})(Z || (Z = {}));
30+
var A;
31+
(function (A) {
32+
var M;
33+
(function (_M) {
34+
var M = Z.M;
35+
function bar() {
36+
}
37+
_M.bar = bar;
38+
M.bar(); // Should call Z.M.bar
39+
})(M = A.M || (A.M = {}));
40+
})(A || (A = {}));
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// [moduleSharesNameWithImportDeclarationInsideIt4.ts]
2+
module Z.M {
3+
export function bar() {
4+
return "";
5+
}
6+
}
7+
module A.M {
8+
interface M { }
9+
import M = Z.M;
10+
export function bar() {
11+
}
12+
M.bar(); // Should call Z.M.bar
13+
}
14+
15+
//// [moduleSharesNameWithImportDeclarationInsideIt4.js]
16+
var Z;
17+
(function (Z) {
18+
var M;
19+
(function (M) {
20+
function bar() {
21+
return "";
22+
}
23+
M.bar = bar;
24+
})(M = Z.M || (Z.M = {}));
25+
})(Z || (Z = {}));
26+
var A;
27+
(function (A) {
28+
var M;
29+
(function (_M) {
30+
var M = Z.M;
31+
function bar() {
32+
}
33+
_M.bar = bar;
34+
M.bar(); // Should call Z.M.bar
35+
})(M = A.M || (A.M = {}));
36+
})(A || (A = {}));
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=== tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt4.ts ===
2+
module Z.M {
3+
>Z : typeof Z
4+
>M : typeof M
5+
6+
export function bar() {
7+
>bar : () => string
8+
9+
return "";
10+
}
11+
}
12+
module A.M {
13+
>A : typeof A
14+
>M : typeof A.M
15+
16+
interface M { }
17+
>M : M
18+
19+
import M = Z.M;
20+
>M : typeof M
21+
>Z : typeof Z
22+
>M : typeof M
23+
24+
export function bar() {
25+
>bar : () => void
26+
}
27+
M.bar(); // Should call Z.M.bar
28+
>M.bar() : string
29+
>M.bar : () => string
30+
>M : typeof M
31+
>bar : () => string
32+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt5.ts(10,12): error TS2300: Duplicate identifier 'M'.
2+
tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt5.ts(11,12): error TS2300: Duplicate identifier 'M'.
3+
4+
5+
==== tests/cases/compiler/moduleSharesNameWithImportDeclarationInsideIt5.ts (2 errors) ====
6+
module Z {
7+
export module M {
8+
export function bar() {
9+
return "";
10+
}
11+
}
12+
export interface I { }
13+
}
14+
module A.M {
15+
import M = Z.I;
16+
~
17+
!!! error TS2300: Duplicate identifier 'M'.
18+
import M = Z.M;
19+
~
20+
!!! error TS2300: Duplicate identifier 'M'.
21+
22+
export function bar() {
23+
}
24+
M.bar(); // Should call Z.M.bar
25+
}

0 commit comments

Comments
 (0)