|
| 1 | +import type { Scope } from "@typescript-eslint/scope-manager"; |
| 2 | +import type { TSESTree } from "@typescript-eslint/types"; |
| 3 | +import type { Construction } from "./construction"; |
| 4 | +import { _ } from "@eslint-react/eff"; |
| 5 | + |
| 6 | +import { AST_NODE_TYPES as T } from "@typescript-eslint/types"; |
| 7 | +import { getVariableInitNode } from "../get-variable-init-node"; |
| 8 | +import { ConstructionDetectionHint } from "./construction-detection-hint"; |
| 9 | + |
| 10 | +/** |
| 11 | + * Detects the construction type of a given node. |
| 12 | + * @param node The node to check. |
| 13 | + * @param initialScope The initial scope to check for variable declarations. |
| 14 | + * @param hint Optional hint to control the detection behavior. |
| 15 | + * @returns The construction type of the node, or `_` if not found. |
| 16 | + */ |
| 17 | +export function getConstructionDetectionResult( |
| 18 | + node: TSESTree.Node | _, |
| 19 | + initialScope: Scope, |
| 20 | + hint = ConstructionDetectionHint.None, |
| 21 | +): Construction | _ { |
| 22 | + if (node == null) return _; |
| 23 | + switch (node.type) { |
| 24 | + case T.JSXElement: |
| 25 | + case T.JSXFragment: |
| 26 | + return { kind: "JSXElement", node } as const; |
| 27 | + case T.ArrayExpression: |
| 28 | + return { kind: "ArrayExpression", node } as const; |
| 29 | + case T.ObjectExpression: |
| 30 | + return { kind: "ObjectExpression", node } as const; |
| 31 | + case T.ClassExpression: |
| 32 | + return { kind: "ClassExpression", node } as const; |
| 33 | + case T.NewExpression: |
| 34 | + return { kind: "NewExpression", node } as const; |
| 35 | + case T.FunctionExpression: |
| 36 | + case T.ArrowFunctionExpression: |
| 37 | + return { kind: "FunctionExpression", node } as const; |
| 38 | + case T.CallExpression: { |
| 39 | + if (hint & ConstructionDetectionHint.StrictCallExpression) { |
| 40 | + return { kind: "CallExpression", node } as const; |
| 41 | + } |
| 42 | + return _; |
| 43 | + } |
| 44 | + case T.MemberExpression: { |
| 45 | + if (!("object" in node)) return _; |
| 46 | + return getConstructionDetectionResult(node.object, initialScope, hint); |
| 47 | + } |
| 48 | + case T.AssignmentExpression: |
| 49 | + case T.AssignmentPattern: { |
| 50 | + if (!("right" in node)) return _; |
| 51 | + return getConstructionDetectionResult(node.right, initialScope, hint); |
| 52 | + } |
| 53 | + case T.LogicalExpression: { |
| 54 | + const lvc = getConstructionDetectionResult(node.left, initialScope, hint); |
| 55 | + if (lvc == null) return _; |
| 56 | + return getConstructionDetectionResult(node.right, initialScope, hint); |
| 57 | + } |
| 58 | + case T.ConditionalExpression: { |
| 59 | + const cvc = getConstructionDetectionResult(node.consequent, initialScope, hint); |
| 60 | + if (cvc == null) return _; |
| 61 | + return getConstructionDetectionResult(node.alternate, initialScope, hint); |
| 62 | + } |
| 63 | + case T.Identifier: { |
| 64 | + if (!("name" in node) || typeof node.name !== "string") { |
| 65 | + return _; |
| 66 | + } |
| 67 | + const variable = initialScope.set.get(node.name); |
| 68 | + const variableNode = getVariableInitNode(variable, -1); |
| 69 | + return getConstructionDetectionResult(variableNode, initialScope, hint); |
| 70 | + } |
| 71 | + case T.Literal: { |
| 72 | + if ("regex" in node) { |
| 73 | + return { kind: "RegExpLiteral", node } as const; |
| 74 | + } |
| 75 | + return _; |
| 76 | + } |
| 77 | + default: { |
| 78 | + if (!("expression" in node) || typeof node.expression !== "object") { |
| 79 | + return _; |
| 80 | + } |
| 81 | + return getConstructionDetectionResult(node.expression, initialScope, hint); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments