Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [consistent-indexed-object-style] convert readonly index signature to readonly record #2798

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -101,10 +101,10 @@ export default createRule<Options, MessageIds>({
fix(fixer) {
const key = sourceCode.getText(keyType.typeAnnotation);
const value = sourceCode.getText(valueType.typeAnnotation);
return fixer.replaceText(
node,
`${prefix}Record<${key}, ${value}>${postfix}`,
);
const record = member.readonly
? `Readonly<Record<${key}, ${value}>>`
: `Record<${key}, ${value}>`;
return fixer.replaceText(node, `${prefix}${record}${postfix}`);
},
});
}
Expand Down
Expand Up @@ -140,6 +140,19 @@ type Foo = Record<string, any>;
errors: [{ messageId: 'preferRecord', line: 2, column: 1 }],
},

// Readonly interface
{
code: `
interface Foo {
readonly [key: string]: any;
}
`,
output: `
type Foo = Readonly<Record<string, any>>;
`,
errors: [{ messageId: 'preferRecord', line: 2, column: 1 }],
},

// Interface with generic parameter
{
code: `
Expand All @@ -153,6 +166,19 @@ type Foo<A> = Record<string, A>;
errors: [{ messageId: 'preferRecord', line: 2, column: 1 }],
},

// Readonly interface with generic parameter
{
code: `
interface Foo<A> {
readonly [key: string]: A;
}
`,
output: `
type Foo<A> = Readonly<Record<string, A>>;
`,
errors: [{ messageId: 'preferRecord', line: 2, column: 1 }],
},

// Interface with multiple generic parameters
{
code: `
Expand All @@ -166,20 +192,47 @@ type Foo<A, B> = Record<A, B>;
errors: [{ messageId: 'preferRecord', line: 2, column: 1 }],
},

// Readonly interface with multiple generic parameters
{
code: `
interface Foo<A, B> {
readonly [key: A]: B;
}
`,
output: `
type Foo<A, B> = Readonly<Record<A, B>>;
`,
errors: [{ messageId: 'preferRecord', line: 2, column: 1 }],
},

// Type literal
{
code: 'type Foo = { [key: string]: any };',
output: 'type Foo = Record<string, any>;',
errors: [{ messageId: 'preferRecord', line: 1, column: 12 }],
},

// Readonly type literal
{
code: 'type Foo = { readonly [key: string]: any };',
output: 'type Foo = Readonly<Record<string, any>>;',
errors: [{ messageId: 'preferRecord', line: 1, column: 12 }],
},

// Generic
{
code: 'type Foo = Generic<{ [key: string]: any }>;',
output: 'type Foo = Generic<Record<string, any>>;',
errors: [{ messageId: 'preferRecord', line: 1, column: 20 }],
},

// Readonly Generic
{
code: 'type Foo = Generic<{ readonly [key: string]: any }>;',
output: 'type Foo = Generic<Readonly<Record<string, any>>>;',
errors: [{ messageId: 'preferRecord', line: 1, column: 20 }],
},

// Function types
{
code: 'function foo(arg: { [key: string]: any }) {}',
Expand All @@ -192,6 +245,18 @@ type Foo<A, B> = Record<A, B>;
errors: [{ messageId: 'preferRecord', line: 1, column: 17 }],
},

// Readonly function types
{
code: 'function foo(arg: { readonly [key: string]: any }) {}',
output: 'function foo(arg: Readonly<Record<string, any>>) {}',
errors: [{ messageId: 'preferRecord', line: 1, column: 19 }],
},
{
code: 'function foo(): { readonly [key: string]: any } {}',
output: 'function foo(): Readonly<Record<string, any>> {}',
errors: [{ messageId: 'preferRecord', line: 1, column: 17 }],
},

// Never
// Type literal
{
Expand Down