Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 29428a4

Browse files
authored
fix(eslint-plugin): [consistent-indexed-object-style] convert readonly index signature to readonly record (typescript-eslint#2798)
1 parent 73a63ee commit 29428a4

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ export default createRule<Options, MessageIds>({
101101
fix(fixer) {
102102
const key = sourceCode.getText(keyType.typeAnnotation);
103103
const value = sourceCode.getText(valueType.typeAnnotation);
104-
return fixer.replaceText(
105-
node,
106-
`${prefix}Record<${key}, ${value}>${postfix}`,
107-
);
104+
const record = member.readonly
105+
? `Readonly<Record<${key}, ${value}>>`
106+
: `Record<${key}, ${value}>`;
107+
return fixer.replaceText(node, `${prefix}${record}${postfix}`);
108108
},
109109
});
110110
}

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

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

143+
// Readonly interface
144+
{
145+
code: `
146+
interface Foo {
147+
readonly [key: string]: any;
148+
}
149+
`,
150+
output: `
151+
type Foo = Readonly<Record<string, any>>;
152+
`,
153+
errors: [{ messageId: 'preferRecord', line: 2, column: 1 }],
154+
},
155+
143156
// Interface with generic parameter
144157
{
145158
code: `
@@ -153,6 +166,19 @@ type Foo<A> = Record<string, A>;
153166
errors: [{ messageId: 'preferRecord', line: 2, column: 1 }],
154167
},
155168

169+
// Readonly interface with generic parameter
170+
{
171+
code: `
172+
interface Foo<A> {
173+
readonly [key: string]: A;
174+
}
175+
`,
176+
output: `
177+
type Foo<A> = Readonly<Record<string, A>>;
178+
`,
179+
errors: [{ messageId: 'preferRecord', line: 2, column: 1 }],
180+
},
181+
156182
// Interface with multiple generic parameters
157183
{
158184
code: `
@@ -166,20 +192,47 @@ type Foo<A, B> = Record<A, B>;
166192
errors: [{ messageId: 'preferRecord', line: 2, column: 1 }],
167193
},
168194

195+
// Readonly interface with multiple generic parameters
196+
{
197+
code: `
198+
interface Foo<A, B> {
199+
readonly [key: A]: B;
200+
}
201+
`,
202+
output: `
203+
type Foo<A, B> = Readonly<Record<A, B>>;
204+
`,
205+
errors: [{ messageId: 'preferRecord', line: 2, column: 1 }],
206+
},
207+
169208
// Type literal
170209
{
171210
code: 'type Foo = { [key: string]: any };',
172211
output: 'type Foo = Record<string, any>;',
173212
errors: [{ messageId: 'preferRecord', line: 1, column: 12 }],
174213
},
175214

215+
// Readonly type literal
216+
{
217+
code: 'type Foo = { readonly [key: string]: any };',
218+
output: 'type Foo = Readonly<Record<string, any>>;',
219+
errors: [{ messageId: 'preferRecord', line: 1, column: 12 }],
220+
},
221+
176222
// Generic
177223
{
178224
code: 'type Foo = Generic<{ [key: string]: any }>;',
179225
output: 'type Foo = Generic<Record<string, any>>;',
180226
errors: [{ messageId: 'preferRecord', line: 1, column: 20 }],
181227
},
182228

229+
// Readonly Generic
230+
{
231+
code: 'type Foo = Generic<{ readonly [key: string]: any }>;',
232+
output: 'type Foo = Generic<Readonly<Record<string, any>>>;',
233+
errors: [{ messageId: 'preferRecord', line: 1, column: 20 }],
234+
},
235+
183236
// Function types
184237
{
185238
code: 'function foo(arg: { [key: string]: any }) {}',
@@ -192,6 +245,18 @@ type Foo<A, B> = Record<A, B>;
192245
errors: [{ messageId: 'preferRecord', line: 1, column: 17 }],
193246
},
194247

248+
// Readonly function types
249+
{
250+
code: 'function foo(arg: { readonly [key: string]: any }) {}',
251+
output: 'function foo(arg: Readonly<Record<string, any>>) {}',
252+
errors: [{ messageId: 'preferRecord', line: 1, column: 19 }],
253+
},
254+
{
255+
code: 'function foo(): { readonly [key: string]: any } {}',
256+
output: 'function foo(): Readonly<Record<string, any>> {}',
257+
errors: [{ messageId: 'preferRecord', line: 1, column: 17 }],
258+
},
259+
195260
// Never
196261
// Type literal
197262
{

0 commit comments

Comments
 (0)