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] fix wrong autofix behaviour with generics #2722

Merged
merged 1 commit into from Nov 1, 2020
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 @@ -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