|
| 1 | +import { unit } from "@eslint-react/eff"; |
| 2 | +import type { Scope } from "@typescript-eslint/scope-manager"; |
| 3 | +import type { TSESTree } from "@typescript-eslint/types"; |
| 4 | + |
| 5 | +import { AST_NODE_TYPES as T } from "@typescript-eslint/types"; |
| 6 | +import { getVariableDefinitionNode } from "./get-variable-definition-node"; |
| 7 | + |
| 8 | +export type ObjectType = |
| 9 | + | { |
| 10 | + kind: "jsx"; |
| 11 | + node: |
| 12 | + | TSESTree.JSXElement |
| 13 | + | TSESTree.JSXFragment; |
| 14 | + } |
| 15 | + | { |
| 16 | + kind: "array"; |
| 17 | + node: TSESTree.ArrayExpression; |
| 18 | + } |
| 19 | + | { |
| 20 | + kind: "plain"; |
| 21 | + node: TSESTree.ObjectExpression; |
| 22 | + } |
| 23 | + | { |
| 24 | + kind: "class"; |
| 25 | + node: TSESTree.ClassExpression; |
| 26 | + } |
| 27 | + | { |
| 28 | + kind: "instance"; |
| 29 | + node: |
| 30 | + | TSESTree.NewExpression |
| 31 | + | TSESTree.ThisExpression; |
| 32 | + } |
| 33 | + | { |
| 34 | + kind: "function"; |
| 35 | + node: |
| 36 | + | TSESTree.FunctionDeclaration |
| 37 | + | TSESTree.FunctionExpression |
| 38 | + | TSESTree.ArrowFunctionExpression; |
| 39 | + } |
| 40 | + | { |
| 41 | + kind: "regexp"; |
| 42 | + node: TSESTree.RegExpLiteral; |
| 43 | + } |
| 44 | + | { |
| 45 | + kind: "unknown"; |
| 46 | + node: TSESTree.Node; |
| 47 | + // Reason for why the type is unknown |
| 48 | + reason: "call-expression" | "unsupported-node"; |
| 49 | + }; |
| 50 | + |
| 51 | +/** |
| 52 | + * Detects the ObjectType of a given node |
| 53 | + * @param node The node to check |
| 54 | + * @param initialScope The initial scope to check for variable declarations |
| 55 | + * @returns The ObjectType of the node, or undefined if not detected |
| 56 | + */ |
| 57 | +export function getObjectType( |
| 58 | + node: TSESTree.Node | unit, |
| 59 | + initialScope: Scope, |
| 60 | +): ObjectType | unit { |
| 61 | + if (node == null) return unit; |
| 62 | + switch (node.type) { |
| 63 | + case T.JSXElement: |
| 64 | + case T.JSXFragment: |
| 65 | + return { kind: "jsx", node } as const; |
| 66 | + case T.ArrayExpression: |
| 67 | + return { kind: "array", node } as const; |
| 68 | + case T.ObjectExpression: |
| 69 | + return { kind: "plain", node } as const; |
| 70 | + case T.ClassExpression: |
| 71 | + return { kind: "class", node } as const; |
| 72 | + case T.NewExpression: |
| 73 | + case T.ThisExpression: |
| 74 | + return { kind: "instance", node } as const; |
| 75 | + case T.FunctionDeclaration: |
| 76 | + case T.FunctionExpression: |
| 77 | + case T.ArrowFunctionExpression: |
| 78 | + return { kind: "function", node } as const; |
| 79 | + case T.Literal: { |
| 80 | + if ("regex" in node) { |
| 81 | + return { kind: "regexp", node } as const; |
| 82 | + } |
| 83 | + return unit; |
| 84 | + } |
| 85 | + case T.Identifier: { |
| 86 | + if (!("name" in node) || typeof node.name !== "string") { |
| 87 | + return unit; |
| 88 | + } |
| 89 | + const variable = initialScope.set.get(node.name); |
| 90 | + const variableNode = getVariableDefinitionNode(variable, -1); |
| 91 | + return getObjectType(variableNode, initialScope); |
| 92 | + } |
| 93 | + case T.MemberExpression: { |
| 94 | + if (!("object" in node)) return unit; |
| 95 | + return getObjectType(node.object, initialScope); |
| 96 | + } |
| 97 | + case T.AssignmentExpression: |
| 98 | + case T.AssignmentPattern: { |
| 99 | + if (!("right" in node)) return unit; |
| 100 | + return getObjectType(node.right, initialScope); |
| 101 | + } |
| 102 | + case T.LogicalExpression: { |
| 103 | + return getObjectType(node.right, initialScope); |
| 104 | + } |
| 105 | + case T.ConditionalExpression: { |
| 106 | + return getObjectType(node.consequent, initialScope) ?? getObjectType(node.alternate, initialScope); |
| 107 | + } |
| 108 | + case T.SequenceExpression: { |
| 109 | + if (node.expressions.length === 0) { |
| 110 | + return unit; |
| 111 | + } |
| 112 | + return getObjectType( |
| 113 | + node.expressions[node.expressions.length - 1], |
| 114 | + initialScope, |
| 115 | + ); |
| 116 | + } |
| 117 | + case T.CallExpression: { |
| 118 | + return { kind: "unknown", node, reason: "call-expression" } as const; |
| 119 | + } |
| 120 | + default: { |
| 121 | + if (!("expression" in node) || typeof node.expression !== "object") { |
| 122 | + return unit; |
| 123 | + } |
| 124 | + return getObjectType(node.expression, initialScope); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments