Skip to content

Commit abb7a13

Browse files
committed
Merge pull request microsoft#5670 from Microsoft/noCrashWithNonMergedOverloads
do not crash when variable and function declarations collide
2 parents 9857e7f + e49e552 commit abb7a13

4 files changed

Lines changed: 41 additions & 2 deletions

File tree

src/compiler/checker.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11360,12 +11360,14 @@ namespace ts {
1136011360
const errorNode: Node = (<FunctionLikeDeclaration>subsequentNode).name || subsequentNode;
1136111361
// TODO(jfreeman): These are methods, so handle computed name case
1136211362
if (node.name && (<FunctionLikeDeclaration>subsequentNode).name && (<Identifier>node.name).text === (<Identifier>(<FunctionLikeDeclaration>subsequentNode).name).text) {
11363-
Debug.assert(node.kind === SyntaxKind.MethodDeclaration || node.kind === SyntaxKind.MethodSignature);
11363+
const reportError =
11364+
(node.kind === SyntaxKind.MethodDeclaration || node.kind === SyntaxKind.MethodSignature) &&
11365+
(node.flags & NodeFlags.Static) !== (subsequentNode.flags & NodeFlags.Static);
1136411366
// we can get here in two cases
1136511367
// 1. mixed static and instance class members
1136611368
// 2. something with the same name was defined before the set of overloads that prevents them from merging
1136711369
// here we'll report error only for the first case since for second we should already report error in binder
11368-
if ((node.flags & NodeFlags.Static) !== (subsequentNode.flags & NodeFlags.Static)) {
11370+
if (reportError) {
1136911371
const diagnostic = node.flags & NodeFlags.Static ? Diagnostics.Function_overload_must_be_static : Diagnostics.Function_overload_must_not_be_static;
1137011372
error(errorNode, diagnostic);
1137111373
}
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: 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)