From 0763913c4a0d1061465ae3329704f1a7de4b9326 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Sun, 25 Oct 2020 23:58:27 +0200 Subject: [PATCH] fix(eslint-plugin): [method-signature-style] correct fixer for overloads in an object literal type (#2708) --- .../src/rules/method-signature-style.ts | 21 ++++--- .../rules/method-signature-style.test.ts | 56 +++++++++++++++++++ 2 files changed, 69 insertions(+), 8 deletions(-) diff --git a/packages/eslint-plugin/src/rules/method-signature-style.ts b/packages/eslint-plugin/src/rules/method-signature-style.ts index 40c504cd677..de6f366b673 100644 --- a/packages/eslint-plugin/src/rules/method-signature-style.ts +++ b/packages/eslint-plugin/src/rules/method-signature-style.ts @@ -118,15 +118,20 @@ export default util.createRule({ return; } - const duplicatedKeyMethodNodes: TSESTree.TSMethodSignature[] = - methodNode.parent?.type === AST_NODE_TYPES.TSInterfaceBody - ? methodNode.parent.body.filter( - (element): element is TSESTree.TSMethodSignature => - element.type === AST_NODE_TYPES.TSMethodSignature && - element !== methodNode && - getMethodKey(element) === getMethodKey(methodNode), - ) + const parent = methodNode.parent; + const members = + parent?.type === AST_NODE_TYPES.TSInterfaceBody + ? parent.body + : parent?.type === AST_NODE_TYPES.TSTypeLiteral + ? parent.members : []; + + 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); if (duplicatedKeyMethodNodes.length > 0) { diff --git a/packages/eslint-plugin/tests/rules/method-signature-style.test.ts b/packages/eslint-plugin/tests/rules/method-signature-style.test.ts index 72c55e16c4a..e322c63c6a3 100644 --- a/packages/eslint-plugin/tests/rules/method-signature-style.test.ts +++ b/packages/eslint-plugin/tests/rules/method-signature-style.test.ts @@ -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, + }, + ], + }, ], });