Skip to content

Commit

Permalink
fix(eslint-plugin): [consistent-indexed-object-style] fix wrong autof…
Browse files Browse the repository at this point in the history
…ix behaviour with generics (#2722)
  • Loading branch information
timozander committed Nov 1, 2020
1 parent d8c67a5 commit 73d9713
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
Expand Up @@ -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} = `,
';',
);
},
};
},
Expand Down
Expand Up @@ -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 };',
Expand Down Expand Up @@ -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>>;',
Expand Down

0 comments on commit 73d9713

Please sign in to comment.