Debian Codegraph @

packages/babel-traverse/src/index.ts

raw symbols SCIP
1 import * as visitors from "./visitors";
2 import {
3 VISITOR_KEYS,
4 removeProperties,
5 type RemovePropertiesOptions,
6 traverseFast,
7 } from "@babel/types";
8 import type * as t from "@babel/types";
9 import * as cache from "./cache";
10 import type NodePath from "./path";
11 import type { default as Scope, Binding } from "./scope";
12 import type { Visitor } from "./types";
13 import { traverseNode } from "./traverse-node";
14
15 export type { Visitor, Binding };
16 export { default as NodePath } from "./path";
17 export { default as Scope } from "./scope";
18 export { default as Hub } from "./hub";
19 export type { HubInterface } from "./hub";
20
21 export { visitors };
22
23 export type TraverseOptions<S = t.Node> = {
24 scope?: Scope;
25 noScope?: boolean;
26 denylist?: string[];
27 } & Visitor<S>;
28
29 function traverse<S>(
30 parent: t.Node,
31 opts: TraverseOptions<S>,
32 scope: Scope | undefined,
33 state: S,
34 parentPath?: NodePath,
35 ): void;
36
37 function traverse(
38 parent: t.Node,
39 opts: TraverseOptions,
40 scope?: Scope,
41 state?: any,
42 parentPath?: NodePath,
43 ): void;
44
45 function traverse<Options extends TraverseOptions>(
46 parent: t.Node,
47 // @ts-expect-error provide {} as default value for Options
48 opts: Options = {},
49 scope?: Scope,
50 state?: any,
51 parentPath?: NodePath,
52 ) {
53 if (!parent) return;
54
55 if (!opts.noScope && !scope) {
56 if (parent.type !== "Program" && parent.type !== "File") {
57 throw new Error(
58 "You must pass a scope and parentPath unless traversing a Program/File. " +
59 `Instead of that you tried to traverse a ${parent.type} node without ` +
60 "passing scope and parentPath.",
61 );
62 }
63 }
64
65 if (!VISITOR_KEYS[parent.type]) {
66 return;
67 }
68
69 visitors.explode(opts as Visitor);
70
71 traverseNode(parent, opts, scope, state, parentPath);
72 }
73
74 export default traverse;
75
76 traverse.visitors = visitors;
77 traverse.verify = visitors.verify;
78 traverse.explode = visitors.explode;
79
80 traverse.cheap = function (node: t.Node, enter: (node: t.Node) => void) {
81 return traverseFast(node, enter);
82 };
83
84 traverse.node = function (
85 node: t.Node,
86 opts: TraverseOptions,
87 scope?: Scope,
88 state?: any,
89 path?: NodePath,
90 skipKeys?: Record<string, boolean>,
91 ) {
92 traverseNode(node, opts, scope, state, path, skipKeys);
93 // traverse.node always returns undefined
94 };
95
96 traverse.clearNode = function (node: t.Node, opts?: RemovePropertiesOptions) {
97 removeProperties(node, opts);
98
99 cache.path.delete(node);
100 };
101
102 traverse.removeProperties = function (
103 tree: t.Node,
104 opts?: RemovePropertiesOptions,
105 ) {
106 traverseFast(tree, traverse.clearNode, opts);
107 return tree;
108 };
109
110 type HasDenylistedTypeState = {
111 has: boolean;
112 type: t.Node["type"];
113 };
114 function hasDenylistedType(path: NodePath, state: HasDenylistedTypeState) {
115 if (path.node.type === state.type) {
116 state.has = true;
117 path.stop();
118 }
119 }
120
121 traverse.hasType = function (
122 tree: t.Node,
123 type: t.Node["type"],
124 denylistTypes?: Array<string>,
125 ): boolean {
126 // the node we're searching in is denylisted
127 if (denylistTypes?.includes(tree.type)) return false;
128
129 // the type we're looking for is the same as the passed node
130 if (tree.type === type) return true;
131
132 const state: HasDenylistedTypeState = {
133 has: false,
134 type: type,
135 };
136
137 traverse(
138 tree,
139 {
140 noScope: true,
141 denylist: denylistTypes,
142 enter: hasDenylistedType,
143 },
144 null,
145 state,
146 );
147
148 return state.has;
149 };
150
151 traverse.cache = cache;