Skip to content

Commit 9e8031c

Browse files
committed
Non-namespace merges override valueDeclaration
Instead of searching `declarations` for a class declaration, make the binder and checker merge `valueDeclaration` such that non-namespace merges always have their `valueDeclaration` win.
1 parent 440d01f commit 9e8031c

File tree

6 files changed

+106
-21
lines changed

6 files changed

+106
-21
lines changed

src/compiler/binder.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ namespace ts {
130130
symbol.members = {};
131131
}
132132

133-
if (symbolFlags & SymbolFlags.Value && !symbol.valueDeclaration) {
133+
if (symbolFlags & SymbolFlags.Value &&
134+
(!symbol.valueDeclaration || symbol.valueDeclaration.kind === SyntaxKind.ModuleDeclaration)) {
135+
// other kinds of value declarations take precedence over modules
134136
symbol.valueDeclaration = node;
135137
}
136138
}

src/compiler/checker.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,11 @@ namespace ts {
293293
target.constEnumOnlyModule = false;
294294
}
295295
target.flags |= source.flags;
296-
if (!target.valueDeclaration && source.valueDeclaration) target.valueDeclaration = source.valueDeclaration;
296+
if (source.valueDeclaration &&
297+
(!target.valueDeclaration || target.valueDeclaration.kind === SyntaxKind.ModuleDeclaration)) {
298+
// other kinds of value declarations take precedence over modules
299+
target.valueDeclaration = source.valueDeclaration;
300+
}
297301
forEach(source.declarations, node => {
298302
target.declarations.push(node);
299303
});
@@ -2766,9 +2770,7 @@ namespace ts {
27662770
}
27672771

27682772
function getBaseTypeNodeOfClass(type: InterfaceType): ExpressionWithTypeArguments {
2769-
let classDeclaration =
2770-
getDeclarationOfKind(type.symbol, SyntaxKind.ClassDeclaration) || type.symbol.valueDeclaration;
2771-
return getClassExtendsHeritageClauseElement(classDeclaration as ClassLikeDeclaration);
2773+
return getClassExtendsHeritageClauseElement(<ClassLikeDeclaration>type.symbol.valueDeclaration);
27722774
}
27732775

27742776
function getConstructorsForTypeArguments(type: ObjectType, typeArgumentNodes: TypeNode[]): Signature[] {

tests/baselines/reference/ambientClassDeclarationWithExtends.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
//// [ambientClassDeclarationWithExtends.ts]
1+
//// [tests/cases/compiler/ambientClassDeclarationWithExtends.ts] ////
2+
3+
//// [ambientClassDeclarationExtends_singleFile.ts]
24
declare class A { }
35
declare class B extends A { }
46

@@ -10,10 +12,29 @@ declare class D extends C { }
1012

1113
var d: C = new D();
1214

15+
//// [ambientClassDeclarationExtends_file1.ts]
16+
17+
declare class E {
18+
public bar;
19+
}
20+
namespace F { var y; }
21+
22+
//// [ambientClassDeclarationExtends_file2.ts]
23+
24+
declare class F extends E { }
25+
var f: E = new F();
26+
1327

14-
//// [ambientClassDeclarationWithExtends.js]
28+
//// [ambientClassDeclarationExtends_singleFile.js]
1529
var D;
1630
(function (D) {
1731
var x;
1832
})(D || (D = {}));
1933
var d = new D();
34+
//// [ambientClassDeclarationExtends_file1.js]
35+
var F;
36+
(function (F) {
37+
var y;
38+
})(F || (F = {}));
39+
//// [ambientClassDeclarationExtends_file2.js]
40+
var f = new F();
Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,50 @@
1-
=== tests/cases/compiler/ambientClassDeclarationWithExtends.ts ===
1+
=== tests/cases/compiler/ambientClassDeclarationExtends_singleFile.ts ===
22
declare class A { }
3-
>A : Symbol(A, Decl(ambientClassDeclarationWithExtends.ts, 0, 0))
3+
>A : Symbol(A, Decl(ambientClassDeclarationExtends_singleFile.ts, 0, 0))
44

55
declare class B extends A { }
6-
>B : Symbol(B, Decl(ambientClassDeclarationWithExtends.ts, 0, 19))
7-
>A : Symbol(A, Decl(ambientClassDeclarationWithExtends.ts, 0, 0))
6+
>B : Symbol(B, Decl(ambientClassDeclarationExtends_singleFile.ts, 0, 19))
7+
>A : Symbol(A, Decl(ambientClassDeclarationExtends_singleFile.ts, 0, 0))
88

99
declare class C {
10-
>C : Symbol(C, Decl(ambientClassDeclarationWithExtends.ts, 1, 29))
10+
>C : Symbol(C, Decl(ambientClassDeclarationExtends_singleFile.ts, 1, 29))
1111

1212
public foo;
13-
>foo : Symbol(foo, Decl(ambientClassDeclarationWithExtends.ts, 3, 17))
13+
>foo : Symbol(foo, Decl(ambientClassDeclarationExtends_singleFile.ts, 3, 17))
1414
}
1515
namespace D { var x; }
16-
>D : Symbol(D, Decl(ambientClassDeclarationWithExtends.ts, 5, 1), Decl(ambientClassDeclarationWithExtends.ts, 6, 22))
17-
>x : Symbol(x, Decl(ambientClassDeclarationWithExtends.ts, 6, 17))
16+
>D : Symbol(D, Decl(ambientClassDeclarationExtends_singleFile.ts, 5, 1), Decl(ambientClassDeclarationExtends_singleFile.ts, 6, 22))
17+
>x : Symbol(x, Decl(ambientClassDeclarationExtends_singleFile.ts, 6, 17))
1818

1919
declare class D extends C { }
20-
>D : Symbol(D, Decl(ambientClassDeclarationWithExtends.ts, 5, 1), Decl(ambientClassDeclarationWithExtends.ts, 6, 22))
21-
>C : Symbol(C, Decl(ambientClassDeclarationWithExtends.ts, 1, 29))
20+
>D : Symbol(D, Decl(ambientClassDeclarationExtends_singleFile.ts, 5, 1), Decl(ambientClassDeclarationExtends_singleFile.ts, 6, 22))
21+
>C : Symbol(C, Decl(ambientClassDeclarationExtends_singleFile.ts, 1, 29))
2222

2323
var d: C = new D();
24-
>d : Symbol(d, Decl(ambientClassDeclarationWithExtends.ts, 9, 3))
25-
>C : Symbol(C, Decl(ambientClassDeclarationWithExtends.ts, 1, 29))
26-
>D : Symbol(D, Decl(ambientClassDeclarationWithExtends.ts, 5, 1), Decl(ambientClassDeclarationWithExtends.ts, 6, 22))
24+
>d : Symbol(d, Decl(ambientClassDeclarationExtends_singleFile.ts, 9, 3))
25+
>C : Symbol(C, Decl(ambientClassDeclarationExtends_singleFile.ts, 1, 29))
26+
>D : Symbol(D, Decl(ambientClassDeclarationExtends_singleFile.ts, 5, 1), Decl(ambientClassDeclarationExtends_singleFile.ts, 6, 22))
27+
28+
=== tests/cases/compiler/ambientClassDeclarationExtends_file1.ts ===
29+
30+
declare class E {
31+
>E : Symbol(E, Decl(ambientClassDeclarationExtends_file1.ts, 0, 0))
32+
33+
public bar;
34+
>bar : Symbol(bar, Decl(ambientClassDeclarationExtends_file1.ts, 1, 17))
35+
}
36+
namespace F { var y; }
37+
>F : Symbol(F, Decl(ambientClassDeclarationExtends_file1.ts, 3, 1), Decl(ambientClassDeclarationExtends_file2.ts, 0, 0))
38+
>y : Symbol(y, Decl(ambientClassDeclarationExtends_file1.ts, 4, 17))
39+
40+
=== tests/cases/compiler/ambientClassDeclarationExtends_file2.ts ===
41+
42+
declare class F extends E { }
43+
>F : Symbol(F, Decl(ambientClassDeclarationExtends_file1.ts, 3, 1), Decl(ambientClassDeclarationExtends_file2.ts, 0, 0))
44+
>E : Symbol(E, Decl(ambientClassDeclarationExtends_file1.ts, 0, 0))
45+
46+
var f: E = new F();
47+
>f : Symbol(f, Decl(ambientClassDeclarationExtends_file2.ts, 2, 3))
48+
>E : Symbol(E, Decl(ambientClassDeclarationExtends_file1.ts, 0, 0))
49+
>F : Symbol(F, Decl(ambientClassDeclarationExtends_file1.ts, 3, 1), Decl(ambientClassDeclarationExtends_file2.ts, 0, 0))
2750

tests/baselines/reference/ambientClassDeclarationWithExtends.types

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
=== tests/cases/compiler/ambientClassDeclarationWithExtends.ts ===
1+
=== tests/cases/compiler/ambientClassDeclarationExtends_singleFile.ts ===
22
declare class A { }
33
>A : A
44

@@ -26,3 +26,27 @@ var d: C = new D();
2626
>new D() : D
2727
>D : typeof D
2828

29+
=== tests/cases/compiler/ambientClassDeclarationExtends_file1.ts ===
30+
31+
declare class E {
32+
>E : E
33+
34+
public bar;
35+
>bar : any
36+
}
37+
namespace F { var y; }
38+
>F : typeof F
39+
>y : any
40+
41+
=== tests/cases/compiler/ambientClassDeclarationExtends_file2.ts ===
42+
43+
declare class F extends E { }
44+
>F : F
45+
>E : E
46+
47+
var f: E = new F();
48+
>f : E
49+
>E : E
50+
>new F() : F
51+
>F : typeof F
52+

tests/cases/compiler/ambientClassDeclarationWithExtends.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @Filename: ambientClassDeclarationExtends_singleFile.ts
12
declare class A { }
23
declare class B extends A { }
34

@@ -8,3 +9,15 @@ namespace D { var x; }
89
declare class D extends C { }
910

1011
var d: C = new D();
12+
13+
// @Filename: ambientClassDeclarationExtends_file1.ts
14+
15+
declare class E {
16+
public bar;
17+
}
18+
namespace F { var y; }
19+
20+
// @Filename: ambientClassDeclarationExtends_file2.ts
21+
22+
declare class F extends E { }
23+
var f: E = new F();

0 commit comments

Comments
 (0)