Open History to see the last commit
1import { AST_NODE_TYPES } from '@typescript-eslint/utils'
2import { uniqueBy } from './unique-by'
3import type { TSESLint, TSESTree } from '@typescript-eslint/utils'
4β
5export const ASTUtils = {
6 isNodeOfOneOf<T extends AST_NODE_TYPES>(
7 node: TSESTree.Node,
8 types: ReadonlyArray<T>,
9 ): node is TSESTree.Node & { type: T } {
10 return types.includes(node.type as T)
11 },
12 isIdentifier(node: TSESTree.Node): node is TSESTree.Identifier {
13 return node.type === AST_NODE_TYPES.Identifier
14 },
15 isIdentifierWithName(
16 node: TSESTree.Node,
17 name: string,
18 ): node is TSESTree.Identifier {
19 return ASTUtils.isIdentifier(node) && node.name === name
20 },
21 isIdentifierWithOneOfNames<T extends Array<string>>(
22 node: TSESTree.Node,
23 name: T,
24 ): node is TSESTree.Identifier & { name: T[number] } {
25 return ASTUtils.isIdentifier(node) && name.includes(node.name)
26 },
27 isProperty(node: TSESTree.Node): node is TSESTree.Property {
28 return node.type === AST_NODE_TYPES.Property
29 },
30 isObjectExpression(node: TSESTree.Node): node is TSESTree.ObjectExpression {
31 return node.type === AST_NODE_TYPES.ObjectExpression
32 },
33 isPropertyWithIdentifierKey(
34 node: TSESTree.Node,
35 key: string,
36 ): node is TSESTree.Property {
37 return (
38 ASTUtils.isProperty(node) && ASTUtils.isIdentifierWithName(node.key, key)
39 )
40 },
41 findPropertyWithIdentifierKey(
42 properties: Array<TSESTree.ObjectLiteralElement>,
43 key: string,
44 ): TSESTree.Property | undefined {
45 return properties.find((x): x is TSESTree.Property =>
46 ASTUtils.isPropertyWithIdentifierKey(x, key),
47 )
48 },
49 traverseUpOnly(
50 identifier: TSESTree.Node,
51 allowedNodeTypes: Array<AST_NODE_TYPES>,
52 ): TSESTree.Node {
53 const parent = identifier.parent
54β
55 if (parent !== undefined && allowedNodeTypes.includes(parent.type)) {
56 return ASTUtils.traverseUpOnly(parent, allowedNodeTypes)
57 }
58β
59 return identifier
60 },
61 traverseUpMemberExpression(node: TSESTree.Node): TSESTree.Node {
62 const parent = node.parent
63β
64 if (
65 parent !== undefined &&
66 ((parent.type === AST_NODE_TYPES.MemberExpression &&
67 parent.object === node) ||
68 parent.type === AST_NODE_TYPES.ChainExpression ||
69 parent.type === AST_NODE_TYPES.TSNonNullExpression)
70 ) {
71 return ASTUtils.traverseUpMemberExpression(parent)
72 }
73β
74 return node
75 },
76 isDeclaredInNode(params: {
77 functionNode: TSESTree.Node
78 reference: TSESLint.Scope.Reference
79 scopeManager: TSESLint.Scope.ScopeManager
80 }) {
81 const { functionNode, reference, scopeManager } = params
82 const scope = scopeManager.acquire(functionNode)
83β
84 if (scope === null) {
85 return false
86 }
87β
88 return scope.set.has(reference.identifier.name)
89 },
90 getExternalRefs(params: {
91 scopeManager: TSESLint.Scope.ScopeManager
92 sourceCode: Readonly<TSESLint.SourceCode>
93 node: TSESTree.Node
94 }): Array<TSESLint.Scope.Reference> {
95 const { scopeManager, sourceCode, node } = params
96 const scope = scopeManager.acquire(node)
97β
98 if (scope === null) {
99 return []
100 }
101β
102 const collectReferences = (
103 currentScope: TSESLint.Scope.Scope,
104 ): Array<TSESLint.Scope.Reference> => {
105 const references = [...currentScope.references]
106β
107 for (const childScope of currentScope.childScopes) {
108 references.push(...collectReferences(childScope))
109 }
110β
111 return references
112 }
113β
114 const references = collectReferences(scope)
115 .filter((x) => x.isRead() && !scope.set.has(x.identifier.name))
116 .map((x) => {
117 const memberPath = ASTUtils.traverseUpMemberExpression(x.identifier)
118 const memberExpression = memberPath.parent
119 const isComputedCallProperty =
120 memberExpression !== undefined &&
121 memberExpression.type === AST_NODE_TYPES.MemberExpression &&
122 memberExpression.computed &&
123 memberExpression.property === memberPath &&
124 memberExpression.parent.type === AST_NODE_TYPES.CallExpression &&
125 memberExpression.parent.callee === memberExpression
126β
127 const referenceNode = isComputedCallProperty
128 ? memberPath
129 : ASTUtils.traverseUpOnly(x.identifier, [
130 AST_NODE_TYPES.MemberExpression,
131 AST_NODE_TYPES.Identifier,
132 ])
133β
134 return {
135 variable: x,
136 node: referenceNode,
137 text: sourceCode.getText(referenceNode),
138 }
139 })
140β
141 const localRefIds = new Set(
142 [...scope.set.values()].map((x) => sourceCode.getText(x.identifiers[0])),
143 )
144β
145 const externalRefs = references.filter(
146 (x) => x.variable.resolved === null || !localRefIds.has(x.text),
147 )
148β
149 return uniqueBy(externalRefs, (x) => x.text).map((x) => x.variable)
150 },
151 isValidReactComponentOrHookName(
152 identifier: TSESTree.Identifier | null | undefined,
153 ) {
154 return (
155 identifier !== null &&
156 identifier !== undefined &&
157 /^(use|[A-Z])/.test(identifier.name)
158 )
159 },
160 getFunctionAncestor(
161 sourceCode: Readonly<TSESLint.SourceCode>,
162 node: TSESTree.Node,
163 ) {
164 for (const ancestor of sourceCode.getAncestors(node)) {
165 if (
166 ASTUtils.isNodeOfOneOf(ancestor, [
167 AST_NODE_TYPES.FunctionDeclaration,
168 AST_NODE_TYPES.FunctionExpression,
169 AST_NODE_TYPES.ArrowFunctionExpression,
170 ])
171 ) {
172 return ancestor
173 }
174β
175 if (
176 ancestor.parent?.type === AST_NODE_TYPES.VariableDeclarator &&
177 ancestor.parent.id.type === AST_NODE_TYPES.Identifier &&
178 ASTUtils.isNodeOfOneOf(ancestor, [
179 AST_NODE_TYPES.FunctionDeclaration,
180 AST_NODE_TYPES.FunctionExpression,
181 AST_NODE_TYPES.ArrowFunctionExpression,
182 ])
183 ) {
184 return ancestor
185 }
186 }
187β
188 return undefined
189 },
190 getReferencedExpressionByIdentifier(params: {
191 node: TSESTree.Node
192 context: Readonly<TSESLint.RuleContext<string, ReadonlyArray<unknown>>>
193 }) {
194 const { node, context } = params
195β
196 // we need the fallbacks for backwards compat with eslint < 8.37.0
197 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
198 const sourceCode = context.sourceCode ?? context.getSourceCode()
199 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
200 const scope = context.sourceCode.getScope(node)
201 ? sourceCode.getScope(node)
202 : context.getScope()
203β
204 const resolvedNode = scope.references.find((ref) => ref.identifier === node)
205 ?.resolved?.defs[0]?.node
206β
207 if (resolvedNode?.type !== AST_NODE_TYPES.VariableDeclarator) {
208 return null
209 }
210β
211 return resolvedNode.init
212 },
213}