Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
fix(eslint-plugin): [consistent-indexed-object-style] don't report on indirect circular references#10537
Changes fromall commits
a0cdea1
43464cf
474df6b
59a8383
9bc7d99
185b38d
762fb84
c81fe19
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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'; | ||
@@ -6,6 +7,7 @@ import { AST_NODE_TYPES, ASTUtils } from '@typescript-eslint/utils'; | ||
import { | ||
createRule, | ||
getFixOrSuggest, | ||
isNodeEqual, | ||
isParenthesized, | ||
nullThrows, | ||
} from '../util'; | ||
@@ -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 && | ||
isDeeplyReferencingType(node, superVar, new Set([parentId])) | ||
) { | ||
return; | ||
} | ||
} | ||
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 🙈 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.