Skip to content

Commit

Permalink
fix(eslint-plugin): fix false positive from adjacent-overload-signatu…
Browse files Browse the repository at this point in the history
…res (#206)
  • Loading branch information
lukyth authored and JamesHenry committed Feb 7, 2019
1 parent 7cd3bc2 commit 07e950e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
35 changes: 25 additions & 10 deletions packages/eslint-plugin/lib/rules/adjacent-overload-signatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ module.exports = {
}
}

/**
* Determine whether two methods are the same or not
* @param {{ name: string; static: boolean }} method1 a method to compare
* @param {{ name: string; static: boolean }} method2 another method to compare with
* @returns {boolean} true if two methods are the same
* @private
*/
function isSameMethod(method1, method2) {
return method1.name === method2.name && method1.static === method2.static;
}

/**
* Check the body for overload methods.
* @param {ASTNode} node the body to be inspected.
Expand All @@ -83,28 +94,32 @@ module.exports = {
const members = node.body || node.members;

if (members) {
let name;
let index;
let lastName;
const seen = [];
let lastMethod;
const seenMethods = [];

members.forEach(member => {
name = getMemberName(member);
const name = getMemberName(member);
const method = {
name,
static: member.static
};

index = seen.indexOf(name);
if (index > -1 && lastName !== name) {
const index = seenMethods.findIndex(seenMethod =>
isSameMethod(method, seenMethod)
);
if (index > -1 && !isSameMethod(method, lastMethod)) {
context.report({
node: member,
messageId: 'adjacentSignature',
data: {
name
name: (method.static ? 'static ' : '') + method.name
}
});
} else if (name && index === -1) {
seen.push(name);
seenMethods.push(method);
}

lastName = name;
lastMethod = method;
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,23 @@ class Foo {
foo(sn: string | number): void {}
bar(): void {}
baz(): void {}
}
`,
`
class Foo {
name: string;
static foo(s: string): void;
static foo(n: number): void;
static foo(sn: string | number): void {}
bar(): void {}
baz(): void {}
}
`,
`
class Test {
static test() {}
untest() {}
test() {}
}
`,
// examples from https://github.com/nzakas/eslint-plugin-typescript/issues/138
Expand Down Expand Up @@ -789,6 +806,26 @@ class Foo {
column: 5
}
]
},
{
code: `
class Foo {
static foo(s: string): void;
name: string;
static foo(n: number): void;
static foo(sn: string | number): void {}
bar(): void {}
baz(): void {}
}
`,
errors: [
{
messageId: 'adjacentSignature',
data: { name: 'static foo' },
line: 5,
column: 5
}
]
}
]
});

0 comments on commit 07e950e

Please sign in to comment.