From 88882d3fd9524bbcb5bc1f1e18cfa376cfe6b9c7 Mon Sep 17 00:00:00 2001 From: Andy Date: Thu, 12 Jan 2017 21:17:38 -0800 Subject: [PATCH] unified-signatures: Don't count a function/method declaration as an overload if it has a body. (#2017) --- src/rules/unifiedSignaturesRule.ts | 5 ++++- test/rules/unified-signatures/test.ts.lint | 20 ++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/rules/unifiedSignaturesRule.ts b/src/rules/unifiedSignaturesRule.ts index 68cf3937cca..5d8c8a36ede 100644 --- a/src/rules/unifiedSignaturesRule.ts +++ b/src/rules/unifiedSignaturesRule.ts @@ -80,6 +80,9 @@ class Walker extends Lint.RuleWalker { this.checkOverloads(statements, (statement) => { if (statement.kind === ts.SyntaxKind.FunctionDeclaration) { const fn = statement as ts.FunctionDeclaration; + if (fn.body) { + return undefined; + } return fn.name && { signature: fn, key: fn.name.text }; } else { return undefined; @@ -90,7 +93,7 @@ class Walker extends Lint.RuleWalker { private checkMembers(members: Array, typeParameters?: ts.TypeParameterDeclaration[]) { this.checkOverloads(members, getOverloadName, typeParameters); function getOverloadName(member: ts.TypeElement | ts.ClassElement) { - if (!isSignatureDeclaration(member)) { + if (!isSignatureDeclaration(member) || (member as ts.MethodDeclaration).body) { return undefined; } const key = getOverloadKey(member); diff --git a/test/rules/unified-signatures/test.ts.lint b/test/rules/unified-signatures/test.ts.lint index bf711b4f207..7bebd293aa6 100644 --- a/test/rules/unified-signatures/test.ts.lint +++ b/test/rules/unified-signatures/test.ts.lint @@ -1,6 +1,22 @@ // Works in non-declaration file too. -function f(x: number): number; -function f(x: string): string; +function f(x: number): void; +function f(x: string): void; + ~~~~~~~~~ [These overloads can be combined into one signature taking `number | string`.] function f(x: any): any { return x; } + +// Body does *not* count as a signature. +function g(): void; +function g(a: number, b: number): void; +function g(a?: number, b?: number): void {} + +class C { + constructor(); + constructor(a: number, b: number); + constructor(a?: number, b?: number) {} + + a(): void; + a(a: number, b: number): void; + a(a?: number, b?: number): void {} +}