Skip to content

fix(eslint-plugin): [consistent-indexed-object-style] fixed circular reference issue #2766

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

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,46 @@ export default createRule<Options, MessageIds>({
return;
}

const scope = context.getScope();
if (
scope.block.type == AST_NODE_TYPES.Program &&
scope.block.body[0].type == AST_NODE_TYPES.TSTypeAliasDeclaration
) {
const body = scope.block.body[0];
const name = body?.id.name;
const memberTypes = member.typeAnnotation?.typeAnnotation;
if (memberTypes) {
if (
memberTypes.type == AST_NODE_TYPES.TSTypeReference &&
memberTypes.typeName.type == AST_NODE_TYPES.Identifier &&
memberTypes.typeName.name == name
) {
return;
} else if (memberTypes.type == AST_NODE_TYPES.TSUnionType) {
const membersArray = memberTypes.types;
let flag = false;
membersArray.forEach((m: TSESTree.Node) => {
if (
m.type == AST_NODE_TYPES.TSTypeReference &&
m.typeName.type == AST_NODE_TYPES.Identifier &&
m.typeName.name == name
) {
flag = true;
}
});
if (flag) {
return;
}
Comment on lines +114 to +127
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use a for-of loop instead of .forEach and that allows you to return directly instead of using the flag:

Suggested change
const membersArray = memberTypes.types;
let flag = false;
membersArray.forEach((m: TSESTree.Node) => {
if (
m.type == AST_NODE_TYPES.TSTypeReference &&
m.typeName.type == AST_NODE_TYPES.Identifier &&
m.typeName.name == name
) {
flag = true;
}
});
if (flag) {
return;
}
const membersArray: TSESTree.Node[] = memberTypes.types;
for (const m of membersArray) {
if (
m.type == AST_NODE_TYPES.TSTypeReference &&
m.typeName.type == AST_NODE_TYPES.Identifier &&
m.typeName.name == name
) {
return;
}
}

}
}
}

context.report({
node,
messageId: 'preferRecord',
fix(fixer) {
const key = sourceCode.getText(keyType.typeAnnotation);
const value = sourceCode.getText(valueType.typeAnnotation);
const key = sourceCode.getText(keyType.typeAnnotation);
return fixer.replaceText(
node,
`${prefix}Record<${key}, ${value}>${postfix}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ interface Foo {
[];
}
`,
'type Foo = { [key: string]: string | Foo };',
'type Foo = { [key: string]: Foo };',

// 'index-signature'
// Unhandled type
Expand Down Expand Up @@ -172,6 +174,26 @@ type Foo<A, B> = Record<A, B>;
output: 'type Foo = Record<string, any>;',
errors: [{ messageId: 'preferRecord', line: 1, column: 12 }],
},
{
code: 'type T = { [k: string]: AT };',
output: 'type T = Record<string, AT>;',
errors: [{ messageId: 'preferRecord', line: 1, column: 10 }],
},
{
code: 'type T = { [k: string]: TA };',
output: 'type T = Record<string, TA>;',
errors: [{ messageId: 'preferRecord', line: 1, column: 10 }],
},
{
code: 'type T = { [k: string]: A.T };',
output: 'type T = Record<string, A.T>;',
errors: [{ messageId: 'preferRecord', line: 1, column: 10 }],
},
{
code: 'type Foo = { [key: string]: AnotherFoo };',
output: 'type Foo = Record<string, AnotherFoo>;',
errors: [{ messageId: 'preferRecord', line: 1, column: 12 }],
},

// Generic
{
Expand Down