|
1 | 1 | import * as AST from "@eslint-react/ast"; |
2 | | -import { _ } from "@eslint-react/eff"; |
| 2 | +import { _, identity } from "@eslint-react/eff"; |
3 | 3 | import type { RuleFeature } from "@eslint-react/shared"; |
4 | 4 | import { getSettingsFromContext } from "@eslint-react/shared"; |
5 | 5 | import * as VAR from "@eslint-react/var"; |
6 | | -import type { Variable } from "@typescript-eslint/scope-manager"; |
7 | 6 | import { getConstrainedTypeAtLocation } from "@typescript-eslint/type-utils"; |
8 | 7 | import type { TSESTree } from "@typescript-eslint/types"; |
9 | 8 | import { AST_NODE_TYPES as T } from "@typescript-eslint/types"; |
@@ -171,27 +170,6 @@ function inspectVariantTypes(types: ts.Type[]) { |
171 | 170 | return variantTypes; |
172 | 171 | } |
173 | 172 |
|
174 | | -function isInitExpression( |
175 | | - node: |
176 | | - | _ |
177 | | - | null |
178 | | - | TSESTree.Expression |
179 | | - | TSESTree.LetOrConstOrVarDeclaration, |
180 | | -): node is TSESTree.Expression { |
181 | | - if (node == null) return false; |
182 | | - return node.type !== T.VariableDeclaration; |
183 | | -} |
184 | | - |
185 | | -function getVariableInitExpression(variable: Variable | _, at: number): TSESTree.Expression | _ { |
186 | | - const def = variable?.defs[at]; |
187 | | - if (def?.node == null || !("init" in def.node)) { |
188 | | - return _; |
189 | | - } |
190 | | - return isInitExpression(def.node.init) |
191 | | - ? def.node.init |
192 | | - : _; |
193 | | -} |
194 | | - |
195 | 173 | // #endregion |
196 | 174 |
|
197 | 175 | // #region Rule Implementation |
@@ -274,7 +252,9 @@ export default createRule<[], MessageID>({ |
274 | 252 | }) |
275 | 253 | .with({ type: T.Identifier }, (n) => { |
276 | 254 | const variable = VAR.findVariable(n.name, context.sourceCode.getScope(n)); |
277 | | - const initExpression = getVariableInitExpression(variable, 0); |
| 255 | + const initExpression = match(variable?.defs.at(0)?.node) |
| 256 | + .with({ init: P.select({ type: P.not(T.VariableDeclaration) }) }, identity) |
| 257 | + .otherwise(() => _); |
278 | 258 | return getReportDescriptor(initExpression); |
279 | 259 | }) |
280 | 260 | .otherwise(() => _); |
|
0 commit comments