10000 fix(eslint-plugin): [consistent-indexed-object-style] fix wrong autof… · dopecodez/typescript-eslint@73d9713 · GitHub
[go: up one dir, main page]

Skip to content

Commit 73d9713

Browse files
authored
fix(eslint-plugin): [consistent-indexed-object-style] fix wrong autofix behaviour with generics (typescript-eslint#2722)
1 parent d8c67a5 commit 73d9713

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,20 @@ export default createRule<Options, MessageIds>({
115115
},
116116

117117
TSInterfaceDeclaration(node): void {
118-
checkMembers(node.body.body, node, `type ${node.id.name} = `, ';');
118+
let genericTypes = '';
119+
120+
if ((node.typeParameters?.params ?? []).length > 0) {
121+
genericTypes = `<${node.typeParameters?.params
122+
.map(p => p.name.name)
123+
.join(', ')}>`;
124+
}
125+
126+
checkMembers(
127+
node.body.body,
128+
node,
129+
`type ${node.id.name}${genericTypes} = `,
130+
';',
131+
);
119132
},
120133
};
121134
},

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,32 @@ type Foo = Record<string, any>;
140140
errors: [{ messageId: 'preferRecord', line: 2, column: 1 }],
141141
},
142142

143+
// Interface with generic parameter
144+
{
145+
code: `
146+
interface Foo<A> {
147+
[key: string]: A;
148+
}
149+
`,
150+
output: `
151+
type Foo<A> = Record<string, A>;
152+
`,
153+
errors: [{ messageId: 'preferRecord', line: 2, column: 1 }],
154+
},
155+
156+
// Interface with multiple generic parameters
157+
{
158+
code: `
159+
interface Foo<A, B> {
160+
[key: A]: B;
161+
}
162+
`,
163+
output: `
164+
type Foo<A, B> = Record<A, B>;
165+
`,
166+
errors: [{ messageId: 'preferRecord', line: 2, column: 1 }],
167+
},
168+
143169
// Type literal
144170
{
145171
code: 'type Foo = { [key: string]: any };',
@@ -175,6 +201,14 @@ type Foo = Record<string, any>;
175201
errors: [{ messageId: 'preferIndexSignature', line: 1, column: 12 }],
176202
},
177203

204+
// Type literal with generic parameter
205+
{
206+
code: 'type Foo<T> = Record<string, T>;',
207+
options: ['index-signature'],
208+
output: 'type Foo<T> = { [key: string]: T };',
209+
errors: [{ messageId: 'preferIndexSignature', line: 1, column: 15 }],
210+
},
211+
178212
// Generic
179213
{
180214
code: 'type Foo = Generic<Record<string, any>>;',

0 commit comments

Comments
 (0)
0