Skip to content

Commit 30fafb0

Browse files
authored
fix(eslint-plugin): [adjacent-overload-signatures] fix false positive on call signatures and a method named call (typescript-eslint#2313)
1 parent e75067a commit 30fafb0

File tree

2 files changed

+59
-21
lines changed

2 files changed

+59
-21
lines changed

packages/eslint-plugin/src/rules/adjacent-overload-signatures.ts

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,24 @@ export default util.createRule({
3030
create(context) {
3131
const sourceCode = context.getSourceCode();
3232

33+
interface Method {
34+
name: string;
35+
static: boolean;
36+
callSignature: boolean;
37+
}
38+
3339
/**
34-
* Gets the name of the member being processed.
40+
* Gets the name and attribute of the member being processed.
3541
* @param member the member being processed.
36-
* @returns the name of the member or null if it's a member not relevant to the rule.
42+
* @returns the name and attribute of the member or null if it's a member not relevant to the rule.
3743
*/
38-
function getMemberName(member: TSESTree.Node): string | null {
44+
function getMemberMethod(member: TSESTree.Node): Method | null {
3945
if (!member) {
4046
return null;
4147
}
4248

49+
const isStatic = 'static' in member && !!member.static;
50+
4351
switch (member.type) {
4452
case AST_NODE_TYPES.ExportDefaultDeclaration:
4553
case AST_NODE_TYPES.ExportNamedDeclaration: {
@@ -49,33 +57,55 @@ export default util.createRule({
4957
return null;
5058
}
5159

52-
return getMemberName(member.declaration);
60+
return getMemberMethod(member.declaration);
5361
}
5462
case AST_NODE_TYPES.TSDeclareFunction:
55-
case AST_NODE_TYPES.FunctionDeclaration:
56-
return member.id?.name ?? null;
63+
case AST_NODE_TYPES.FunctionDeclaration: {
64+
const name = member.id?.name ?? null;
65+
if (name === null) {
66+
return null;
67+
}
68+
return {
69+
name,
70+
static: isStatic,
71+
callSignature: false,
72+
};
73+
}
5774
case AST_NODE_TYPES.TSMethodSignature:
58-
return util.getNameFromMember(member, sourceCode);
75+
return {
76+
name: util.getNameFromMember(member, sourceCode),
77+
static: isStatic,
78+
callSignature: false,
79+
};
5980
case AST_NODE_TYPES.TSCallSignatureDeclaration:
60-
return 'call';
81+
return {
82+
name: 'call',
83+
static: isStatic,
84+
callSignature: true,
85+
};
6186
case AST_NODE_TYPES.TSConstructSignatureDeclaration:
62-
return 'new';
87+
return {
88+
name: 'new',
89+
static: isStatic,
90+
callSignature: false,
91+
};
6392
case AST_NODE_TYPES.MethodDefinition:
64-
return util.getNameFromMember(member, sourceCode);
93+
return {
94+
name: util.getNameFromMember(member, sourceCode),
95+
static: isStatic,
96+
callSignature: false,
97+
};
6598
}
6699

67100
return null;
68101
}
69102

70-
interface Method {
71-
name: string;
72-
static: boolean;
73-
}
74103
function isSameMethod(method1: Method, method2: Method | null): boolean {
75104
return (
76105
!!method2 &&
77106
method1.name === method2.name &&
78-
method1.static === method2.static
107+
method1.static === method2.static &&
108+
method1.callSignature === method2.callSignature
79109
);
80110
}
81111

@@ -104,15 +134,11 @@ export default util.createRule({
104134
const seenMethods: Method[] = [];
105135

106136
members.forEach(member => {
107-
const name = getMemberName(member);
108-
if (name === null) {
137+
const method = getMemberMethod(member);
138+
if (method === null) {
109139
lastMethod = null;
110140
return;
111141
}
112-
const method = {
113-
name,
114-
static: 'static' in member && !!member.static,
115-
};
116142

117143
const index = seenMethods.findIndex(seenMethod =>
118144
isSameMethod(method, seenMethod),

packages/eslint-plugin/tests/rules/adjacent-overload-signatures.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ interface Foo {
131131
}
132132
`,
133133
`
134+
interface Foo {
135+
(s: string): void;
136+
(n: number): void;
137+
(sn: string | number): void;
138+
foo(n: number): void;
139+
bar(): void;
140+
baz(): void;
141+
call(): void;
142+
}
143+
`,
144+
`
134145
interface Foo {
135146
foo(s: string): void;
136147
foo(n: number): void;
@@ -534,6 +545,7 @@ interface Foo {
534545
(sn: string | number): void;
535546
bar(): void;
536547
baz(): void;
548+
call(): void;
537549
}
538550
`,
539551
errors: [

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy