Skip to content

Commit d370ac8

Browse files
committed
Merge pull request #5664 from Microsoft/Port-5662
port 5662 into release-1.7
2 parents 6e918d1 + a7fb3e4 commit d370ac8

10 files changed

+174
-5
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11172,11 +11172,17 @@ 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
11176-
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);
11175+
const reportError =
11176+
(node.kind === SyntaxKind.MethodDeclaration || node.kind === SyntaxKind.MethodSignature) &&
11177+
(node.flags & NodeFlags.Static) !== (subsequentNode.flags & NodeFlags.Static);
11178+
// we can get here in two cases
11179+
// 1. mixed static and instance class members
11180+
// 2. something with the same name was defined before the set of overloads that prevents them from merging
11181+
// here we'll report error only for the first case since for second we should already report error in binder
11182+
if (reportError) {
11183+
const diagnostic = node.flags & NodeFlags.Static ? Diagnostics.Function_overload_must_be_static : Diagnostics.Function_overload_must_not_be_static;
11184+
error(errorNode, diagnostic);
11185+
}
1118011186
return;
1118111187
}
1118211188
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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
tests/cases/compiler/nonMergedOverloads.ts(1,5): error TS2300: Duplicate identifier 'f'.
2+
tests/cases/compiler/nonMergedOverloads.ts(3,17): error TS1148: Cannot compile modules unless the '--module' flag is provided.
3+
tests/cases/compiler/nonMergedOverloads.ts(3,17): error TS2300: Duplicate identifier 'f'.
4+
tests/cases/compiler/nonMergedOverloads.ts(4,17): error TS2300: Duplicate identifier 'f'.
5+
6+
7+
==== tests/cases/compiler/nonMergedOverloads.ts (4 errors) ====
8+
var f = 10;
9+
~
10+
!!! error TS2300: Duplicate identifier 'f'.
11+
12+
export function f();
13+
~
14+
!!! error TS1148: Cannot compile modules unless the '--module' flag is provided.
15+
~
16+
!!! error TS2300: Duplicate identifier 'f'.
17+
export function f() {
18+
~
19+
!!! error TS2300: Duplicate identifier 'f'.
20+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [nonMergedOverloads.ts]
2+
var f = 10;
3+
4+
export function f();
5+
export function f() {
6+
}
7+
8+
//// [nonMergedOverloads.js]
9+
var f = 10;
10+
function f() {
11+
}
12+
exports.f = f;
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+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var f = 10;
2+
3+
export function f();
4+
export function f() {
5+
}

0 commit comments

Comments
 (0)