Skip to content

fix(eslint-plugin): [consistent-indexed-object-style] don't report on indirect circular references #10537

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 94 additions & 10 deletions packages/eslint-plugin/src/rules/consistent-indexed-object-style.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ScopeVariable } from '@typescript-eslint/scope-manager';
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import type { ReportFixFunction } from '@typescript-eslint/utils/ts-eslint';

Expand All @@ -6,6 +7,7 @@ import { AST_NODE_TYPES, ASTUtils } from '@typescript-eslint/utils';
import {
createRule,
getFixOrSuggest,
isNodeEqual,
isParenthesized,
nullThrows,
} from '../util';
Expand Down Expand Up @@ -78,16 +80,12 @@ export default createRule<Options, MessageIds>({
if (parentId) {
const scope = context.sourceCode.getScope(parentId);
const superVar = ASTUtils.findVariable(scope, parentId.name);
if (superVar) {
const isCircular = superVar.references.some(
item =>
item.isTypeReference &&
node.range[0] <= item.identifier.range[0] &&
node.range[1] >= item.identifier.range[1],
);
if (isCircular) {
return;
}

if (
superVar &&
isDeeplyReferencingType(node, superVar, new Set([parentId]))
) {
return;
}
}

Expand Down Expand Up @@ -269,3 +267,89 @@ function findParentDeclaration(
}
return undefined;
}

function isDeeplyReferencingType(
node: TSESTree.Node,
superVar: ScopeVariable,
visited: Set<TSESTree.Node>,
): boolean {
if (visited.has(node)) {
// something on the chain is circular but it's not the reference being checked
return false;
}

visited.add(node);

switch (node.type) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I went over the possibilities and I think I got them all, though it's possible I missed some 🙈

Copy link
Member

Choose a reason for hiding this comment

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

This is pretty thorough and for a not-very-common case. I'm never surprised by edge cases popping up in "check all kinds of nodes/types" logic, but would be surprised if many came out of this.

case AST_NODE_TYPES.TSTypeLiteral:
return node.members.some(member =>
isDeeplyReferencingType(member, superVar, visited),
);
case AST_NODE_TYPES.TSTypeAliasDeclaration:
return isDeeplyReferencingType(node.typeAnnotation, superVar, visited);
case AST_NODE_TYPES.TSIndexedAccessType:
return [node.indexType, node.objectType].some(type =>
isDeeplyReferencingType(type, superVar, visited),
);
case AST_NODE_TYPES.TSConditionalType:
return [
node.checkType,
node.extendsType,
node.falseType,
node.trueType,
].some(type => isDeeplyReferencingType(type, superVar, visited));
case AST_NODE_TYPES.TSUnionType:
case AST_NODE_TYPES.TSIntersectionType:
return node.types.some(type =>
isDeeplyReferencingType(type, superVar, visited),
);
case AST_NODE_TYPES.TSInterfaceDeclaration:
return node.body.body.some(type =>
isDeeplyReferencingType(type, superVar, visited),
);
case AST_NODE_TYPES.TSTypeAnnotation:
return isDeeplyReferencingType(node.typeAnnotation, superVar, visited);
case AST_NODE_TYPES.TSIndexSignature: {
if (node.typeAnnotation) {
return isDeeplyReferencingType(node.typeAnnotation, superVar, visited);
}
break;
}
case AST_NODE_TYPES.TSTypeParameterInstantiation: {
return node.params.some(param =>
isDeeplyReferencingType(param, superVar, visited),
);
}
case AST_NODE_TYPES.TSTypeReference: {
if (isDeeplyReferencingType(node.typeName, superVar, visited)) {
return true;
}

if (
node.typeArguments &&
isDeeplyReferencingType(node.typeArguments, superVar, visited)
) {
return true;
}

break;
}
case AST_NODE_TYPES.Identifier: {
// check if the identifier is a reference of the type being checked
if (superVar.references.some(ref => isNodeEqual(ref.identifier, node))) {
return true;
}

// otherwise, follow its definition(s)
const refVar = ASTUtils.findVariable(superVar.scope, node.name);

if (refVar) {
return refVar.defs.some(def =>
isDeeplyReferencingType(def.node, superVar, visited),
);
}
}
}

return false;
}
Loading
Loading