Skip to content

Commit b0475af

Browse files
authored
fix(eslint-plugin): [consistent-indexed-object-style] do not autofix if interface has extends (typescript-eslint#3009)
1 parent 25f459c commit b0475af

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

packages/eslint-plugin/src/rules/consistent-indexed-object-style.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createRule } from '../util';
22
import {
33
AST_NODE_TYPES,
44
TSESTree,
5+
TSESLint,
56
} from '@typescript-eslint/experimental-utils';
67

78
type MessageIds = 'preferRecord' | 'preferIndexSignature';
@@ -66,6 +67,7 @@ export default createRule<Options, MessageIds>({
6667
node: TSESTree.Node,
6768
prefix: string,
6869
postfix: string,
70+
safeFix = true,
6971
): void {
7072
if (members.length !== 1) {
7173
return;
@@ -98,14 +100,16 @@ export default createRule<Options, MessageIds>({
98100
context.report({
99101
node,
100102
messageId: 'preferRecord',
101-
fix(fixer) {
102-
const key = sourceCode.getText(keyType.typeAnnotation);
103-
const value = sourceCode.getText(valueType.typeAnnotation);
104-
const record = member.readonly
105-
? `Readonly<Record<${key}, ${value}>>`
106-
: `Record<${key}, ${value}>`;
107-
return fixer.replaceText(node, `${prefix}${record}${postfix}`);
108-
},
103+
fix: safeFix
104+
? (fixer): TSESLint.RuleFix => {
105+
const key = sourceCode.getText(keyType.typeAnnotation);
106+
const value = sourceCode.getText(valueType.typeAnnotation);
107+
const record = member.readonly
108+
? `Readonly<Record<${key}, ${value}>>`
109+
: `Record<${key}, ${value}>`;
110+
return fixer.replaceText(node, `${prefix}${record}${postfix}`);
111+
}
112+
: null,
109113
});
110114
}
111115

@@ -128,6 +132,7 @@ export default createRule<Options, MessageIds>({
128132
node,
129133
`type ${node.id.name}${genericTypes} = `,
130134
';',
135+
!node.extends?.length,
131136
);
132137
},
133138
};

packages/eslint-plugin/tests/rules/consistent-indexed-object-style.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ interface Foo {
7676
[];
7777
}
7878
`,
79-
8079
// 'index-signature'
8180
// Unhandled type
8281
{
@@ -166,6 +165,20 @@ type Foo<A> = Record<string, A>;
166165
errors: [{ messageId: 'preferRecord', line: 2, column: 1 }],
167166
},
168167

168+
// Interface with extends
169+
{
170+
code: `
171+
interface B extends A {
172+
[index: number]: unknown;
173+
}
174+
`,
175+
output: `
176+
interface B extends A {
177+
[index: number]: unknown;
178+
}
179+
`,
180+
errors: [{ messageId: 'preferRecord', line: 2, column: 1 }],
181+
},
169182
// Readonly interface with generic parameter
170183
{
171184
code: `

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