Skip to content

Commit

Permalink
fix(eslint-plugin): [consistent-indexed-object-style] convert readonl…
Browse files Browse the repository at this point in the history
…y index signature to readonly record (#2798)
  • Loading branch information
ryym committed Nov 25, 2020
1 parent 73a63ee commit 29428a4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
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

0 comments on commit 29428a4

Please sign in to comment.