Skip to content

Commit b3ed46f

Browse files
committed
port 5662 into release-1.7
1 parent 6e918d1 commit b3ed46f

File tree

7 files changed

+134
-4
lines changed

7 files changed

+134
-4
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11172,11 +11172,15 @@ namespace ts {
1117211172
let errorNode: Node = (<FunctionLikeDeclaration>subsequentNode).name || subsequentNode;
1117311173
// TODO(jfreeman): These are methods, so handle computed name case
1117411174
if (node.name && (<FunctionLikeDeclaration>subsequentNode).name && (<Identifier>node.name).text === (<Identifier>(<FunctionLikeDeclaration>subsequentNode).name).text) {
11175-
// the only situation when this is possible (same kind\same name but different symbol) - mixed static and instance class members
1117611175
Debug.assert(node.kind === SyntaxKind.MethodDeclaration || node.kind === SyntaxKind.MethodSignature);
11177-
Debug.assert((node.flags & NodeFlags.Static) !== (subsequentNode.flags & NodeFlags.Static));
11178-
let diagnostic = node.flags & NodeFlags.Static ? Diagnostics.Function_overload_must_be_static : Diagnostics.Function_overload_must_not_be_static;
11179-
error(errorNode, diagnostic);
11176+
// we can get here in two cases
11177+
// 1. mixed static and instance class members
11178+
// 2. something with the same name was defined before the set of overloads that prevents them from merging
11179+
// here we'll report error only for the first case since for second we should already report error in binder
11180+
if ((node.flags & NodeFlags.Static) !== (subsequentNode.flags & NodeFlags.Static)) {
11181+
const diagnostic = node.flags & NodeFlags.Static ? Diagnostics.Function_overload_must_be_static : Diagnostics.Function_overload_must_not_be_static;
11182+
error(errorNode, diagnostic);
11183+
}
1118011184
return;
1118111185
}
1118211186
else if (nodeIsPresent((<FunctionLikeDeclaration>subsequentNode).body)) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
tests/cases/compiler/mixedStaticAndInstanceClassMembers.ts(4,5): error TS2387: Function overload must be static.
2+
tests/cases/compiler/mixedStaticAndInstanceClassMembers.ts(12,12): error TS2388: Function overload must not be static.
3+
tests/cases/compiler/mixedStaticAndInstanceClassMembers.ts(13,5): error TS2387: Function overload must be static.
4+
5+
6+
==== tests/cases/compiler/mixedStaticAndInstanceClassMembers.ts (3 errors) ====
7+
class A {
8+
f() {}
9+
static m1 (a: string): void;
10+
m1 (a: number): void;
11+
~~
12+
!!! error TS2387: Function overload must be static.
13+
m1 (a: any): void {
14+
}
15+
}
16+
17+
class B {
18+
f() {}
19+
m1 (a: string): void;
20+
static m1 (a: number): void;
21+
~~
22+
!!! error TS2388: Function overload must not be static.
23+
m1 (a: any): void {
24+
~~
25+
!!! error TS2387: Function overload must be static.
26+
}
27+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//// [mixedStaticAndInstanceClassMembers.ts]
2+
class A {
3+
f() {}
4+
static m1 (a: string): void;
5+
m1 (a: number): void;
6+
m1 (a: any): void {
7+
}
8+
}
9+
10+
class B {
11+
f() {}
12+
m1 (a: string): void;
13+
static m1 (a: number): void;
14+
m1 (a: any): void {
15+
}
16+
}
17+
18+
//// [mixedStaticAndInstanceClassMembers.js]
19+
var A = (function () {
20+
function A() {
21+
}
22+
A.prototype.f = function () { };
23+
A.prototype.m1 = function (a) {
24+
};
25+
return A;
26+
})();
27+
var B = (function () {
28+
function B() {
29+
}
30+
B.prototype.f = function () { };
31+
B.prototype.m1 = function (a) {
32+
};
33+
return B;
34+
})();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
tests/cases/compiler/nonMergedDeclarationsAndOverloads.ts(2,5): error TS2300: Duplicate identifier 'm1'.
2+
tests/cases/compiler/nonMergedDeclarationsAndOverloads.ts(4,5): error TS2300: Duplicate identifier 'm1'.
3+
tests/cases/compiler/nonMergedDeclarationsAndOverloads.ts(5,5): error TS2300: Duplicate identifier 'm1'.
4+
tests/cases/compiler/nonMergedDeclarationsAndOverloads.ts(6,5): error TS2300: Duplicate identifier 'm1'.
5+
6+
7+
==== tests/cases/compiler/nonMergedDeclarationsAndOverloads.ts (4 errors) ====
8+
class A {
9+
m1: string;
10+
~~
11+
!!! error TS2300: Duplicate identifier 'm1'.
12+
f() {}
13+
m1 (a: string): void;
14+
~~
15+
!!! error TS2300: Duplicate identifier 'm1'.
16+
m1 (a: number): void;
17+
~~
18+
!!! error TS2300: Duplicate identifier 'm1'.
19+
m1 (a: any): void {
20+
~~
21+
!!! error TS2300: Duplicate identifier 'm1'.
22+
}
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [nonMergedDeclarationsAndOverloads.ts]
2+
class A {
3+
m1: string;
4+
f() {}
5+
m1 (a: string): void;
6+
m1 (a: number): void;
7+
m1 (a: any): void {
8+
}
9+
}
10+
11+
//// [nonMergedDeclarationsAndOverloads.js]
12+
var A = (function () {
13+
function A() {
14+
}
15+
A.prototype.f = function () { };
16+
A.prototype.m1 = function (a) {
17+
};
18+
return A;
19+
})();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class A {
2+
f() {}
3+
static m1 (a: string): void;
4+
m1 (a: number): void;
5+
m1 (a: any): void {
6+
}
7+
}
8+
9+
class B {
10+
f() {}
11+
m1 (a: string): void;
12+
static m1 (a: number): void;
13+
m1 (a: any): void {
14+
}
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class A {
2+
m1: string;
3+
f() {}
4+
m1 (a: string): void;
5+
m1 (a: number): void;
6+
m1 (a: any): void {
7+
}
8+
}

0 commit comments

Comments
 (0)