Skip to content

Commit 07e950e

Browse files
lukythJamesHenry
authored andcommitted
fix(eslint-plugin): fix false positive from adjacent-overload-signatures (#206)
1 parent 7cd3bc2 commit 07e950e

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

Diff for: packages/eslint-plugin/lib/rules/adjacent-overload-signatures.js

+25-10
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ module.exports = {
7373
}
7474
}
7575

76+
/**
77+
* Determine whether two methods are the same or not
78+
* @param {{ name: string; static: boolean }} method1 a method to compare
79+
* @param {{ name: string; static: boolean }} method2 another method to compare with
80+
* @returns {boolean} true if two methods are the same
81+
* @private
82+
*/
83+
function isSameMethod(method1, method2) {
84+
return method1.name === method2.name && method1.static === method2.static;
85+
}
86+
7687
/**
7788
* Check the body for overload methods.
7889
* @param {ASTNode} node the body to be inspected.
@@ -83,28 +94,32 @@ module.exports = {
8394
const members = node.body || node.members;
8495

8596
if (members) {
86-
let name;
87-
let index;
88-
let lastName;
89-
const seen = [];
97+
let lastMethod;
98+
const seenMethods = [];
9099

91100
members.forEach(member => {
92-
name = getMemberName(member);
101+
const name = getMemberName(member);
102+
const method = {
103+
name,
104+
static: member.static
105+
};
93106

94-
index = seen.indexOf(name);
95-
if (index > -1 && lastName !== name) {
107+
const index = seenMethods.findIndex(seenMethod =>
108+
isSameMethod(method, seenMethod)
109+
);
110+
if (index > -1 && !isSameMethod(method, lastMethod)) {
96111
context.report({
97112
node: member,
98113
messageId: 'adjacentSignature',
99114
data: {
100-
name
115+
name: (method.static ? 'static ' : '') + method.name
101116
}
102117
});
103118
} else if (name && index === -1) {
104-
seen.push(name);
119+
seenMethods.push(method);
105120
}
106121

107-
lastName = name;
122+
lastMethod = method;
108123
});
109124
}
110125
}

Diff for: packages/eslint-plugin/tests/lib/rules/adjacent-overload-signatures.js

+37
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,23 @@ class Foo {
211211
foo(sn: string | number): void {}
212212
bar(): void {}
213213
baz(): void {}
214+
}
215+
`,
216+
`
217+
class Foo {
218+
name: string;
219+
static foo(s: string): void;
220+
static foo(n: number): void;
221+
static foo(sn: string | number): void {}
222+
bar(): void {}
223+
baz(): void {}
224+
}
225+
`,
226+
`
227+
class Test {
228+
static test() {}
229+
untest() {}
230+
test() {}
214231
}
215232
`,
216233
// examples from https://github.com/nzakas/eslint-plugin-typescript/issues/138
@@ -789,6 +806,26 @@ class Foo {
789806
column: 5
790807
}
791808
]
809+
},
810+
{
811+
code: `
812+
class Foo {
813+
static foo(s: string): void;
814+
name: string;
815+
static foo(n: number): void;
816+
static foo(sn: string | number): void {}
817+
bar(): void {}
818+
baz(): void {}
819+
}
820+
`,
821+
errors: [
822+
{
823+
messageId: 'adjacentSignature',
824+
data: { name: 'static foo' },
825+
line: 5,
826+
column: 5
827+
}
828+
]
792829
}
793830
]
794831
});

0 commit comments

Comments
 (0)