-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
visitors.ts
388 lines (326 loc) 路 11 KB
/
visitors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import * as virtualTypes from "./path/lib/virtual-types.ts";
import * as virtualTypesValidators from "./path/lib/virtual-types-validator.ts";
import type { Node } from "@babel/types";
import {
DEPRECATED_KEYS,
DEPRECATED_ALIASES,
FLIPPED_ALIAS_KEYS,
TYPES,
__internal__deprecationWarning as deprecationWarning,
} from "@babel/types";
import type { ExplodedVisitor, NodePath, Visitor } from "./index.ts";
import type { ExplVisitNode, VisitNodeFunction, VisitPhase } from "./types.ts";
type VIRTUAL_TYPES = keyof typeof virtualTypes;
function isVirtualType(type: string): type is VIRTUAL_TYPES {
return type in virtualTypes;
}
export type VisitWrapper<S = any> = (
stateName: string | undefined,
visitorType: VisitPhase,
callback: VisitNodeFunction<S, Node>,
) => VisitNodeFunction<S, Node>;
export function isExplodedVisitor(
visitor: Visitor,
): visitor is ExplodedVisitor {
// @ts-expect-error _exploded is not defined on non-exploded Visitor
return visitor?._exploded;
}
/**
* explode() will take a visitor object with all of the various shorthands
* that we support, and validates & normalizes it into a common format, ready
* to be used in traversal
*
* The various shorthands are:
* * `Identifier() { ... }` -> `Identifier: { enter() { ... } }`
* * `"Identifier|NumericLiteral": { ... }` -> `Identifier: { ... }, NumericLiteral: { ... }`
* * Aliases in `@babel/types`: e.g. `Property: { ... }` -> `ObjectProperty: { ... }, ClassProperty: { ... }`
* Other normalizations are:
* * Visitors of virtual types are wrapped, so that they are only visited when
* their dynamic check passes
* * `enter` and `exit` functions are wrapped in arrays, to ease merging of
* visitors
*/
export function explode<S>(visitor: Visitor<S>): ExplodedVisitor<S> {
if (isExplodedVisitor(visitor)) return visitor;
// @ts-expect-error `visitor` will be cast to ExplodedVisitor by this function
visitor._exploded = true;
// normalise pipes
for (const nodeType of Object.keys(visitor) as (keyof Visitor)[]) {
if (shouldIgnoreKey(nodeType)) continue;
const parts: Array<string> = nodeType.split("|");
if (parts.length === 1) continue;
const fns = visitor[nodeType];
delete visitor[nodeType];
for (const part of parts) {
// @ts-expect-error part will be verified by `verify` later
visitor[part] = fns;
}
}
// verify data structure
verify(visitor);
// make sure there's no __esModule type since this is because we're using loose mode
// and it sets __esModule to be enumerable on all modules :(
// @ts-expect-error ESModule interop
delete visitor.__esModule;
// ensure visitors are objects
ensureEntranceObjects(visitor);
// ensure enter/exit callbacks are arrays
ensureCallbackArrays(visitor);
// add type wrappers
for (const nodeType of Object.keys(visitor)) {
if (shouldIgnoreKey(nodeType)) continue;
if (!isVirtualType(nodeType)) continue;
// wrap all the functions
const fns = visitor[nodeType];
for (const type of Object.keys(fns)) {
// @ts-expect-error normalised as VisitNodeObject
fns[type] = wrapCheck(nodeType, fns[type]);
}
// clear it from the visitor
delete visitor[nodeType];
const types = virtualTypes[nodeType];
if (types !== null) {
for (const type of types) {
// merge the visitor if necessary or just put it back in
if (visitor[type]) {
mergePair(visitor[type], fns);
} else {
// @ts-expect-error Expression produces too complex union
visitor[type] = fns;
}
}
} else {
mergePair(visitor, fns);
}
}
// add aliases
for (const nodeType of Object.keys(visitor) as (keyof Visitor)[]) {
if (shouldIgnoreKey(nodeType)) continue;
let aliases = FLIPPED_ALIAS_KEYS[nodeType];
if (nodeType in DEPRECATED_KEYS) {
const deprecatedKey = DEPRECATED_KEYS[nodeType];
deprecationWarning(nodeType, deprecatedKey, "Visitor ");
aliases = [deprecatedKey];
} else if (nodeType in DEPRECATED_ALIASES) {
const deprecatedAlias =
DEPRECATED_ALIASES[nodeType as keyof typeof DEPRECATED_ALIASES];
deprecationWarning(nodeType, deprecatedAlias, "Visitor ");
aliases = FLIPPED_ALIAS_KEYS[deprecatedAlias];
}
if (!aliases) continue;
const fns = visitor[nodeType];
// clear it from the visitor
delete visitor[nodeType];
for (const alias of aliases) {
const existing = visitor[alias];
if (existing) {
mergePair(existing, fns);
} else {
// @ts-expect-error Expression produces a union type that is too complex to represent.
visitor[alias] = { ...fns };
}
}
}
for (const nodeType of Object.keys(visitor)) {
if (shouldIgnoreKey(nodeType)) continue;
ensureCallbackArrays(
// @ts-expect-error nodeType must present in visitor after previous validations
visitor[nodeType],
);
}
// @ts-expect-error explosion has been performed
return visitor as ExplodedVisitor;
}
export function verify(visitor: Visitor) {
// @ts-expect-error _verified is not defined on non-verified Visitor.
// TODO: unify _verified and _exploded.
if (visitor._verified) return;
if (typeof visitor === "function") {
throw new Error(
"You passed `traverse()` a function when it expected a visitor object, " +
"are you sure you didn't mean `{ enter: Function }`?",
);
}
for (const nodeType of Object.keys(visitor) as (keyof Visitor)[]) {
if (nodeType === "enter" || nodeType === "exit") {
validateVisitorMethods(nodeType, visitor[nodeType]);
}
if (shouldIgnoreKey(nodeType)) continue;
if (TYPES.indexOf(nodeType) < 0) {
throw new Error(
`You gave us a visitor for the node type ${nodeType} but it's not a valid type`,
);
}
const visitors = visitor[nodeType];
if (typeof visitors === "object") {
for (const visitorKey of Object.keys(visitors)) {
if (visitorKey === "enter" || visitorKey === "exit") {
// verify that it just contains functions
validateVisitorMethods(
`${nodeType}.${visitorKey}`,
visitors[visitorKey],
);
} else {
throw new Error(
"You passed `traverse()` a visitor object with the property " +
`${nodeType} that has the invalid property ${visitorKey}`,
);
}
}
}
}
// @ts-expect-error _verified is not defined on non-verified Visitor.
// TODO: unify _verified and _exploded.
visitor._verified = true;
}
function validateVisitorMethods(
path: string,
val: any,
): asserts val is Function | Function[] {
const fns = [].concat(val);
for (const fn of fns) {
if (typeof fn !== "function") {
throw new TypeError(
`Non-function found defined in ${path} with type ${typeof fn}`,
);
}
}
}
export function merge<State>(
visitors: Visitor<State>[],
): ExplodedVisitor<State>;
export function merge(
visitors: Visitor<unknown>[],
states?: any[],
wrapper?: Function | null,
): ExplodedVisitor<unknown>;
export function merge(
visitors: any[],
states: any[] = [],
wrapper?: VisitWrapper | null,
): ExplodedVisitor {
// @ts-expect-error don't bother with internal flags so it can work with earlier @babel/core validations
const mergedVisitor: ExplodedVisitor = {};
for (let i = 0; i < visitors.length; i++) {
const visitor = explode(visitors[i]);
const state = states[i];
let topVisitor: ExplVisitNode<unknown, Node> = visitor;
if (state || wrapper) {
topVisitor = wrapWithStateOrWrapper(topVisitor, state, wrapper);
}
mergePair(mergedVisitor, topVisitor);
for (const key of Object.keys(visitor) as (keyof ExplodedVisitor)[]) {
if (shouldIgnoreKey(key)) continue;
let typeVisitor = visitor[key];
// if we have state or wrapper then overload the callbacks to take it
if (state || wrapper) {
typeVisitor = wrapWithStateOrWrapper(typeVisitor, state, wrapper);
}
const nodeVisitor = (mergedVisitor[key] ||= {});
mergePair(nodeVisitor, typeVisitor);
}
}
if (process.env.BABEL_8_BREAKING) {
return {
...mergedVisitor,
_exploded: true,
_verified: true,
};
}
return mergedVisitor;
}
function wrapWithStateOrWrapper<State>(
oldVisitor: ExplVisitNode<State, Node>,
state: State | null,
wrapper?: VisitWrapper<State> | null,
): ExplVisitNode<State, Node> {
const newVisitor: ExplVisitNode<State, Node> = {};
for (const phase of ["enter", "exit"] as VisitPhase[]) {
let fns = oldVisitor[phase];
// not an enter/exit array of callbacks
if (!Array.isArray(fns)) continue;
fns = fns.map(function (fn) {
let newFn = fn;
if (state) {
newFn = function (path: NodePath) {
fn.call(state, path, state);
};
}
if (wrapper) {
// @ts-expect-error Fixme: actually PluginPass.key (aka pluginAlias)?
newFn = wrapper(state?.key, phase, newFn);
}
// Override toString in case this function is printed, we want to print the wrapped function, same as we do in `wrapCheck`
if (newFn !== fn) {
newFn.toString = () => fn.toString();
}
return newFn;
});
newVisitor[phase] = fns;
}
return newVisitor;
}
function ensureEntranceObjects(obj: Visitor) {
for (const key of Object.keys(obj) as (keyof Visitor)[]) {
if (shouldIgnoreKey(key)) continue;
const fns = obj[key];
if (typeof fns === "function") {
// @ts-expect-error: Expression produces a union type that is too complex to represent.
obj[key] = { enter: fns };
}
}
}
function ensureCallbackArrays(obj: Visitor) {
if (obj.enter && !Array.isArray(obj.enter)) obj.enter = [obj.enter];
if (obj.exit && !Array.isArray(obj.exit)) obj.exit = [obj.exit];
}
function wrapCheck(nodeType: VIRTUAL_TYPES, fn: Function) {
const fnKey = `is${nodeType}`;
// @ts-expect-error we know virtualTypesValidators will contain `fnKey`, but TS doesn't
const validator = virtualTypesValidators[fnKey];
const newFn = function (this: unknown, path: NodePath) {
if (validator.call(path)) {
return fn.apply(this, arguments);
}
};
newFn.toString = () => fn.toString();
return newFn;
}
function shouldIgnoreKey(
key: string,
): key is
| `_${string}`
| "enter"
| "exit"
| "shouldSkip"
| "denylist"
| "noScope"
| "skipKeys"
| "blacklist" {
// internal/hidden key
if (key[0] === "_") return true;
// ignore function keys
if (key === "enter" || key === "exit" || key === "shouldSkip") return true;
// ignore other options
if (key === "denylist" || key === "noScope" || key === "skipKeys") {
return true;
}
if (!process.env.BABEL_8_BREAKING) {
if (key === "blacklist") {
return true;
}
}
return false;
}
/*
function mergePair(
dest: ExplVisitNode<unknown, Node>,
src: ExplVisitNode<unknown, Node>,
);
*/
function mergePair(dest: any, src: any) {
for (const phase of ["enter", "exit"] as VisitPhase[]) {
if (!src[phase]) continue;
dest[phase] = [].concat(dest[phase] || [], src[phase]);
}
}