Skip to content

Commit 0a9fb1a

Browse files
committed
Merge pull request microsoft#3946 from Microsoft/disambiguating
Fix resolution when block-scoped variable names collide with those of other entities
2 parents bbadd70 + 9d1f051 commit 0a9fb1a

21 files changed

Lines changed: 230 additions & 1 deletion

src/compiler/checker.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,19 @@ namespace ts {
592592
declarationNameToString(propertyName), typeof nameArg === "string" ? nameArg : declarationNameToString(nameArg));
593593
return undefined;
594594
}
595-
if (result.flags & SymbolFlags.BlockScopedVariable) {
595+
596+
// Only check for block-scoped variable if we are looking for the
597+
// name with variable meaning
598+
// For example,
599+
// declare module foo {
600+
// interface bar {}
601+
// }
602+
// let foo/*1*/: foo/*2*/.bar;
603+
// The foo at /*1*/ and /*2*/ will share same symbol with two meaning
604+
// block - scope variable and namespace module. However, only when we
605+
// try to resolve name in /*1*/ which is used in variable position,
606+
// we want to check for block- scoped
607+
if (meaning & SymbolFlags.BlockScopedVariable && result.flags & SymbolFlags.BlockScopedVariable) {
596608
checkResolvedBlockScopedVariable(result, errorLocation);
597609
}
598610
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [resolveInterfaceNameWithSameLetDeclarationName1.ts]
2+
interface bar { }
3+
let bar: bar;
4+
5+
6+
//// [resolveInterfaceNameWithSameLetDeclarationName1.js]
7+
var bar;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/resolveInterfaceNameWithSameLetDeclarationName1.ts ===
2+
interface bar { }
3+
>bar : Symbol(bar, Decl(resolveInterfaceNameWithSameLetDeclarationName1.ts, 0, 0), Decl(resolveInterfaceNameWithSameLetDeclarationName1.ts, 1, 3))
4+
5+
let bar: bar;
6+
>bar : Symbol(bar, Decl(resolveInterfaceNameWithSameLetDeclarationName1.ts, 0, 0), Decl(resolveInterfaceNameWithSameLetDeclarationName1.ts, 1, 3))
7+
>bar : Symbol(bar, Decl(resolveInterfaceNameWithSameLetDeclarationName1.ts, 0, 0), Decl(resolveInterfaceNameWithSameLetDeclarationName1.ts, 1, 3))
8+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/resolveInterfaceNameWithSameLetDeclarationName1.ts ===
2+
interface bar { }
3+
>bar : bar
4+
5+
let bar: bar;
6+
>bar : bar
7+
>bar : bar
8+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [resolveInterfaceNameWithSameLetDeclarationName2.ts]
2+
interface foo { }
3+
interface bar { }
4+
let bar: bar | foo;
5+
let foo: bar | foo;
6+
7+
//// [resolveInterfaceNameWithSameLetDeclarationName2.js]
8+
var bar;
9+
var foo;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/resolveInterfaceNameWithSameLetDeclarationName2.ts ===
2+
interface foo { }
3+
>foo : Symbol(foo, Decl(resolveInterfaceNameWithSameLetDeclarationName2.ts, 0, 0), Decl(resolveInterfaceNameWithSameLetDeclarationName2.ts, 3, 3))
4+
5+
interface bar { }
6+
>bar : Symbol(bar, Decl(resolveInterfaceNameWithSameLetDeclarationName2.ts, 0, 17), Decl(resolveInterfaceNameWithSameLetDeclarationName2.ts, 2, 3))
7+
8+
let bar: bar | foo;
9+
>bar : Symbol(bar, Decl(resolveInterfaceNameWithSameLetDeclarationName2.ts, 0, 17), Decl(resolveInterfaceNameWithSameLetDeclarationName2.ts, 2, 3))
10+
>bar : Symbol(bar, Decl(resolveInterfaceNameWithSameLetDeclarationName2.ts, 0, 17), Decl(resolveInterfaceNameWithSameLetDeclarationName2.ts, 2, 3))
11+
>foo : Symbol(foo, Decl(resolveInterfaceNameWithSameLetDeclarationName2.ts, 0, 0), Decl(resolveInterfaceNameWithSameLetDeclarationName2.ts, 3, 3))
12+
13+
let foo: bar | foo;
14+
>foo : Symbol(foo, Decl(resolveInterfaceNameWithSameLetDeclarationName2.ts, 0, 0), Decl(resolveInterfaceNameWithSameLetDeclarationName2.ts, 3, 3))
15+
>bar : Symbol(bar, Decl(resolveInterfaceNameWithSameLetDeclarationName2.ts, 0, 17), Decl(resolveInterfaceNameWithSameLetDeclarationName2.ts, 2, 3))
16+
>foo : Symbol(foo, Decl(resolveInterfaceNameWithSameLetDeclarationName2.ts, 0, 0), Decl(resolveInterfaceNameWithSameLetDeclarationName2.ts, 3, 3))
17+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/resolveInterfaceNameWithSameLetDeclarationName2.ts ===
2+
interface foo { }
3+
>foo : foo
4+
5+
interface bar { }
6+
>bar : bar
7+
8+
let bar: bar | foo;
9+
>bar : bar | foo
10+
>bar : bar
11+
>foo : foo
12+
13+
let foo: bar | foo;
14+
>foo : bar | foo
15+
>bar : bar
16+
>foo : foo
17+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [resolveModuleNameWithSameLetDeclarationName1.ts]
2+
declare module foo {
3+
4+
interface Bar {
5+
6+
}
7+
}
8+
9+
let foo: foo.Bar;
10+
11+
//// [resolveModuleNameWithSameLetDeclarationName1.js]
12+
var foo;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/resolveModuleNameWithSameLetDeclarationName1.ts ===
2+
declare module foo {
3+
>foo : Symbol(foo, Decl(resolveModuleNameWithSameLetDeclarationName1.ts, 0, 0), Decl(resolveModuleNameWithSameLetDeclarationName1.ts, 7, 3))
4+
5+
interface Bar {
6+
>Bar : Symbol(Bar, Decl(resolveModuleNameWithSameLetDeclarationName1.ts, 0, 20))
7+
8+
}
9+
}
10+
11+
let foo: foo.Bar;
12+
>foo : Symbol(foo, Decl(resolveModuleNameWithSameLetDeclarationName1.ts, 0, 0), Decl(resolveModuleNameWithSameLetDeclarationName1.ts, 7, 3))
13+
>foo : Symbol(foo, Decl(resolveModuleNameWithSameLetDeclarationName1.ts, 0, 0), Decl(resolveModuleNameWithSameLetDeclarationName1.ts, 7, 3))
14+
>Bar : Symbol(foo.Bar, Decl(resolveModuleNameWithSameLetDeclarationName1.ts, 0, 20))
15+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/resolveModuleNameWithSameLetDeclarationName1.ts ===
2+
declare module foo {
3+
>foo : Bar
4+
5+
interface Bar {
6+
>Bar : Bar
7+
8+
}
9+
}
10+
11+
let foo: foo.Bar;
12+
>foo : foo.Bar
13+
>foo : any
14+
>Bar : foo.Bar
15+

0 commit comments

Comments
 (0)