Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
unified-signatures: Don't count a function/method declaration as an o…
Browse files Browse the repository at this point in the history
…verload if it has a body. (#2017)
  • Loading branch information
Andy authored and nchen63 committed Jan 13, 2017
1 parent cab6265 commit 88882d3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/rules/unifiedSignaturesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -90,7 +93,7 @@ class Walker extends Lint.RuleWalker {
private checkMembers(members: Array<ts.TypeElement | ts.ClassElement>, 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);
Expand Down
20 changes: 18 additions & 2 deletions test/rules/unified-signatures/test.ts.lint
Original file line number Diff line number Diff line change
@@ -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 {}
}

0 comments on commit 88882d3

Please sign in to comment.