Skip to content

Commit

Permalink
Merge pull request #5664 from Microsoft/Port-5662
Browse files Browse the repository at this point in the history
port 5662 into release-1.7
  • Loading branch information
vladima committed Nov 16, 2015
2 parents 6e918d1 + a7fb3e4 commit d370ac8
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11172,11 +11172,17 @@ namespace ts {
let errorNode: Node = (<FunctionLikeDeclaration>subsequentNode).name || subsequentNode;
// TODO(jfreeman): These are methods, so handle computed name case
if (node.name && (<FunctionLikeDeclaration>subsequentNode).name && (<Identifier>node.name).text === (<Identifier>(<FunctionLikeDeclaration>subsequentNode).name).text) {
// the only situation when this is possible (same kind\same name but different symbol) - mixed static and instance class members
Debug.assert(node.kind === SyntaxKind.MethodDeclaration || node.kind === SyntaxKind.MethodSignature);
Debug.assert((node.flags & NodeFlags.Static) !== (subsequentNode.flags & NodeFlags.Static));
let diagnostic = node.flags & NodeFlags.Static ? Diagnostics.Function_overload_must_be_static : Diagnostics.Function_overload_must_not_be_static;
error(errorNode, diagnostic);
const reportError =
(node.kind === SyntaxKind.MethodDeclaration || node.kind === SyntaxKind.MethodSignature) &&
(node.flags & NodeFlags.Static) !== (subsequentNode.flags & NodeFlags.Static);
// we can get here in two cases
// 1. mixed static and instance class members
// 2. something with the same name was defined before the set of overloads that prevents them from merging
// here we'll report error only for the first case since for second we should already report error in binder
if (reportError) {
const diagnostic = node.flags & NodeFlags.Static ? Diagnostics.Function_overload_must_be_static : Diagnostics.Function_overload_must_not_be_static;
error(errorNode, diagnostic);
}
return;
}
else if (nodeIsPresent((<FunctionLikeDeclaration>subsequentNode).body)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
tests/cases/compiler/mixedStaticAndInstanceClassMembers.ts(4,5): error TS2387: Function overload must be static.
tests/cases/compiler/mixedStaticAndInstanceClassMembers.ts(12,12): error TS2388: Function overload must not be static.
tests/cases/compiler/mixedStaticAndInstanceClassMembers.ts(13,5): error TS2387: Function overload must be static.


==== tests/cases/compiler/mixedStaticAndInstanceClassMembers.ts (3 errors) ====
class A {
f() {}
static m1 (a: string): void;
m1 (a: number): void;
~~
!!! error TS2387: Function overload must be static.
m1 (a: any): void {
}
}

class B {
f() {}
m1 (a: string): void;
static m1 (a: number): void;
~~
!!! error TS2388: Function overload must not be static.
m1 (a: any): void {
~~
!!! error TS2387: Function overload must be static.
}
}
34 changes: 34 additions & 0 deletions tests/baselines/reference/mixedStaticAndInstanceClassMembers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//// [mixedStaticAndInstanceClassMembers.ts]
class A {
f() {}
static m1 (a: string): void;
m1 (a: number): void;
m1 (a: any): void {
}
}

class B {
f() {}
m1 (a: string): void;
static m1 (a: number): void;
m1 (a: any): void {
}
}

//// [mixedStaticAndInstanceClassMembers.js]
var A = (function () {
function A() {
}
A.prototype.f = function () { };
A.prototype.m1 = function (a) {
};
return A;
})();
var B = (function () {
function B() {
}
B.prototype.f = function () { };
B.prototype.m1 = function (a) {
};
return B;
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
tests/cases/compiler/nonMergedDeclarationsAndOverloads.ts(2,5): error TS2300: Duplicate identifier 'm1'.
tests/cases/compiler/nonMergedDeclarationsAndOverloads.ts(4,5): error TS2300: Duplicate identifier 'm1'.
tests/cases/compiler/nonMergedDeclarationsAndOverloads.ts(5,5): error TS2300: Duplicate identifier 'm1'.
tests/cases/compiler/nonMergedDeclarationsAndOverloads.ts(6,5): error TS2300: Duplicate identifier 'm1'.


==== tests/cases/compiler/nonMergedDeclarationsAndOverloads.ts (4 errors) ====
class A {
m1: string;
~~
!!! error TS2300: Duplicate identifier 'm1'.
f() {}
m1 (a: string): void;
~~
!!! error TS2300: Duplicate identifier 'm1'.
m1 (a: number): void;
~~
!!! error TS2300: Duplicate identifier 'm1'.
m1 (a: any): void {
~~
!!! error TS2300: Duplicate identifier 'm1'.
}
}
19 changes: 19 additions & 0 deletions tests/baselines/reference/nonMergedDeclarationsAndOverloads.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [nonMergedDeclarationsAndOverloads.ts]
class A {
m1: string;
f() {}
m1 (a: string): void;
m1 (a: number): void;
m1 (a: any): void {
}
}

//// [nonMergedDeclarationsAndOverloads.js]
var A = (function () {
function A() {
}
A.prototype.f = function () { };
A.prototype.m1 = function (a) {
};
return A;
})();
20 changes: 20 additions & 0 deletions tests/baselines/reference/nonMergedOverloads.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
tests/cases/compiler/nonMergedOverloads.ts(1,5): error TS2300: Duplicate identifier 'f'.
tests/cases/compiler/nonMergedOverloads.ts(3,17): error TS1148: Cannot compile modules unless the '--module' flag is provided.
tests/cases/compiler/nonMergedOverloads.ts(3,17): error TS2300: Duplicate identifier 'f'.
tests/cases/compiler/nonMergedOverloads.ts(4,17): error TS2300: Duplicate identifier 'f'.


==== tests/cases/compiler/nonMergedOverloads.ts (4 errors) ====
var f = 10;
~
!!! error TS2300: Duplicate identifier 'f'.

export function f();
~
!!! error TS1148: Cannot compile modules unless the '--module' flag is provided.
~
!!! error TS2300: Duplicate identifier 'f'.
export function f() {
~
!!! error TS2300: Duplicate identifier 'f'.
}
12 changes: 12 additions & 0 deletions tests/baselines/reference/nonMergedOverloads.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//// [nonMergedOverloads.ts]
var f = 10;

export function f();
export function f() {
}

//// [nonMergedOverloads.js]
var f = 10;
function f() {
}
exports.f = f;
15 changes: 15 additions & 0 deletions tests/cases/compiler/mixedStaticAndInstanceClassMembers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class A {
f() {}
static m1 (a: string): void;
m1 (a: number): void;
m1 (a: any): void {
}
}

class B {
f() {}
m1 (a: string): void;
static m1 (a: number): void;
m1 (a: any): void {
}
}
8 changes: 8 additions & 0 deletions tests/cases/compiler/nonMergedDeclarationsAndOverloads.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class A {
m1: string;
f() {}
m1 (a: string): void;
m1 (a: number): void;
m1 (a: any): void {
}
}
5 changes: 5 additions & 0 deletions tests/cases/compiler/nonMergedOverloads.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var f = 10;

export function f();
export function f() {
}

0 comments on commit d370ac8

Please sign in to comment.