1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.environmentVisitor = environmentVisitor;
|
---|
7 | exports.explode = explode$1;
|
---|
8 | exports.isExplodedVisitor = isExplodedVisitor;
|
---|
9 | exports.merge = merge;
|
---|
10 | exports.verify = verify$1;
|
---|
11 | var virtualTypes = require("./path/lib/virtual-types.js");
|
---|
12 | var virtualTypesValidators = require("./path/lib/virtual-types-validator.js");
|
---|
13 | var _t = require("@babel/types");
|
---|
14 | var _context = require("./path/context.js");
|
---|
15 | const {
|
---|
16 | DEPRECATED_KEYS,
|
---|
17 | DEPRECATED_ALIASES,
|
---|
18 | FLIPPED_ALIAS_KEYS,
|
---|
19 | TYPES,
|
---|
20 | __internal__deprecationWarning: deprecationWarning
|
---|
21 | } = _t;
|
---|
22 | function isVirtualType(type) {
|
---|
23 | return type in virtualTypes;
|
---|
24 | }
|
---|
25 | function isExplodedVisitor(visitor) {
|
---|
26 | return visitor == null ? void 0 : visitor._exploded;
|
---|
27 | }
|
---|
28 | function explode$1(visitor) {
|
---|
29 | if (isExplodedVisitor(visitor)) return visitor;
|
---|
30 | visitor._exploded = true;
|
---|
31 | for (const nodeType of Object.keys(visitor)) {
|
---|
32 | if (shouldIgnoreKey(nodeType)) continue;
|
---|
33 | const parts = nodeType.split("|");
|
---|
34 | if (parts.length === 1) continue;
|
---|
35 | const fns = visitor[nodeType];
|
---|
36 | delete visitor[nodeType];
|
---|
37 | for (const part of parts) {
|
---|
38 | visitor[part] = fns;
|
---|
39 | }
|
---|
40 | }
|
---|
41 | verify$1(visitor);
|
---|
42 | delete visitor.__esModule;
|
---|
43 | ensureEntranceObjects(visitor);
|
---|
44 | ensureCallbackArrays(visitor);
|
---|
45 | for (const nodeType of Object.keys(visitor)) {
|
---|
46 | if (shouldIgnoreKey(nodeType)) continue;
|
---|
47 | if (!isVirtualType(nodeType)) continue;
|
---|
48 | const fns = visitor[nodeType];
|
---|
49 | for (const type of Object.keys(fns)) {
|
---|
50 | fns[type] = wrapCheck(nodeType, fns[type]);
|
---|
51 | }
|
---|
52 | delete visitor[nodeType];
|
---|
53 | const types = virtualTypes[nodeType];
|
---|
54 | if (types !== null) {
|
---|
55 | for (const type of types) {
|
---|
56 | if (visitor[type]) {
|
---|
57 | mergePair(visitor[type], fns);
|
---|
58 | } else {
|
---|
59 | visitor[type] = fns;
|
---|
60 | }
|
---|
61 | }
|
---|
62 | } else {
|
---|
63 | mergePair(visitor, fns);
|
---|
64 | }
|
---|
65 | }
|
---|
66 | for (const nodeType of Object.keys(visitor)) {
|
---|
67 | if (shouldIgnoreKey(nodeType)) continue;
|
---|
68 | let aliases = FLIPPED_ALIAS_KEYS[nodeType];
|
---|
69 | if (nodeType in DEPRECATED_KEYS) {
|
---|
70 | const deprecatedKey = DEPRECATED_KEYS[nodeType];
|
---|
71 | deprecationWarning(nodeType, deprecatedKey, "Visitor ");
|
---|
72 | aliases = [deprecatedKey];
|
---|
73 | } else if (nodeType in DEPRECATED_ALIASES) {
|
---|
74 | const deprecatedAlias = DEPRECATED_ALIASES[nodeType];
|
---|
75 | deprecationWarning(nodeType, deprecatedAlias, "Visitor ");
|
---|
76 | aliases = FLIPPED_ALIAS_KEYS[deprecatedAlias];
|
---|
77 | }
|
---|
78 | if (!aliases) continue;
|
---|
79 | const fns = visitor[nodeType];
|
---|
80 | delete visitor[nodeType];
|
---|
81 | for (const alias of aliases) {
|
---|
82 | const existing = visitor[alias];
|
---|
83 | if (existing) {
|
---|
84 | mergePair(existing, fns);
|
---|
85 | } else {
|
---|
86 | visitor[alias] = Object.assign({}, fns);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 | for (const nodeType of Object.keys(visitor)) {
|
---|
91 | if (shouldIgnoreKey(nodeType)) continue;
|
---|
92 | ensureCallbackArrays(visitor[nodeType]);
|
---|
93 | }
|
---|
94 | return visitor;
|
---|
95 | }
|
---|
96 | function verify$1(visitor) {
|
---|
97 | if (visitor._verified) return;
|
---|
98 | if (typeof visitor === "function") {
|
---|
99 | throw new Error("You passed `traverse()` a function when it expected a visitor object, " + "are you sure you didn't mean `{ enter: Function }`?");
|
---|
100 | }
|
---|
101 | for (const nodeType of Object.keys(visitor)) {
|
---|
102 | if (nodeType === "enter" || nodeType === "exit") {
|
---|
103 | validateVisitorMethods(nodeType, visitor[nodeType]);
|
---|
104 | }
|
---|
105 | if (shouldIgnoreKey(nodeType)) continue;
|
---|
106 | if (!TYPES.includes(nodeType)) {
|
---|
107 | throw new Error(`You gave us a visitor for the node type ${nodeType} but it's not a valid type in @babel/traverse ${"7.25.3"}`);
|
---|
108 | }
|
---|
109 | const visitors = visitor[nodeType];
|
---|
110 | if (typeof visitors === "object") {
|
---|
111 | for (const visitorKey of Object.keys(visitors)) {
|
---|
112 | if (visitorKey === "enter" || visitorKey === "exit") {
|
---|
113 | validateVisitorMethods(`${nodeType}.${visitorKey}`, visitors[visitorKey]);
|
---|
114 | } else {
|
---|
115 | throw new Error("You passed `traverse()` a visitor object with the property " + `${nodeType} that has the invalid property ${visitorKey}`);
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|
120 | visitor._verified = true;
|
---|
121 | }
|
---|
122 | function validateVisitorMethods(path, val) {
|
---|
123 | const fns = [].concat(val);
|
---|
124 | for (const fn of fns) {
|
---|
125 | if (typeof fn !== "function") {
|
---|
126 | throw new TypeError(`Non-function found defined in ${path} with type ${typeof fn}`);
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|
130 | function merge(visitors, states = [], wrapper) {
|
---|
131 | const mergedVisitor = {
|
---|
132 | _verified: true,
|
---|
133 | _exploded: true
|
---|
134 | };
|
---|
135 | {
|
---|
136 | Object.defineProperty(mergedVisitor, "_exploded", {
|
---|
137 | enumerable: false
|
---|
138 | });
|
---|
139 | Object.defineProperty(mergedVisitor, "_verified", {
|
---|
140 | enumerable: false
|
---|
141 | });
|
---|
142 | }
|
---|
143 | for (let i = 0; i < visitors.length; i++) {
|
---|
144 | const visitor = explode$1(visitors[i]);
|
---|
145 | const state = states[i];
|
---|
146 | let topVisitor = visitor;
|
---|
147 | if (state || wrapper) {
|
---|
148 | topVisitor = wrapWithStateOrWrapper(topVisitor, state, wrapper);
|
---|
149 | }
|
---|
150 | mergePair(mergedVisitor, topVisitor);
|
---|
151 | for (const key of Object.keys(visitor)) {
|
---|
152 | if (shouldIgnoreKey(key)) continue;
|
---|
153 | let typeVisitor = visitor[key];
|
---|
154 | if (state || wrapper) {
|
---|
155 | typeVisitor = wrapWithStateOrWrapper(typeVisitor, state, wrapper);
|
---|
156 | }
|
---|
157 | const nodeVisitor = mergedVisitor[key] || (mergedVisitor[key] = {});
|
---|
158 | mergePair(nodeVisitor, typeVisitor);
|
---|
159 | }
|
---|
160 | }
|
---|
161 | return mergedVisitor;
|
---|
162 | }
|
---|
163 | function wrapWithStateOrWrapper(oldVisitor, state, wrapper) {
|
---|
164 | const newVisitor = {};
|
---|
165 | for (const phase of ["enter", "exit"]) {
|
---|
166 | let fns = oldVisitor[phase];
|
---|
167 | if (!Array.isArray(fns)) continue;
|
---|
168 | fns = fns.map(function (fn) {
|
---|
169 | let newFn = fn;
|
---|
170 | if (state) {
|
---|
171 | newFn = function (path) {
|
---|
172 | fn.call(state, path, state);
|
---|
173 | };
|
---|
174 | }
|
---|
175 | if (wrapper) {
|
---|
176 | newFn = wrapper(state == null ? void 0 : state.key, phase, newFn);
|
---|
177 | }
|
---|
178 | if (newFn !== fn) {
|
---|
179 | newFn.toString = () => fn.toString();
|
---|
180 | }
|
---|
181 | return newFn;
|
---|
182 | });
|
---|
183 | newVisitor[phase] = fns;
|
---|
184 | }
|
---|
185 | return newVisitor;
|
---|
186 | }
|
---|
187 | function ensureEntranceObjects(obj) {
|
---|
188 | for (const key of Object.keys(obj)) {
|
---|
189 | if (shouldIgnoreKey(key)) continue;
|
---|
190 | const fns = obj[key];
|
---|
191 | if (typeof fns === "function") {
|
---|
192 | obj[key] = {
|
---|
193 | enter: fns
|
---|
194 | };
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 | function ensureCallbackArrays(obj) {
|
---|
199 | if (obj.enter && !Array.isArray(obj.enter)) obj.enter = [obj.enter];
|
---|
200 | if (obj.exit && !Array.isArray(obj.exit)) obj.exit = [obj.exit];
|
---|
201 | }
|
---|
202 | function wrapCheck(nodeType, fn) {
|
---|
203 | const fnKey = `is${nodeType}`;
|
---|
204 | const validator = virtualTypesValidators[fnKey];
|
---|
205 | const newFn = function (path) {
|
---|
206 | if (validator.call(path)) {
|
---|
207 | return fn.apply(this, arguments);
|
---|
208 | }
|
---|
209 | };
|
---|
210 | newFn.toString = () => fn.toString();
|
---|
211 | return newFn;
|
---|
212 | }
|
---|
213 | function shouldIgnoreKey(key) {
|
---|
214 | if (key[0] === "_") return true;
|
---|
215 | if (key === "enter" || key === "exit" || key === "shouldSkip") return true;
|
---|
216 | if (key === "denylist" || key === "noScope" || key === "skipKeys") {
|
---|
217 | return true;
|
---|
218 | }
|
---|
219 | {
|
---|
220 | if (key === "blacklist") {
|
---|
221 | return true;
|
---|
222 | }
|
---|
223 | }
|
---|
224 | return false;
|
---|
225 | }
|
---|
226 | function mergePair(dest, src) {
|
---|
227 | for (const phase of ["enter", "exit"]) {
|
---|
228 | if (!src[phase]) continue;
|
---|
229 | dest[phase] = [].concat(dest[phase] || [], src[phase]);
|
---|
230 | }
|
---|
231 | }
|
---|
232 | const _environmentVisitor = {
|
---|
233 | FunctionParent(path) {
|
---|
234 | if (path.isArrowFunctionExpression()) return;
|
---|
235 | path.skip();
|
---|
236 | if (path.isMethod()) {
|
---|
237 | if (!path.requeueComputedKeyAndDecorators) {
|
---|
238 | _context.requeueComputedKeyAndDecorators.call(path);
|
---|
239 | } else {
|
---|
240 | path.requeueComputedKeyAndDecorators();
|
---|
241 | }
|
---|
242 | }
|
---|
243 | },
|
---|
244 | Property(path) {
|
---|
245 | if (path.isObjectProperty()) return;
|
---|
246 | path.skip();
|
---|
247 | if (!path.requeueComputedKeyAndDecorators) {
|
---|
248 | _context.requeueComputedKeyAndDecorators.call(path);
|
---|
249 | } else {
|
---|
250 | path.requeueComputedKeyAndDecorators();
|
---|
251 | }
|
---|
252 | }
|
---|
253 | };
|
---|
254 | function environmentVisitor(visitor) {
|
---|
255 | return merge([_environmentVisitor, visitor]);
|
---|
256 | }
|
---|
257 |
|
---|
258 | //# sourceMappingURL=visitors.js.map
|
---|