From b0475aff3920d748fa74b5a6d8a7ad5dd731aec4 Mon Sep 17 00:00:00 2001 From: Armano Date: Sun, 28 Feb 2021 20:08:46 +0100 Subject: [PATCH] fix(eslint-plugin): [consistent-indexed-object-style] do not autofix if interface has extends (#3009) --- .../rules/consistent-indexed-object-style.ts | 21 ++++++++++++------- .../consistent-indexed-object-style.test.ts | 15 ++++++++++++- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/packages/eslint-plugin/src/rules/consistent-indexed-object-style.ts b/packages/eslint-plugin/src/rules/consistent-indexed-object-style.ts index bd48718c68b5..a30102dd123e 100644 --- a/packages/eslint-plugin/src/rules/consistent-indexed-object-style.ts +++ b/packages/eslint-plugin/src/rules/consistent-indexed-object-style.ts @@ -2,6 +2,7 @@ import { createRule } from '../util'; import { AST_NODE_TYPES, TSESTree, + TSESLint, } from '@typescript-eslint/experimental-utils'; type MessageIds = 'preferRecord' | 'preferIndexSignature'; @@ -66,6 +67,7 @@ export default createRule({ node: TSESTree.Node, prefix: string, postfix: string, + safeFix = true, ): void { if (members.length !== 1) { return; @@ -98,14 +100,16 @@ export default createRule({ context.report({ node, messageId: 'preferRecord', - fix(fixer) { - const key = sourceCode.getText(keyType.typeAnnotation); - const value = sourceCode.getText(valueType.typeAnnotation); - const record = member.readonly - ? `Readonly>` - : `Record<${key}, ${value}>`; - return fixer.replaceText(node, `${prefix}${record}${postfix}`); - }, + fix: safeFix + ? (fixer): TSESLint.RuleFix => { + const key = sourceCode.getText(keyType.typeAnnotation); + const value = sourceCode.getText(valueType.typeAnnotation); + const record = member.readonly + ? `Readonly>` + : `Record<${key}, ${value}>`; + return fixer.replaceText(node, `${prefix}${record}${postfix}`); + } + : null, }); } @@ -128,6 +132,7 @@ export default createRule({ node, `type ${node.id.name}${genericTypes} = `, ';', + !node.extends?.length, ); }, }; diff --git a/packages/eslint-plugin/tests/rules/consistent-indexed-object-style.test.ts b/packages/eslint-plugin/tests/rules/consistent-indexed-object-style.test.ts index 31077fcf6206..eb7e5cc8d1f6 100644 --- a/packages/eslint-plugin/tests/rules/consistent-indexed-object-style.test.ts +++ b/packages/eslint-plugin/tests/rules/consistent-indexed-object-style.test.ts @@ -76,7 +76,6 @@ interface Foo { []; } `, - // 'index-signature' // Unhandled type { @@ -166,6 +165,20 @@ type Foo = Record; errors: [{ messageId: 'preferRecord', line: 2, column: 1 }], }, + // Interface with extends + { + code: ` +interface B extends A { + [index: number]: unknown; +} + `, + output: ` +interface B extends A { + [index: number]: unknown; +} + `, + errors: [{ messageId: 'preferRecord', line: 2, column: 1 }], + }, // Readonly interface with generic parameter { code: `