Skip to content
Permalink
Browse files

fix(eslint-plugin): [consistent-indexed-object-style] fix wrong autof…

…ix behaviour with generics (#2722)
  • Loading branch information
timozander committed Nov 1, 2020
1 parent d8c67a5 commit 73d97130afe79b8a458c215581ce86c62009ad8b
@@ -115,7 +115,20 @@ export default createRule<Options, MessageIds>({
},

TSInterfaceDeclaration(node): void {
checkMembers(node.body.body, node, `type ${node.id.name} = `, ';');
let genericTypes = '';

if ((node.typeParameters?.params ?? []).length > 0) {
genericTypes = `<${node.typeParameters?.params
.map(p => p.name.name)
.join(', ')}>`;
}

checkMembers(
node.body.body,
node,
`type ${node.id.name}${genericTypes} = `,
';',
);
},
};
},
@@ -140,6 +140,32 @@ type Foo = Record<string, any>;
errors: [{ messageId: 'preferRecord', line: 2, column: 1 }],
},

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

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

// Type literal
{
code: 'type Foo = { [key: string]: any };',
@@ -175,6 +201,14 @@ type Foo = Record<string, any>;
errors: [{ messageId: 'preferIndexSignature', line: 1, column: 12 }],
},

// Type literal with generic parameter
{
code: 'type Foo<T> = Record<string, T>;',
options: ['index-signature'],
output: 'type Foo<T> = { [key: string]: T };',
errors: [{ messageId: 'preferIndexSignature', line: 1, column: 15 }],
},

// Generic
{
code: 'type Foo = Generic<Record<string, any>>;',

0 comments on commit 73d9713

Please sign in to comment.