Skip to content

Commit 0763913

Browse files
authored
fix(eslint-plugin): [method-signature-style] correct fixer for overloads in an object literal type (typescript-eslint#2708)
1 parent 66e9c6e commit 0763913

File tree

2 files changed

+69
-8
lines changed

2 files changed

+69
-8
lines changed

packages/eslint-plugin/src/rules/method-signature-style.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,20 @@ export default util.createRule<Options, MessageIds>({
118118
return;
119119
}
120120

121-
const duplicatedKeyMethodNodes: TSESTree.TSMethodSignature[] =
122-
methodNode.parent?.type === AST_NODE_TYPES.TSInterfaceBody
123-
? methodNode.parent.body.filter(
124-
(element): element is TSESTree.TSMethodSignature =>
125-
element.type === AST_NODE_TYPES.TSMethodSignature &&
126-
element !== methodNode &&
127-
getMethodKey(element) === getMethodKey(methodNode),
128-
)
121+
const parent = methodNode.parent;
122+
const members =
123+
parent?.type === AST_NODE_TYPES.TSInterfaceBody
124+
? parent.body
125+
: parent?.type === AST_NODE_TYPES.TSTypeLiteral
126+
? parent.members
129127
: [];
128+
129+
const duplicatedKeyMethodNodes: TSESTree.TSMethodSignature[] = members.filter(
130+
(element): element is TSESTree.TSMethodSignature =>
131+
element.type === AST_NODE_TYPES.TSMethodSignature &&
132+
element !== methodNode &&
133+
getMethodKey(element) === getMethodKey(methodNode),
134+
);
130135
const isParentModule = isNodeParentModuleDeclaration(methodNode);
131136

132137
if (duplicatedKeyMethodNodes.length > 0) {

packages/eslint-plugin/tests/rules/method-signature-style.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,5 +371,61 @@ interface Foo {
371371
},
372372
],
373373
},
374+
{
375+
code: noFormat`
376+
type Foo = {
377+
foo(): one;
378+
foo(): two;
379+
foo(): three;
380+
}
381+
`,
382+
output: noFormat`
383+
type Foo = {
384+
foo: (() => one) & (() => two) & (() => three);
385+
}
386+
`,
387+
errors: [
388+
{
389+
messageId: 'errorMethod',
390+
line: 3,
391+
},
392+
{
393+
messageId: 'errorMethod',
394+
line: 4,
395+
},
396+
{
397+
messageId: 'errorMethod',
398+
line: 5,
399+
},
400+
],
401+
},
402+
{
403+
code: noFormat`
404+
declare const Foo: {
405+
foo(): one;
406+
foo(): two;
407+
foo(): three;
408+
}
409+
`,
410+
output: noFormat`
411+
declare const Foo: {
412+
foo: (() => one) & (() => two) & (() => three);
413+
}
414+
`,
415+
errors: [
416+
{
417+
messageId: 'errorMethod',
418+
line: 3,
419+
},
420+
{
421+
messageId: 'errorMethod',
422+
line: 4,
423+
},
424+
{
425+
messageId: 'errorMethod',
426+
line: 5,
427+
},
428+
],
429+
},
374430
],
375431
});

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