Skip to content

Commit

Permalink
fix(eslint-plugin): [method-signature-style] correct fixer for overlo…
Browse files Browse the repository at this point in the history
…ads in an object literal type (#2708)
  • Loading branch information
a-tarasyuk committed Oct 25, 2020
1 parent 66e9c6e commit 0763913
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
21 changes: 13 additions & 8 deletions packages/eslint-plugin/src/rules/method-signature-style.ts
Expand Up @@ -118,15 +118,20 @@ export default util.createRule<Options, MessageIds>({
return; return;
} }


const duplicatedKeyMethodNodes: TSESTree.TSMethodSignature[] = const parent = methodNode.parent;
methodNode.parent?.type === AST_NODE_TYPES.TSInterfaceBody const members =
? methodNode.parent.body.filter( parent?.type === AST_NODE_TYPES.TSInterfaceBody
(element): element is TSESTree.TSMethodSignature => ? parent.body
element.type === AST_NODE_TYPES.TSMethodSignature && : parent?.type === AST_NODE_TYPES.TSTypeLiteral
element !== methodNode && ? parent.members
getMethodKey(element) === getMethodKey(methodNode),
)
: []; : [];

const duplicatedKeyMethodNodes: TSESTree.TSMethodSignature[] = members.filter(
(element): element is TSESTree.TSMethodSignature =>
element.type === AST_NODE_TYPES.TSMethodSignature &&
element !== methodNode &&
getMethodKey(element) === getMethodKey(methodNode),
);
const isParentModule = isNodeParentModuleDeclaration(methodNode); const isParentModule = isNodeParentModuleDeclaration(methodNode);


if (duplicatedKeyMethodNodes.length > 0) { if (duplicatedKeyMethodNodes.length > 0) {
Expand Down
56 changes: 56 additions & 0 deletions packages/eslint-plugin/tests/rules/method-signature-style.test.ts
Expand Up @@ -371,5 +371,61 @@ interface Foo {
}, },
], ],
}, },
{
code: noFormat`
type Foo = {
foo(): one;
foo(): two;
foo(): three;
}
`,
output: noFormat`
type Foo = {
foo: (() => one) & (() => two) & (() => three);
}
`,
errors: [
{
messageId: 'errorMethod',
line: 3,
},
{
messageId: 'errorMethod',
line: 4,
},
{
messageId: 'errorMethod',
line: 5,
},
],
},
{
code: noFormat`
declare const Foo: {
foo(): one;
foo(): two;
foo(): three;
}
`,
output: noFormat`
declare const Foo: {
foo: (() => one) & (() => two) & (() => three);
}
`,
errors: [
{
messageId: 'errorMethod',
line: 3,
},
{
messageId: 'errorMethod',
line: 4,
},
{
messageId: 'errorMethod',
line: 5,
},
],
},
], ],
}); });

0 comments on commit 0763913

Please sign in to comment.