|
| 1 | +import { namedTypes } from "ast-types" |
| 2 | +import { TreeNode } from "../core/treeNode" |
| 3 | +import { Classes } from "./classes" |
| 4 | +import { Functions } from "./functions" |
| 5 | + |
| 6 | +export namespace VariableDeclarations { |
| 7 | + export function Handle(n: namedTypes.VariableDeclaration) { |
| 8 | + const nodes: TreeNode[] = [] |
| 9 | + |
| 10 | + // For variable declarations |
| 11 | + for (const d of n.declarations) { |
| 12 | + if (namedTypes.VariableDeclarator.check(d)) { |
| 13 | + const node = new TreeNode( |
| 14 | + (d.id as any).name, |
| 15 | + "variable", |
| 16 | + "", |
| 17 | + n.loc?.start.line ?? 0, |
| 18 | + n.loc?.start.column ?? 0 |
| 19 | + ) |
| 20 | + |
| 21 | + // For variables declared using other variables |
| 22 | + if (namedTypes.Identifier.check(d.init)) { |
| 23 | + if (!d.init.name) continue |
| 24 | + node.pushUse({ |
| 25 | + name: d.init.name, |
| 26 | + type: "variable", |
| 27 | + }) |
| 28 | + } |
| 29 | + // For variables declared using function calls |
| 30 | + else if (namedTypes.CallExpression.check(d.init)) { |
| 31 | + node.pushUse( |
| 32 | + { |
| 33 | + name: (d.init.callee as any).name, |
| 34 | + type: "function", |
| 35 | + }, |
| 36 | + ...Functions.flattenCallExpression(d.init) |
| 37 | + ) |
| 38 | + } |
| 39 | + // For variables declared using new expressions |
| 40 | + else if (namedTypes.NewExpression.check(d.init)) { |
| 41 | + if (namedTypes.Identifier.check(d.init.callee)) { |
| 42 | + node.pushUse({ |
| 43 | + name: d.init.callee.name, |
| 44 | + type: "class", |
| 45 | + }) |
| 46 | + } |
| 47 | + node.pushUse(...Classes.flattenNewExpression(d.init)) |
| 48 | + } |
| 49 | + |
| 50 | + nodes.push(node) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return nodes |
| 55 | + } |
| 56 | +} |
0 commit comments