Skip to content

Commit

Permalink
do not crash when variable and function declarations collide
Browse files Browse the repository at this point in the history
  • Loading branch information
vladima committed Nov 14, 2015
1 parent b3ed46f commit a7fb3e4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11172,12 +11172,14 @@ 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) {
Debug.assert(node.kind === SyntaxKind.MethodDeclaration || node.kind === SyntaxKind.MethodSignature);
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 ((node.flags & NodeFlags.Static) !== (subsequentNode.flags & NodeFlags.Static)) {
if (reportError) {
const diagnostic = node.flags & NodeFlags.Static ? Diagnostics.Function_overload_must_be_static : Diagnostics.Function_overload_must_not_be_static;
error(errorNode, diagnostic);
}
Expand Down
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;
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 a7fb3e4

Please sign in to comment.