@babel/traverse 7.22.8 → 8.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cache.js +7 -18
- package/lib/cache.js.map +1 -1
- package/lib/context.js +7 -14
- package/lib/context.js.map +1 -1
- package/lib/hub.js +1 -8
- package/lib/hub.js.map +1 -1
- package/lib/index.js +4846 -33
- package/lib/index.js.map +1 -1
- package/lib/path/ancestry.js +11 -26
- package/lib/path/ancestry.js.map +1 -1
- package/lib/path/comments.js +4 -12
- package/lib/path/comments.js.map +1 -1
- package/lib/path/context.js +35 -63
- package/lib/path/context.js.map +1 -1
- package/lib/path/conversion.js +18 -38
- package/lib/path/conversion.js.map +1 -1
- package/lib/path/evaluation.js +4 -11
- package/lib/path/evaluation.js.map +1 -1
- package/lib/path/family.js +19 -33
- package/lib/path/family.js.map +1 -1
- package/lib/path/index.js +42 -54
- package/lib/path/index.js.map +1 -1
- package/lib/path/inference/index.js +9 -21
- package/lib/path/inference/index.js.map +1 -1
- package/lib/path/inference/inferer-reference.js +5 -11
- package/lib/path/inference/inferer-reference.js.map +1 -1
- package/lib/path/inference/inferers.js +30 -65
- package/lib/path/inference/inferers.js.map +1 -1
- package/lib/path/inference/util.js +6 -16
- package/lib/path/inference/util.js.map +1 -1
- package/lib/path/introspection.js +20 -45
- package/lib/path/introspection.js.map +1 -1
- package/lib/path/lib/hoister.js +10 -17
- package/lib/path/lib/hoister.js.map +1 -1
- package/lib/path/lib/removal-hooks.js +1 -8
- package/lib/path/lib/removal-hooks.js.map +1 -1
- package/lib/path/lib/virtual-types-validator.js +19 -42
- package/lib/path/lib/virtual-types-validator.js.map +1 -1
- package/lib/path/lib/virtual-types.js +18 -42
- package/lib/path/lib/virtual-types.js.map +1 -1
- package/lib/path/modification.js +20 -35
- package/lib/path/modification.js.map +1 -1
- package/lib/path/removal.js +13 -25
- package/lib/path/removal.js.map +1 -1
- package/lib/path/replacement.js +25 -38
- package/lib/path/replacement.js.map +1 -1
- package/lib/scope/binding.js +10 -17
- package/lib/scope/binding.js.map +1 -1
- package/lib/scope/index.js +49 -66
- package/lib/scope/index.js.map +1 -1
- package/lib/scope/lib/renamer.js +12 -20
- package/lib/scope/lib/renamer.js.map +1 -1
- package/lib/traverse-node.js +4 -10
- package/lib/traverse-node.js.map +1 -1
- package/lib/visitors.js +16 -24
- package/lib/visitors.js.map +1 -1
- package/package.json +17 -13
package/lib/index.js
CHANGED
@@ -1,35 +1,4849 @@
|
|
1
|
-
|
1
|
+
import * as _t from '@babel/types';
|
2
|
+
import buildDebug from 'debug';
|
3
|
+
import splitExportDeclaration from '@babel/helper-split-export-declaration';
|
4
|
+
import environmentVisitor, { requeueComputedKeyAndDecorators } from '@babel/helper-environment-visitor';
|
5
|
+
import globals from 'globals';
|
6
|
+
import generator from '@babel/generator';
|
7
|
+
import { codeFrameColumns } from '@babel/code-frame';
|
8
|
+
import { parse } from '@babel/parser';
|
9
|
+
import hoistVariables from '@babel/helper-hoist-variables';
|
10
|
+
import nameFunction from '@babel/helper-function-name';
|
2
11
|
|
3
|
-
|
4
|
-
|
12
|
+
const ReferencedIdentifier = ["Identifier", "JSXIdentifier"];
|
13
|
+
const ReferencedMemberExpression = ["MemberExpression"];
|
14
|
+
const BindingIdentifier = ["Identifier"];
|
15
|
+
const Statement = ["Statement"];
|
16
|
+
const Expression = ["Expression"];
|
17
|
+
const Scope$1 = ["Scopable", "Pattern"];
|
18
|
+
const Referenced = null;
|
19
|
+
const BlockScoped = null;
|
20
|
+
const Var = ["VariableDeclaration"];
|
21
|
+
const User = null;
|
22
|
+
const Generated = null;
|
23
|
+
const Pure = null;
|
24
|
+
const Flow = ["Flow", "ImportDeclaration", "ExportDeclaration", "ImportSpecifier"];
|
25
|
+
const RestProperty = ["RestElement"];
|
26
|
+
const SpreadProperty = ["RestElement"];
|
27
|
+
const ExistentialTypeParam = ["ExistsTypeAnnotation"];
|
28
|
+
const NumericLiteralTypeAnnotation = ["NumberLiteralTypeAnnotation"];
|
29
|
+
const ForAwaitStatement = ["ForOfStatement"];
|
30
|
+
|
31
|
+
var virtualTypes = /*#__PURE__*/Object.freeze({
|
32
|
+
__proto__: null,
|
33
|
+
ReferencedIdentifier: ReferencedIdentifier,
|
34
|
+
ReferencedMemberExpression: ReferencedMemberExpression,
|
35
|
+
BindingIdentifier: BindingIdentifier,
|
36
|
+
Statement: Statement,
|
37
|
+
Expression: Expression,
|
38
|
+
Scope: Scope$1,
|
39
|
+
Referenced: Referenced,
|
40
|
+
BlockScoped: BlockScoped,
|
41
|
+
Var: Var,
|
42
|
+
User: User,
|
43
|
+
Generated: Generated,
|
44
|
+
Pure: Pure,
|
45
|
+
Flow: Flow,
|
46
|
+
RestProperty: RestProperty,
|
47
|
+
SpreadProperty: SpreadProperty,
|
48
|
+
ExistentialTypeParam: ExistentialTypeParam,
|
49
|
+
NumericLiteralTypeAnnotation: NumericLiteralTypeAnnotation,
|
50
|
+
ForAwaitStatement: ForAwaitStatement
|
51
|
+
});
|
52
|
+
|
53
|
+
const {
|
54
|
+
DEPRECATED_KEYS,
|
55
|
+
DEPRECATED_ALIASES,
|
56
|
+
FLIPPED_ALIAS_KEYS,
|
57
|
+
TYPES,
|
58
|
+
__internal__deprecationWarning: deprecationWarning
|
59
|
+
} = _t;
|
60
|
+
function isVirtualType(type) {
|
61
|
+
return type in virtualTypes;
|
62
|
+
}
|
63
|
+
function isExplodedVisitor(visitor) {
|
64
|
+
return visitor?._exploded;
|
65
|
+
}
|
66
|
+
function explode(visitor) {
|
67
|
+
if (isExplodedVisitor(visitor)) return visitor;
|
68
|
+
visitor._exploded = true;
|
69
|
+
for (const nodeType of Object.keys(visitor)) {
|
70
|
+
if (shouldIgnoreKey(nodeType)) continue;
|
71
|
+
const parts = nodeType.split("|");
|
72
|
+
if (parts.length === 1) continue;
|
73
|
+
const fns = visitor[nodeType];
|
74
|
+
delete visitor[nodeType];
|
75
|
+
for (const part of parts) {
|
76
|
+
visitor[part] = fns;
|
77
|
+
}
|
78
|
+
}
|
79
|
+
verify(visitor);
|
80
|
+
delete visitor.__esModule;
|
81
|
+
ensureEntranceObjects(visitor);
|
82
|
+
ensureCallbackArrays(visitor);
|
83
|
+
for (const nodeType of Object.keys(visitor)) {
|
84
|
+
if (shouldIgnoreKey(nodeType)) continue;
|
85
|
+
if (!isVirtualType(nodeType)) continue;
|
86
|
+
const fns = visitor[nodeType];
|
87
|
+
for (const type of Object.keys(fns)) {
|
88
|
+
fns[type] = wrapCheck(nodeType, fns[type]);
|
89
|
+
}
|
90
|
+
delete visitor[nodeType];
|
91
|
+
const types = virtualTypes[nodeType];
|
92
|
+
if (types !== null) {
|
93
|
+
for (const type of types) {
|
94
|
+
if (visitor[type]) {
|
95
|
+
mergePair(visitor[type], fns);
|
96
|
+
} else {
|
97
|
+
visitor[type] = fns;
|
98
|
+
}
|
99
|
+
}
|
100
|
+
} else {
|
101
|
+
mergePair(visitor, fns);
|
102
|
+
}
|
103
|
+
}
|
104
|
+
for (const nodeType of Object.keys(visitor)) {
|
105
|
+
if (shouldIgnoreKey(nodeType)) continue;
|
106
|
+
let aliases = FLIPPED_ALIAS_KEYS[nodeType];
|
107
|
+
if (nodeType in DEPRECATED_KEYS) {
|
108
|
+
const deprecatedKey = DEPRECATED_KEYS[nodeType];
|
109
|
+
deprecationWarning(nodeType, deprecatedKey, "Visitor ");
|
110
|
+
aliases = [deprecatedKey];
|
111
|
+
} else if (nodeType in DEPRECATED_ALIASES) {
|
112
|
+
const deprecatedAlias = DEPRECATED_ALIASES[nodeType];
|
113
|
+
deprecationWarning(nodeType, deprecatedAlias, "Visitor ");
|
114
|
+
aliases = FLIPPED_ALIAS_KEYS[deprecatedAlias];
|
115
|
+
}
|
116
|
+
if (!aliases) continue;
|
117
|
+
const fns = visitor[nodeType];
|
118
|
+
delete visitor[nodeType];
|
119
|
+
for (const alias of aliases) {
|
120
|
+
const existing = visitor[alias];
|
121
|
+
if (existing) {
|
122
|
+
mergePair(existing, fns);
|
123
|
+
} else {
|
124
|
+
visitor[alias] = Object.assign({}, fns);
|
125
|
+
}
|
126
|
+
}
|
127
|
+
}
|
128
|
+
for (const nodeType of Object.keys(visitor)) {
|
129
|
+
if (shouldIgnoreKey(nodeType)) continue;
|
130
|
+
ensureCallbackArrays(visitor[nodeType]);
|
131
|
+
}
|
132
|
+
return visitor;
|
133
|
+
}
|
134
|
+
function verify(visitor) {
|
135
|
+
if (visitor._verified) return;
|
136
|
+
if (typeof visitor === "function") {
|
137
|
+
throw new Error("You passed `traverse()` a function when it expected a visitor object, " + "are you sure you didn't mean `{ enter: Function }`?");
|
138
|
+
}
|
139
|
+
for (const nodeType of Object.keys(visitor)) {
|
140
|
+
if (nodeType === "enter" || nodeType === "exit") {
|
141
|
+
validateVisitorMethods(nodeType, visitor[nodeType]);
|
142
|
+
}
|
143
|
+
if (shouldIgnoreKey(nodeType)) continue;
|
144
|
+
if (TYPES.indexOf(nodeType) < 0) {
|
145
|
+
throw new Error(`You gave us a visitor for the node type ${nodeType} but it's not a valid type`);
|
146
|
+
}
|
147
|
+
const visitors = visitor[nodeType];
|
148
|
+
if (typeof visitors === "object") {
|
149
|
+
for (const visitorKey of Object.keys(visitors)) {
|
150
|
+
if (visitorKey === "enter" || visitorKey === "exit") {
|
151
|
+
validateVisitorMethods(`${nodeType}.${visitorKey}`, visitors[visitorKey]);
|
152
|
+
} else {
|
153
|
+
throw new Error("You passed `traverse()` a visitor object with the property " + `${nodeType} that has the invalid property ${visitorKey}`);
|
154
|
+
}
|
155
|
+
}
|
156
|
+
}
|
157
|
+
}
|
158
|
+
visitor._verified = true;
|
159
|
+
}
|
160
|
+
function validateVisitorMethods(path, val) {
|
161
|
+
const fns = [].concat(val);
|
162
|
+
for (const fn of fns) {
|
163
|
+
if (typeof fn !== "function") {
|
164
|
+
throw new TypeError(`Non-function found defined in ${path} with type ${typeof fn}`);
|
165
|
+
}
|
166
|
+
}
|
167
|
+
}
|
168
|
+
function merge(visitors, states = [], wrapper) {
|
169
|
+
const mergedVisitor = {};
|
170
|
+
for (let i = 0; i < visitors.length; i++) {
|
171
|
+
const visitor = explode(visitors[i]);
|
172
|
+
const state = states[i];
|
173
|
+
let topVisitor = visitor;
|
174
|
+
if (state || wrapper) {
|
175
|
+
topVisitor = wrapWithStateOrWrapper(topVisitor, state, wrapper);
|
176
|
+
}
|
177
|
+
mergePair(mergedVisitor, topVisitor);
|
178
|
+
for (const key of Object.keys(visitor)) {
|
179
|
+
if (shouldIgnoreKey(key)) continue;
|
180
|
+
let typeVisitor = visitor[key];
|
181
|
+
if (state || wrapper) {
|
182
|
+
typeVisitor = wrapWithStateOrWrapper(typeVisitor, state, wrapper);
|
183
|
+
}
|
184
|
+
const nodeVisitor = mergedVisitor[key] ||= {};
|
185
|
+
mergePair(nodeVisitor, typeVisitor);
|
186
|
+
}
|
187
|
+
}
|
188
|
+
{
|
189
|
+
return Object.assign({}, mergedVisitor, {
|
190
|
+
_exploded: true,
|
191
|
+
_verified: true
|
192
|
+
});
|
193
|
+
}
|
194
|
+
}
|
195
|
+
function wrapWithStateOrWrapper(oldVisitor, state, wrapper) {
|
196
|
+
const newVisitor = {};
|
197
|
+
for (const phase of ["enter", "exit"]) {
|
198
|
+
let fns = oldVisitor[phase];
|
199
|
+
if (!Array.isArray(fns)) continue;
|
200
|
+
fns = fns.map(function (fn) {
|
201
|
+
let newFn = fn;
|
202
|
+
if (state) {
|
203
|
+
newFn = function (path) {
|
204
|
+
fn.call(state, path, state);
|
205
|
+
};
|
206
|
+
}
|
207
|
+
if (wrapper) {
|
208
|
+
newFn = wrapper(state?.key, phase, newFn);
|
209
|
+
}
|
210
|
+
if (newFn !== fn) {
|
211
|
+
newFn.toString = () => fn.toString();
|
212
|
+
}
|
213
|
+
return newFn;
|
214
|
+
});
|
215
|
+
newVisitor[phase] = fns;
|
216
|
+
}
|
217
|
+
return newVisitor;
|
218
|
+
}
|
219
|
+
function ensureEntranceObjects(obj) {
|
220
|
+
for (const key of Object.keys(obj)) {
|
221
|
+
if (shouldIgnoreKey(key)) continue;
|
222
|
+
const fns = obj[key];
|
223
|
+
if (typeof fns === "function") {
|
224
|
+
obj[key] = {
|
225
|
+
enter: fns
|
226
|
+
};
|
227
|
+
}
|
228
|
+
}
|
229
|
+
}
|
230
|
+
function ensureCallbackArrays(obj) {
|
231
|
+
if (obj.enter && !Array.isArray(obj.enter)) obj.enter = [obj.enter];
|
232
|
+
if (obj.exit && !Array.isArray(obj.exit)) obj.exit = [obj.exit];
|
233
|
+
}
|
234
|
+
function wrapCheck(nodeType, fn) {
|
235
|
+
const newFn = function (path) {
|
236
|
+
if (path[`is${nodeType}`]()) {
|
237
|
+
return fn.apply(this, arguments);
|
238
|
+
}
|
239
|
+
};
|
240
|
+
newFn.toString = () => fn.toString();
|
241
|
+
return newFn;
|
242
|
+
}
|
243
|
+
function shouldIgnoreKey(key) {
|
244
|
+
if (key[0] === "_") return true;
|
245
|
+
if (key === "enter" || key === "exit" || key === "shouldSkip") return true;
|
246
|
+
if (key === "denylist" || key === "noScope" || key === "skipKeys") {
|
247
|
+
return true;
|
248
|
+
}
|
249
|
+
return false;
|
250
|
+
}
|
251
|
+
function mergePair(dest, src) {
|
252
|
+
for (const phase of ["enter", "exit"]) {
|
253
|
+
if (!src[phase]) continue;
|
254
|
+
dest[phase] = [].concat(dest[phase] || [], src[phase]);
|
255
|
+
}
|
256
|
+
}
|
257
|
+
|
258
|
+
var visitors = /*#__PURE__*/Object.freeze({
|
259
|
+
__proto__: null,
|
260
|
+
isExplodedVisitor: isExplodedVisitor,
|
261
|
+
explode: explode,
|
262
|
+
verify: verify,
|
263
|
+
merge: merge
|
264
|
+
});
|
265
|
+
|
266
|
+
let path = new WeakMap();
|
267
|
+
let scope = new WeakMap();
|
268
|
+
function clear() {
|
269
|
+
clearPath();
|
270
|
+
clearScope();
|
271
|
+
}
|
272
|
+
function clearPath() {
|
273
|
+
path = new WeakMap();
|
274
|
+
}
|
275
|
+
function clearScope() {
|
276
|
+
scope = new WeakMap();
|
277
|
+
}
|
278
|
+
|
279
|
+
var cache = /*#__PURE__*/Object.freeze({
|
280
|
+
__proto__: null,
|
281
|
+
get path () { return path; },
|
282
|
+
get scope () { return scope; },
|
283
|
+
clear: clear,
|
284
|
+
clearPath: clearPath,
|
285
|
+
clearScope: clearScope
|
286
|
+
});
|
287
|
+
|
288
|
+
const renameVisitor = {
|
289
|
+
ReferencedIdentifier({
|
290
|
+
node
|
291
|
+
}, state) {
|
292
|
+
if (node.name === state.oldName) {
|
293
|
+
node.name = state.newName;
|
294
|
+
}
|
295
|
+
},
|
296
|
+
Scope(path, state) {
|
297
|
+
if (!path.scope.bindingIdentifierEquals(state.oldName, state.binding.identifier)) {
|
298
|
+
path.skip();
|
299
|
+
if (path.isMethod()) {
|
300
|
+
requeueComputedKeyAndDecorators(path);
|
301
|
+
}
|
302
|
+
}
|
303
|
+
},
|
304
|
+
ObjectProperty({
|
305
|
+
node,
|
306
|
+
scope
|
307
|
+
}, state) {
|
308
|
+
const {
|
309
|
+
name
|
310
|
+
} = node.key;
|
311
|
+
if (node.shorthand && (name === state.oldName || name === state.newName) && scope.getBindingIdentifier(name) === state.binding.identifier) {
|
312
|
+
node.shorthand = false;
|
313
|
+
if (node.extra?.shorthand) node.extra.shorthand = false;
|
314
|
+
}
|
315
|
+
},
|
316
|
+
"AssignmentExpression|Declaration|VariableDeclarator"(path, state) {
|
317
|
+
if (path.isVariableDeclaration()) return;
|
318
|
+
const ids = path.getOuterBindingIdentifiers();
|
319
|
+
for (const name in ids) {
|
320
|
+
if (name === state.oldName) ids[name].name = state.newName;
|
321
|
+
}
|
322
|
+
}
|
323
|
+
};
|
324
|
+
class Renamer {
|
325
|
+
constructor(binding, oldName, newName) {
|
326
|
+
this.newName = newName;
|
327
|
+
this.oldName = oldName;
|
328
|
+
this.binding = binding;
|
329
|
+
}
|
330
|
+
maybeConvertFromExportDeclaration(parentDeclar) {
|
331
|
+
const maybeExportDeclar = parentDeclar.parentPath;
|
332
|
+
if (!maybeExportDeclar.isExportDeclaration()) {
|
333
|
+
return;
|
334
|
+
}
|
335
|
+
if (maybeExportDeclar.isExportDefaultDeclaration()) {
|
336
|
+
const {
|
337
|
+
declaration
|
338
|
+
} = maybeExportDeclar.node;
|
339
|
+
if (_t.isDeclaration(declaration) && !declaration.id) {
|
340
|
+
return;
|
341
|
+
}
|
342
|
+
}
|
343
|
+
if (maybeExportDeclar.isExportAllDeclaration()) {
|
344
|
+
return;
|
345
|
+
}
|
346
|
+
splitExportDeclaration(maybeExportDeclar);
|
347
|
+
}
|
348
|
+
maybeConvertFromClassFunctionDeclaration(path) {
|
349
|
+
return path;
|
350
|
+
}
|
351
|
+
maybeConvertFromClassFunctionExpression(path) {
|
352
|
+
return path;
|
353
|
+
}
|
354
|
+
rename() {
|
355
|
+
const {
|
356
|
+
binding,
|
357
|
+
oldName,
|
358
|
+
newName
|
359
|
+
} = this;
|
360
|
+
const {
|
361
|
+
scope,
|
362
|
+
path
|
363
|
+
} = binding;
|
364
|
+
const parentDeclar = path.find(path => path.isDeclaration() || path.isFunctionExpression() || path.isClassExpression());
|
365
|
+
if (parentDeclar) {
|
366
|
+
const bindingIds = parentDeclar.getOuterBindingIdentifiers();
|
367
|
+
if (bindingIds[oldName] === binding.identifier) {
|
368
|
+
this.maybeConvertFromExportDeclaration(parentDeclar);
|
369
|
+
}
|
370
|
+
}
|
371
|
+
const blockToTraverse = scope.block;
|
372
|
+
traverseNode(blockToTraverse, explode(renameVisitor), scope, this, scope.path, {
|
373
|
+
discriminant: true
|
374
|
+
});
|
375
|
+
{
|
376
|
+
scope.removeOwnBinding(oldName);
|
377
|
+
scope.bindings[newName] = binding;
|
378
|
+
this.binding.identifier.name = newName;
|
379
|
+
}
|
380
|
+
if (parentDeclar) {
|
381
|
+
this.maybeConvertFromClassFunctionDeclaration(path);
|
382
|
+
this.maybeConvertFromClassFunctionExpression(path);
|
383
|
+
}
|
384
|
+
}
|
385
|
+
}
|
386
|
+
|
387
|
+
class Binding {
|
388
|
+
identifier;
|
389
|
+
scope;
|
390
|
+
path;
|
391
|
+
kind;
|
392
|
+
constructor({
|
393
|
+
identifier,
|
394
|
+
scope,
|
395
|
+
path,
|
396
|
+
kind
|
397
|
+
}) {
|
398
|
+
this.identifier = identifier;
|
399
|
+
this.scope = scope;
|
400
|
+
this.path = path;
|
401
|
+
this.kind = kind;
|
402
|
+
if ((kind === "var" || kind === "hoisted") && isDeclaredInLoop(path || (() => {
|
403
|
+
throw new Error("Internal Babel error: unreachable ");
|
404
|
+
})())) {
|
405
|
+
this.reassign(path);
|
406
|
+
}
|
407
|
+
this.clearValue();
|
408
|
+
}
|
409
|
+
constantViolations = [];
|
410
|
+
constant = true;
|
411
|
+
referencePaths = [];
|
412
|
+
referenced = false;
|
413
|
+
references = 0;
|
414
|
+
deoptValue() {
|
415
|
+
this.clearValue();
|
416
|
+
this.hasDeoptedValue = true;
|
417
|
+
}
|
418
|
+
setValue(value) {
|
419
|
+
if (this.hasDeoptedValue) return;
|
420
|
+
this.hasValue = true;
|
421
|
+
this.value = value;
|
422
|
+
}
|
423
|
+
clearValue() {
|
424
|
+
this.hasDeoptedValue = false;
|
425
|
+
this.hasValue = false;
|
426
|
+
this.value = null;
|
427
|
+
}
|
428
|
+
reassign(path) {
|
429
|
+
this.constant = false;
|
430
|
+
if (this.constantViolations.indexOf(path) !== -1) {
|
431
|
+
return;
|
432
|
+
}
|
433
|
+
this.constantViolations.push(path);
|
434
|
+
}
|
435
|
+
reference(path) {
|
436
|
+
if (this.referencePaths.indexOf(path) !== -1) {
|
437
|
+
return;
|
438
|
+
}
|
439
|
+
this.referenced = true;
|
440
|
+
this.references++;
|
441
|
+
this.referencePaths.push(path);
|
442
|
+
}
|
443
|
+
dereference() {
|
444
|
+
this.references--;
|
445
|
+
this.referenced = !!this.references;
|
446
|
+
}
|
447
|
+
}
|
448
|
+
function isDeclaredInLoop(path) {
|
449
|
+
for (let {
|
450
|
+
parentPath,
|
451
|
+
key
|
452
|
+
} = path; parentPath; ({
|
453
|
+
parentPath,
|
454
|
+
key
|
455
|
+
} = parentPath)) {
|
456
|
+
if (parentPath.isFunctionParent()) return false;
|
457
|
+
if (parentPath.isWhile() || parentPath.isForXStatement() || parentPath.isForStatement() && key === "body") {
|
458
|
+
return true;
|
459
|
+
}
|
460
|
+
}
|
461
|
+
return false;
|
462
|
+
}
|
463
|
+
|
464
|
+
const {
|
465
|
+
NOT_LOCAL_BINDING,
|
466
|
+
callExpression: callExpression$3,
|
467
|
+
cloneNode: cloneNode$3,
|
468
|
+
getBindingIdentifiers: getBindingIdentifiers$1,
|
469
|
+
identifier: identifier$3,
|
470
|
+
isArrayExpression,
|
471
|
+
isBinary,
|
472
|
+
isClass,
|
473
|
+
isClassBody,
|
474
|
+
isClassDeclaration,
|
475
|
+
isExportAllDeclaration,
|
476
|
+
isExportDefaultDeclaration,
|
477
|
+
isExportNamedDeclaration: isExportNamedDeclaration$1,
|
478
|
+
isFunctionDeclaration,
|
479
|
+
isIdentifier: isIdentifier$6,
|
480
|
+
isImportDeclaration: isImportDeclaration$1,
|
481
|
+
isLiteral: isLiteral$1,
|
482
|
+
isMethod,
|
483
|
+
isModuleSpecifier,
|
484
|
+
isNullLiteral,
|
485
|
+
isObjectExpression,
|
486
|
+
isProperty,
|
487
|
+
isPureish,
|
488
|
+
isRegExpLiteral,
|
489
|
+
isSuper: isSuper$1,
|
490
|
+
isTaggedTemplateExpression,
|
491
|
+
isTemplateLiteral,
|
492
|
+
isThisExpression,
|
493
|
+
isUnaryExpression,
|
494
|
+
isVariableDeclaration: isVariableDeclaration$1,
|
495
|
+
matchesPattern: matchesPattern$1,
|
496
|
+
memberExpression: memberExpression$1,
|
497
|
+
numericLiteral: numericLiteral$2,
|
498
|
+
toIdentifier,
|
499
|
+
unaryExpression: unaryExpression$2,
|
500
|
+
variableDeclaration: variableDeclaration$1,
|
501
|
+
variableDeclarator: variableDeclarator$1,
|
502
|
+
isRecordExpression,
|
503
|
+
isTupleExpression,
|
504
|
+
isObjectProperty,
|
505
|
+
isTopicReference,
|
506
|
+
isMetaProperty,
|
507
|
+
isPrivateName,
|
508
|
+
isExportDeclaration: isExportDeclaration$1
|
509
|
+
} = _t;
|
510
|
+
function gatherNodeParts(node, parts) {
|
511
|
+
switch (node?.type) {
|
512
|
+
default:
|
513
|
+
if (isImportDeclaration$1(node) || isExportDeclaration$1(node)) {
|
514
|
+
if ((isExportAllDeclaration(node) || isExportNamedDeclaration$1(node) || isImportDeclaration$1(node)) && node.source) {
|
515
|
+
gatherNodeParts(node.source, parts);
|
516
|
+
} else if ((isExportNamedDeclaration$1(node) || isImportDeclaration$1(node)) && node.specifiers?.length) {
|
517
|
+
for (const e of node.specifiers) gatherNodeParts(e, parts);
|
518
|
+
} else if ((isExportDefaultDeclaration(node) || isExportNamedDeclaration$1(node)) && node.declaration) {
|
519
|
+
gatherNodeParts(node.declaration, parts);
|
520
|
+
}
|
521
|
+
} else if (isModuleSpecifier(node)) {
|
522
|
+
gatherNodeParts(node.local, parts);
|
523
|
+
} else if (isLiteral$1(node) && !isNullLiteral(node) && !isRegExpLiteral(node) && !isTemplateLiteral(node)) {
|
524
|
+
parts.push(node.value);
|
525
|
+
}
|
526
|
+
break;
|
527
|
+
case "MemberExpression":
|
528
|
+
case "OptionalMemberExpression":
|
529
|
+
case "JSXMemberExpression":
|
530
|
+
gatherNodeParts(node.object, parts);
|
531
|
+
gatherNodeParts(node.property, parts);
|
532
|
+
break;
|
533
|
+
case "Identifier":
|
534
|
+
case "JSXIdentifier":
|
535
|
+
parts.push(node.name);
|
536
|
+
break;
|
537
|
+
case "CallExpression":
|
538
|
+
case "OptionalCallExpression":
|
539
|
+
case "NewExpression":
|
540
|
+
gatherNodeParts(node.callee, parts);
|
541
|
+
break;
|
542
|
+
case "ObjectExpression":
|
543
|
+
case "ObjectPattern":
|
544
|
+
for (const e of node.properties) {
|
545
|
+
gatherNodeParts(e, parts);
|
546
|
+
}
|
547
|
+
break;
|
548
|
+
case "SpreadElement":
|
549
|
+
case "RestElement":
|
550
|
+
gatherNodeParts(node.argument, parts);
|
551
|
+
break;
|
552
|
+
case "ObjectProperty":
|
553
|
+
case "ObjectMethod":
|
554
|
+
case "ClassProperty":
|
555
|
+
case "ClassMethod":
|
556
|
+
case "ClassPrivateProperty":
|
557
|
+
case "ClassPrivateMethod":
|
558
|
+
gatherNodeParts(node.key, parts);
|
559
|
+
break;
|
560
|
+
case "ThisExpression":
|
561
|
+
parts.push("this");
|
562
|
+
break;
|
563
|
+
case "Super":
|
564
|
+
parts.push("super");
|
565
|
+
break;
|
566
|
+
case "Import":
|
567
|
+
parts.push("import");
|
568
|
+
break;
|
569
|
+
case "DoExpression":
|
570
|
+
parts.push("do");
|
571
|
+
break;
|
572
|
+
case "YieldExpression":
|
573
|
+
parts.push("yield");
|
574
|
+
gatherNodeParts(node.argument, parts);
|
575
|
+
break;
|
576
|
+
case "AwaitExpression":
|
577
|
+
parts.push("await");
|
578
|
+
gatherNodeParts(node.argument, parts);
|
579
|
+
break;
|
580
|
+
case "AssignmentExpression":
|
581
|
+
gatherNodeParts(node.left, parts);
|
582
|
+
break;
|
583
|
+
case "VariableDeclarator":
|
584
|
+
gatherNodeParts(node.id, parts);
|
585
|
+
break;
|
586
|
+
case "FunctionExpression":
|
587
|
+
case "FunctionDeclaration":
|
588
|
+
case "ClassExpression":
|
589
|
+
case "ClassDeclaration":
|
590
|
+
gatherNodeParts(node.id, parts);
|
591
|
+
break;
|
592
|
+
case "PrivateName":
|
593
|
+
gatherNodeParts(node.id, parts);
|
594
|
+
break;
|
595
|
+
case "ParenthesizedExpression":
|
596
|
+
gatherNodeParts(node.expression, parts);
|
597
|
+
break;
|
598
|
+
case "UnaryExpression":
|
599
|
+
case "UpdateExpression":
|
600
|
+
gatherNodeParts(node.argument, parts);
|
601
|
+
break;
|
602
|
+
case "MetaProperty":
|
603
|
+
gatherNodeParts(node.meta, parts);
|
604
|
+
gatherNodeParts(node.property, parts);
|
605
|
+
break;
|
606
|
+
case "JSXElement":
|
607
|
+
gatherNodeParts(node.openingElement, parts);
|
608
|
+
break;
|
609
|
+
case "JSXOpeningElement":
|
610
|
+
gatherNodeParts(node.name, parts);
|
611
|
+
break;
|
612
|
+
case "JSXFragment":
|
613
|
+
gatherNodeParts(node.openingFragment, parts);
|
614
|
+
break;
|
615
|
+
case "JSXOpeningFragment":
|
616
|
+
parts.push("Fragment");
|
617
|
+
break;
|
618
|
+
case "JSXNamespacedName":
|
619
|
+
gatherNodeParts(node.namespace, parts);
|
620
|
+
gatherNodeParts(node.name, parts);
|
621
|
+
break;
|
622
|
+
}
|
623
|
+
}
|
624
|
+
const collectorVisitor = {
|
625
|
+
ForStatement(path) {
|
626
|
+
const declar = path.get("init");
|
627
|
+
if (declar.isVar()) {
|
628
|
+
const {
|
629
|
+
scope
|
630
|
+
} = path;
|
631
|
+
const parentScope = scope.getFunctionParent() || scope.getProgramParent();
|
632
|
+
parentScope.registerBinding("var", declar);
|
633
|
+
}
|
634
|
+
},
|
635
|
+
Declaration(path) {
|
636
|
+
if (path.isBlockScoped()) return;
|
637
|
+
if (path.isImportDeclaration()) return;
|
638
|
+
if (path.isExportDeclaration()) return;
|
639
|
+
const parent = path.scope.getFunctionParent() || path.scope.getProgramParent();
|
640
|
+
parent.registerDeclaration(path);
|
641
|
+
},
|
642
|
+
ImportDeclaration(path) {
|
643
|
+
const parent = path.scope.getBlockParent();
|
644
|
+
parent.registerDeclaration(path);
|
645
|
+
},
|
646
|
+
ReferencedIdentifier(path, state) {
|
647
|
+
state.references.push(path);
|
648
|
+
},
|
649
|
+
ForXStatement(path, state) {
|
650
|
+
const left = path.get("left");
|
651
|
+
if (left.isPattern() || left.isIdentifier()) {
|
652
|
+
state.constantViolations.push(path);
|
653
|
+
} else if (left.isVar()) {
|
654
|
+
const {
|
655
|
+
scope
|
656
|
+
} = path;
|
657
|
+
const parentScope = scope.getFunctionParent() || scope.getProgramParent();
|
658
|
+
parentScope.registerBinding("var", left);
|
659
|
+
}
|
660
|
+
},
|
661
|
+
ExportDeclaration: {
|
662
|
+
exit(path) {
|
663
|
+
const {
|
664
|
+
node,
|
665
|
+
scope
|
666
|
+
} = path;
|
667
|
+
if (isExportAllDeclaration(node)) return;
|
668
|
+
const declar = node.declaration;
|
669
|
+
if (isClassDeclaration(declar) || isFunctionDeclaration(declar)) {
|
670
|
+
const id = declar.id;
|
671
|
+
if (!id) return;
|
672
|
+
const binding = scope.getBinding(id.name);
|
673
|
+
binding?.reference(path);
|
674
|
+
} else if (isVariableDeclaration$1(declar)) {
|
675
|
+
for (const decl of declar.declarations) {
|
676
|
+
for (const name of Object.keys(getBindingIdentifiers$1(decl))) {
|
677
|
+
const binding = scope.getBinding(name);
|
678
|
+
binding?.reference(path);
|
679
|
+
}
|
680
|
+
}
|
681
|
+
}
|
682
|
+
}
|
683
|
+
},
|
684
|
+
LabeledStatement(path) {
|
685
|
+
path.scope.getBlockParent().registerDeclaration(path);
|
686
|
+
},
|
687
|
+
AssignmentExpression(path, state) {
|
688
|
+
state.assignments.push(path);
|
689
|
+
},
|
690
|
+
UpdateExpression(path, state) {
|
691
|
+
state.constantViolations.push(path);
|
692
|
+
},
|
693
|
+
UnaryExpression(path, state) {
|
694
|
+
if (path.node.operator === "delete") {
|
695
|
+
state.constantViolations.push(path);
|
696
|
+
}
|
697
|
+
},
|
698
|
+
BlockScoped(path) {
|
699
|
+
let scope = path.scope;
|
700
|
+
if (scope.path === path) scope = scope.parent;
|
701
|
+
const parent = scope.getBlockParent();
|
702
|
+
parent.registerDeclaration(path);
|
703
|
+
if (path.isClassDeclaration() && path.node.id) {
|
704
|
+
const id = path.node.id;
|
705
|
+
const name = id.name;
|
706
|
+
path.scope.bindings[name] = path.scope.parent.getBinding(name);
|
707
|
+
}
|
708
|
+
},
|
709
|
+
CatchClause(path) {
|
710
|
+
path.scope.registerBinding("let", path);
|
711
|
+
},
|
712
|
+
Function(path) {
|
713
|
+
const params = path.get("params");
|
714
|
+
for (const param of params) {
|
715
|
+
path.scope.registerBinding("param", param);
|
716
|
+
}
|
717
|
+
if (path.isFunctionExpression() && path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
|
718
|
+
path.scope.registerBinding("local", path.get("id"), path);
|
719
|
+
}
|
720
|
+
},
|
721
|
+
ClassExpression(path) {
|
722
|
+
if (path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
|
723
|
+
path.scope.registerBinding("local", path);
|
724
|
+
}
|
725
|
+
}
|
726
|
+
};
|
727
|
+
let uid = 0;
|
728
|
+
class Scope {
|
729
|
+
uid;
|
730
|
+
path;
|
731
|
+
block;
|
732
|
+
labels;
|
733
|
+
inited;
|
734
|
+
bindings;
|
735
|
+
references;
|
736
|
+
globals;
|
737
|
+
uids;
|
738
|
+
data;
|
739
|
+
crawling;
|
740
|
+
constructor(path) {
|
741
|
+
const {
|
742
|
+
node
|
743
|
+
} = path;
|
744
|
+
const cached = scope.get(node);
|
745
|
+
if (cached?.path === path) {
|
746
|
+
return cached;
|
747
|
+
}
|
748
|
+
scope.set(node, this);
|
749
|
+
this.uid = uid++;
|
750
|
+
this.block = node;
|
751
|
+
this.path = path;
|
752
|
+
this.labels = new Map();
|
753
|
+
this.inited = false;
|
754
|
+
}
|
755
|
+
static globals = Object.keys(globals.builtin);
|
756
|
+
static contextVariables = ["arguments", "undefined", "Infinity", "NaN"];
|
757
|
+
get parent() {
|
758
|
+
let parent,
|
759
|
+
path = this.path;
|
760
|
+
do {
|
761
|
+
const shouldSkip = path.key === "key" || path.listKey === "decorators";
|
762
|
+
path = path.parentPath;
|
763
|
+
if (shouldSkip && path.isMethod()) path = path.parentPath;
|
764
|
+
if (path && path.isScope()) parent = path;
|
765
|
+
} while (path && !parent);
|
766
|
+
return parent?.scope;
|
767
|
+
}
|
768
|
+
get parentBlock() {
|
769
|
+
return this.path.parent;
|
770
|
+
}
|
771
|
+
get hub() {
|
772
|
+
return this.path.hub;
|
773
|
+
}
|
774
|
+
traverse(node, opts, state) {
|
775
|
+
traverse(node, opts, this, state, this.path);
|
776
|
+
}
|
777
|
+
generateDeclaredUidIdentifier(name) {
|
778
|
+
const id = this.generateUidIdentifier(name);
|
779
|
+
this.push({
|
780
|
+
id
|
781
|
+
});
|
782
|
+
return cloneNode$3(id);
|
783
|
+
}
|
784
|
+
generateUidIdentifier(name) {
|
785
|
+
return identifier$3(this.generateUid(name));
|
786
|
+
}
|
787
|
+
generateUid(name = "temp") {
|
788
|
+
name = toIdentifier(name).replace(/^_+/, "").replace(/[0-9]+$/g, "");
|
789
|
+
let uid;
|
790
|
+
let i = 1;
|
791
|
+
do {
|
792
|
+
uid = this._generateUid(name, i);
|
793
|
+
i++;
|
794
|
+
} while (this.hasLabel(uid) || this.hasBinding(uid) || this.hasGlobal(uid) || this.hasReference(uid));
|
795
|
+
const program = this.getProgramParent();
|
796
|
+
program.references[uid] = true;
|
797
|
+
program.uids[uid] = true;
|
798
|
+
return uid;
|
799
|
+
}
|
800
|
+
_generateUid(name, i) {
|
801
|
+
let id = name;
|
802
|
+
if (i > 1) id += i;
|
803
|
+
return `_${id}`;
|
804
|
+
}
|
805
|
+
generateUidBasedOnNode(node, defaultName) {
|
806
|
+
const parts = [];
|
807
|
+
gatherNodeParts(node, parts);
|
808
|
+
let id = parts.join("$");
|
809
|
+
id = id.replace(/^_/, "") || defaultName || "ref";
|
810
|
+
return this.generateUid(id.slice(0, 20));
|
811
|
+
}
|
812
|
+
generateUidIdentifierBasedOnNode(node, defaultName) {
|
813
|
+
return identifier$3(this.generateUidBasedOnNode(node, defaultName));
|
814
|
+
}
|
815
|
+
isStatic(node) {
|
816
|
+
if (isThisExpression(node) || isSuper$1(node) || isTopicReference(node)) {
|
817
|
+
return true;
|
818
|
+
}
|
819
|
+
if (isIdentifier$6(node)) {
|
820
|
+
const binding = this.getBinding(node.name);
|
821
|
+
if (binding) {
|
822
|
+
return binding.constant;
|
823
|
+
} else {
|
824
|
+
return this.hasBinding(node.name);
|
825
|
+
}
|
826
|
+
}
|
827
|
+
return false;
|
828
|
+
}
|
829
|
+
maybeGenerateMemoised(node, dontPush) {
|
830
|
+
if (this.isStatic(node)) {
|
831
|
+
return null;
|
832
|
+
} else {
|
833
|
+
const id = this.generateUidIdentifierBasedOnNode(node);
|
834
|
+
if (!dontPush) {
|
835
|
+
this.push({
|
836
|
+
id
|
837
|
+
});
|
838
|
+
return cloneNode$3(id);
|
839
|
+
}
|
840
|
+
return id;
|
841
|
+
}
|
842
|
+
}
|
843
|
+
checkBlockScopedCollisions(local, kind, name, id) {
|
844
|
+
if (kind === "param") return;
|
845
|
+
if (local.kind === "local") return;
|
846
|
+
const duplicate = kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module" || local.kind === "param" && kind === "const";
|
847
|
+
if (duplicate) {
|
848
|
+
throw this.hub.buildError(id, `Duplicate declaration "${name}"`, TypeError);
|
849
|
+
}
|
850
|
+
}
|
851
|
+
rename(oldName, newName) {
|
852
|
+
const binding = this.getBinding(oldName);
|
853
|
+
if (binding) {
|
854
|
+
newName ||= this.generateUidIdentifier(oldName).name;
|
855
|
+
const renamer = new Renamer(binding, oldName, newName);
|
856
|
+
{
|
857
|
+
renamer.rename();
|
858
|
+
}
|
859
|
+
}
|
860
|
+
}
|
861
|
+
_renameFromMap(map, oldName, newName, value) {
|
862
|
+
if (map[oldName]) {
|
863
|
+
map[newName] = value;
|
864
|
+
map[oldName] = null;
|
865
|
+
}
|
866
|
+
}
|
867
|
+
dump() {
|
868
|
+
const sep = "-".repeat(60);
|
869
|
+
console.log(sep);
|
870
|
+
let scope = this;
|
871
|
+
do {
|
872
|
+
console.log("#", scope.block.type);
|
873
|
+
for (const name of Object.keys(scope.bindings)) {
|
874
|
+
const binding = scope.bindings[name];
|
875
|
+
console.log(" -", name, {
|
876
|
+
constant: binding.constant,
|
877
|
+
references: binding.references,
|
878
|
+
violations: binding.constantViolations.length,
|
879
|
+
kind: binding.kind
|
880
|
+
});
|
881
|
+
}
|
882
|
+
} while (scope = scope.parent);
|
883
|
+
console.log(sep);
|
884
|
+
}
|
885
|
+
toArray(node, i, arrayLikeIsIterable) {
|
886
|
+
if (isIdentifier$6(node)) {
|
887
|
+
const binding = this.getBinding(node.name);
|
888
|
+
if (binding?.constant && binding.path.isGenericType("Array")) {
|
889
|
+
return node;
|
890
|
+
}
|
891
|
+
}
|
892
|
+
if (isArrayExpression(node)) {
|
893
|
+
return node;
|
894
|
+
}
|
895
|
+
if (isIdentifier$6(node, {
|
896
|
+
name: "arguments"
|
897
|
+
})) {
|
898
|
+
return callExpression$3(memberExpression$1(memberExpression$1(memberExpression$1(identifier$3("Array"), identifier$3("prototype")), identifier$3("slice")), identifier$3("call")), [node]);
|
899
|
+
}
|
900
|
+
let helperName;
|
901
|
+
const args = [node];
|
902
|
+
if (i === true) {
|
903
|
+
helperName = "toConsumableArray";
|
904
|
+
} else if (typeof i === "number") {
|
905
|
+
args.push(numericLiteral$2(i));
|
906
|
+
helperName = "slicedToArray";
|
907
|
+
} else {
|
908
|
+
helperName = "toArray";
|
909
|
+
}
|
910
|
+
if (arrayLikeIsIterable) {
|
911
|
+
args.unshift(this.hub.addHelper(helperName));
|
912
|
+
helperName = "maybeArrayLike";
|
913
|
+
}
|
914
|
+
return callExpression$3(this.hub.addHelper(helperName), args);
|
915
|
+
}
|
916
|
+
hasLabel(name) {
|
917
|
+
return !!this.getLabel(name);
|
918
|
+
}
|
919
|
+
getLabel(name) {
|
920
|
+
return this.labels.get(name);
|
921
|
+
}
|
922
|
+
registerLabel(path) {
|
923
|
+
this.labels.set(path.node.label.name, path);
|
924
|
+
}
|
925
|
+
registerDeclaration(path) {
|
926
|
+
if (path.isLabeledStatement()) {
|
927
|
+
this.registerLabel(path);
|
928
|
+
} else if (path.isFunctionDeclaration()) {
|
929
|
+
this.registerBinding("hoisted", path.get("id"), path);
|
930
|
+
} else if (path.isVariableDeclaration()) {
|
931
|
+
const declarations = path.get("declarations");
|
932
|
+
const {
|
933
|
+
kind
|
934
|
+
} = path.node;
|
935
|
+
for (const declar of declarations) {
|
936
|
+
this.registerBinding(kind === "using" || kind === "await using" ? "const" : kind, declar);
|
937
|
+
}
|
938
|
+
} else if (path.isClassDeclaration()) {
|
939
|
+
if (path.node.declare) return;
|
940
|
+
this.registerBinding("let", path);
|
941
|
+
} else if (path.isImportDeclaration()) {
|
942
|
+
const isTypeDeclaration = path.node.importKind === "type" || path.node.importKind === "typeof";
|
943
|
+
const specifiers = path.get("specifiers");
|
944
|
+
for (const specifier of specifiers) {
|
945
|
+
const isTypeSpecifier = isTypeDeclaration || specifier.isImportSpecifier() && (specifier.node.importKind === "type" || specifier.node.importKind === "typeof");
|
946
|
+
this.registerBinding(isTypeSpecifier ? "unknown" : "module", specifier);
|
947
|
+
}
|
948
|
+
} else if (path.isExportDeclaration()) {
|
949
|
+
const declar = path.get("declaration");
|
950
|
+
if (declar.isClassDeclaration() || declar.isFunctionDeclaration() || declar.isVariableDeclaration()) {
|
951
|
+
this.registerDeclaration(declar);
|
952
|
+
}
|
953
|
+
} else {
|
954
|
+
this.registerBinding("unknown", path);
|
955
|
+
}
|
956
|
+
}
|
957
|
+
buildUndefinedNode() {
|
958
|
+
return unaryExpression$2("void", numericLiteral$2(0), true);
|
959
|
+
}
|
960
|
+
registerConstantViolation(path) {
|
961
|
+
const ids = path.getBindingIdentifiers();
|
962
|
+
for (const name of Object.keys(ids)) {
|
963
|
+
this.getBinding(name)?.reassign(path);
|
964
|
+
}
|
965
|
+
}
|
966
|
+
registerBinding(kind, path, bindingPath = path) {
|
967
|
+
if (!kind) throw new ReferenceError("no `kind`");
|
968
|
+
if (path.isVariableDeclaration()) {
|
969
|
+
const declarators = path.get("declarations");
|
970
|
+
for (const declar of declarators) {
|
971
|
+
this.registerBinding(kind, declar);
|
972
|
+
}
|
973
|
+
return;
|
974
|
+
}
|
975
|
+
const parent = this.getProgramParent();
|
976
|
+
const ids = path.getOuterBindingIdentifiers(true);
|
977
|
+
for (const name of Object.keys(ids)) {
|
978
|
+
parent.references[name] = true;
|
979
|
+
for (const id of ids[name]) {
|
980
|
+
const local = this.getOwnBinding(name);
|
981
|
+
if (local) {
|
982
|
+
if (local.identifier === id) continue;
|
983
|
+
this.checkBlockScopedCollisions(local, kind, name, id);
|
984
|
+
}
|
985
|
+
if (local) {
|
986
|
+
this.registerConstantViolation(bindingPath);
|
987
|
+
} else {
|
988
|
+
this.bindings[name] = new Binding({
|
989
|
+
identifier: id,
|
990
|
+
scope: this,
|
991
|
+
path: bindingPath,
|
992
|
+
kind: kind
|
993
|
+
});
|
994
|
+
}
|
995
|
+
}
|
996
|
+
}
|
997
|
+
}
|
998
|
+
addGlobal(node) {
|
999
|
+
this.globals[node.name] = node;
|
1000
|
+
}
|
1001
|
+
hasUid(name) {
|
1002
|
+
let scope = this;
|
1003
|
+
do {
|
1004
|
+
if (scope.uids[name]) return true;
|
1005
|
+
} while (scope = scope.parent);
|
1006
|
+
return false;
|
1007
|
+
}
|
1008
|
+
hasGlobal(name) {
|
1009
|
+
let scope = this;
|
1010
|
+
do {
|
1011
|
+
if (scope.globals[name]) return true;
|
1012
|
+
} while (scope = scope.parent);
|
1013
|
+
return false;
|
1014
|
+
}
|
1015
|
+
hasReference(name) {
|
1016
|
+
return !!this.getProgramParent().references[name];
|
1017
|
+
}
|
1018
|
+
isPure(node, constantsOnly) {
|
1019
|
+
if (isIdentifier$6(node)) {
|
1020
|
+
const binding = this.getBinding(node.name);
|
1021
|
+
if (!binding) return false;
|
1022
|
+
if (constantsOnly) return binding.constant;
|
1023
|
+
return true;
|
1024
|
+
} else if (isThisExpression(node) || isMetaProperty(node) || isTopicReference(node) || isPrivateName(node)) {
|
1025
|
+
return true;
|
1026
|
+
} else if (isClass(node)) {
|
1027
|
+
if (node.superClass && !this.isPure(node.superClass, constantsOnly)) {
|
1028
|
+
return false;
|
1029
|
+
}
|
1030
|
+
if (node.decorators?.length > 0) {
|
1031
|
+
return false;
|
1032
|
+
}
|
1033
|
+
return this.isPure(node.body, constantsOnly);
|
1034
|
+
} else if (isClassBody(node)) {
|
1035
|
+
for (const method of node.body) {
|
1036
|
+
if (!this.isPure(method, constantsOnly)) return false;
|
1037
|
+
}
|
1038
|
+
return true;
|
1039
|
+
} else if (isBinary(node)) {
|
1040
|
+
return this.isPure(node.left, constantsOnly) && this.isPure(node.right, constantsOnly);
|
1041
|
+
} else if (isArrayExpression(node) || isTupleExpression(node)) {
|
1042
|
+
for (const elem of node.elements) {
|
1043
|
+
if (elem !== null && !this.isPure(elem, constantsOnly)) return false;
|
1044
|
+
}
|
1045
|
+
return true;
|
1046
|
+
} else if (isObjectExpression(node) || isRecordExpression(node)) {
|
1047
|
+
for (const prop of node.properties) {
|
1048
|
+
if (!this.isPure(prop, constantsOnly)) return false;
|
1049
|
+
}
|
1050
|
+
return true;
|
1051
|
+
} else if (isMethod(node)) {
|
1052
|
+
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
|
1053
|
+
if (node.decorators?.length > 0) {
|
1054
|
+
return false;
|
1055
|
+
}
|
1056
|
+
return true;
|
1057
|
+
} else if (isProperty(node)) {
|
1058
|
+
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
|
1059
|
+
if (node.decorators?.length > 0) {
|
1060
|
+
return false;
|
1061
|
+
}
|
1062
|
+
if (isObjectProperty(node) || node.static) {
|
1063
|
+
if (node.value !== null && !this.isPure(node.value, constantsOnly)) {
|
1064
|
+
return false;
|
1065
|
+
}
|
1066
|
+
}
|
1067
|
+
return true;
|
1068
|
+
} else if (isUnaryExpression(node)) {
|
1069
|
+
return this.isPure(node.argument, constantsOnly);
|
1070
|
+
} else if (isTaggedTemplateExpression(node)) {
|
1071
|
+
return matchesPattern$1(node.tag, "String.raw") && !this.hasBinding("String", true) && this.isPure(node.quasi, constantsOnly);
|
1072
|
+
} else if (isTemplateLiteral(node)) {
|
1073
|
+
for (const expression of node.expressions) {
|
1074
|
+
if (!this.isPure(expression, constantsOnly)) return false;
|
1075
|
+
}
|
1076
|
+
return true;
|
1077
|
+
} else {
|
1078
|
+
return isPureish(node);
|
1079
|
+
}
|
1080
|
+
}
|
1081
|
+
setData(key, val) {
|
1082
|
+
return this.data[key] = val;
|
1083
|
+
}
|
1084
|
+
getData(key) {
|
1085
|
+
let scope = this;
|
1086
|
+
do {
|
1087
|
+
const data = scope.data[key];
|
1088
|
+
if (data != null) return data;
|
1089
|
+
} while (scope = scope.parent);
|
1090
|
+
}
|
1091
|
+
removeData(key) {
|
1092
|
+
let scope = this;
|
1093
|
+
do {
|
1094
|
+
const data = scope.data[key];
|
1095
|
+
if (data != null) scope.data[key] = null;
|
1096
|
+
} while (scope = scope.parent);
|
1097
|
+
}
|
1098
|
+
init() {
|
1099
|
+
if (!this.inited) {
|
1100
|
+
this.inited = true;
|
1101
|
+
this.crawl();
|
1102
|
+
}
|
1103
|
+
}
|
1104
|
+
crawl() {
|
1105
|
+
const path = this.path;
|
1106
|
+
this.references = Object.create(null);
|
1107
|
+
this.bindings = Object.create(null);
|
1108
|
+
this.globals = Object.create(null);
|
1109
|
+
this.uids = Object.create(null);
|
1110
|
+
this.data = Object.create(null);
|
1111
|
+
const programParent = this.getProgramParent();
|
1112
|
+
if (programParent.crawling) return;
|
1113
|
+
const state = {
|
1114
|
+
references: [],
|
1115
|
+
constantViolations: [],
|
1116
|
+
assignments: []
|
1117
|
+
};
|
1118
|
+
this.crawling = true;
|
1119
|
+
if (path.type !== "Program" && isExplodedVisitor(collectorVisitor)) {
|
1120
|
+
for (const visit of collectorVisitor.enter) {
|
1121
|
+
visit.call(state, path, state);
|
1122
|
+
}
|
1123
|
+
const typeVisitors = collectorVisitor[path.type];
|
1124
|
+
if (typeVisitors) {
|
1125
|
+
for (const visit of typeVisitors.enter) {
|
1126
|
+
visit.call(state, path, state);
|
1127
|
+
}
|
1128
|
+
}
|
1129
|
+
}
|
1130
|
+
path.traverse(collectorVisitor, state);
|
1131
|
+
this.crawling = false;
|
1132
|
+
for (const path of state.assignments) {
|
1133
|
+
const ids = path.getBindingIdentifiers();
|
1134
|
+
for (const name of Object.keys(ids)) {
|
1135
|
+
if (path.scope.getBinding(name)) continue;
|
1136
|
+
programParent.addGlobal(ids[name]);
|
1137
|
+
}
|
1138
|
+
path.scope.registerConstantViolation(path);
|
1139
|
+
}
|
1140
|
+
for (const ref of state.references) {
|
1141
|
+
const binding = ref.scope.getBinding(ref.node.name);
|
1142
|
+
if (binding) {
|
1143
|
+
binding.reference(ref);
|
1144
|
+
} else {
|
1145
|
+
programParent.addGlobal(ref.node);
|
1146
|
+
}
|
1147
|
+
}
|
1148
|
+
for (const path of state.constantViolations) {
|
1149
|
+
path.scope.registerConstantViolation(path);
|
1150
|
+
}
|
1151
|
+
}
|
1152
|
+
push(opts) {
|
1153
|
+
let path = this.path;
|
1154
|
+
if (path.isPattern()) {
|
1155
|
+
path = this.getPatternParent().path;
|
1156
|
+
} else if (!path.isBlockStatement() && !path.isProgram()) {
|
1157
|
+
path = this.getBlockParent().path;
|
1158
|
+
}
|
1159
|
+
if (path.isSwitchStatement()) {
|
1160
|
+
path = (this.getFunctionParent() || this.getProgramParent()).path;
|
1161
|
+
}
|
1162
|
+
const {
|
1163
|
+
init,
|
1164
|
+
unique,
|
1165
|
+
kind = "var",
|
1166
|
+
id
|
1167
|
+
} = opts;
|
1168
|
+
if (!init && !unique && (kind === "var" || kind === "let") && path.isFunction() && !path.node.name && _t.isCallExpression(path.parent, {
|
1169
|
+
callee: path.node
|
1170
|
+
}) && path.parent.arguments.length <= path.node.params.length && _t.isIdentifier(id)) {
|
1171
|
+
path.pushContainer("params", id);
|
1172
|
+
path.scope.registerBinding("param", path.get("params")[path.node.params.length - 1]);
|
1173
|
+
return;
|
1174
|
+
}
|
1175
|
+
if (path.isLoop() || path.isCatchClause() || path.isFunction()) {
|
1176
|
+
path.ensureBlock();
|
1177
|
+
path = path.get("body");
|
1178
|
+
}
|
1179
|
+
const blockHoist = opts._blockHoist == null ? 2 : opts._blockHoist;
|
1180
|
+
const dataKey = `declaration:${kind}:${blockHoist}`;
|
1181
|
+
let declarPath = !unique && path.getData(dataKey);
|
1182
|
+
if (!declarPath) {
|
1183
|
+
const declar = variableDeclaration$1(kind, []);
|
1184
|
+
declar._blockHoist = blockHoist;
|
1185
|
+
[declarPath] = path.unshiftContainer("body", [declar]);
|
1186
|
+
if (!unique) path.setData(dataKey, declarPath);
|
1187
|
+
}
|
1188
|
+
const declarator = variableDeclarator$1(id, init);
|
1189
|
+
const len = declarPath.node.declarations.push(declarator);
|
1190
|
+
path.scope.registerBinding(kind, declarPath.get("declarations")[len - 1]);
|
1191
|
+
}
|
1192
|
+
getProgramParent() {
|
1193
|
+
let scope = this;
|
1194
|
+
do {
|
1195
|
+
if (scope.path.isProgram()) {
|
1196
|
+
return scope;
|
1197
|
+
}
|
1198
|
+
} while (scope = scope.parent);
|
1199
|
+
throw new Error("Couldn't find a Program");
|
1200
|
+
}
|
1201
|
+
getFunctionParent() {
|
1202
|
+
let scope = this;
|
1203
|
+
do {
|
1204
|
+
if (scope.path.isFunctionParent()) {
|
1205
|
+
return scope;
|
1206
|
+
}
|
1207
|
+
} while (scope = scope.parent);
|
1208
|
+
return null;
|
1209
|
+
}
|
1210
|
+
getBlockParent() {
|
1211
|
+
let scope = this;
|
1212
|
+
do {
|
1213
|
+
if (scope.path.isBlockParent()) {
|
1214
|
+
return scope;
|
1215
|
+
}
|
1216
|
+
} while (scope = scope.parent);
|
1217
|
+
throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
|
1218
|
+
}
|
1219
|
+
getPatternParent() {
|
1220
|
+
let scope = this;
|
1221
|
+
do {
|
1222
|
+
if (!scope.path.isPattern()) {
|
1223
|
+
return scope.getBlockParent();
|
1224
|
+
}
|
1225
|
+
} while (scope = scope.parent.parent);
|
1226
|
+
throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
|
1227
|
+
}
|
1228
|
+
getAllBindings() {
|
1229
|
+
const ids = Object.create(null);
|
1230
|
+
let scope = this;
|
1231
|
+
do {
|
1232
|
+
for (const key of Object.keys(scope.bindings)) {
|
1233
|
+
if (key in ids === false) {
|
1234
|
+
ids[key] = scope.bindings[key];
|
1235
|
+
}
|
1236
|
+
}
|
1237
|
+
scope = scope.parent;
|
1238
|
+
} while (scope);
|
1239
|
+
return ids;
|
1240
|
+
}
|
1241
|
+
getAllBindingsOfKind(...kinds) {
|
1242
|
+
const ids = Object.create(null);
|
1243
|
+
for (const kind of kinds) {
|
1244
|
+
let scope = this;
|
1245
|
+
do {
|
1246
|
+
for (const name of Object.keys(scope.bindings)) {
|
1247
|
+
const binding = scope.bindings[name];
|
1248
|
+
if (binding.kind === kind) ids[name] = binding;
|
1249
|
+
}
|
1250
|
+
scope = scope.parent;
|
1251
|
+
} while (scope);
|
1252
|
+
}
|
1253
|
+
return ids;
|
1254
|
+
}
|
1255
|
+
bindingIdentifierEquals(name, node) {
|
1256
|
+
return this.getBindingIdentifier(name) === node;
|
1257
|
+
}
|
1258
|
+
getBinding(name) {
|
1259
|
+
let scope = this;
|
1260
|
+
let previousPath;
|
1261
|
+
do {
|
1262
|
+
const binding = scope.getOwnBinding(name);
|
1263
|
+
if (binding) {
|
1264
|
+
if (previousPath?.isPattern() && binding.kind !== "param" && binding.kind !== "local") ; else {
|
1265
|
+
return binding;
|
1266
|
+
}
|
1267
|
+
} else if (!binding && name === "arguments" && scope.path.isFunction() && !scope.path.isArrowFunctionExpression()) {
|
1268
|
+
break;
|
1269
|
+
}
|
1270
|
+
previousPath = scope.path;
|
1271
|
+
} while (scope = scope.parent);
|
1272
|
+
}
|
1273
|
+
getOwnBinding(name) {
|
1274
|
+
return this.bindings[name];
|
1275
|
+
}
|
1276
|
+
getBindingIdentifier(name) {
|
1277
|
+
return this.getBinding(name)?.identifier;
|
1278
|
+
}
|
1279
|
+
getOwnBindingIdentifier(name) {
|
1280
|
+
const binding = this.bindings[name];
|
1281
|
+
return binding?.identifier;
|
1282
|
+
}
|
1283
|
+
hasOwnBinding(name) {
|
1284
|
+
return !!this.getOwnBinding(name);
|
1285
|
+
}
|
1286
|
+
hasBinding(name, opts) {
|
1287
|
+
if (!name) return false;
|
1288
|
+
if (this.hasOwnBinding(name)) return true;
|
1289
|
+
{
|
1290
|
+
if (typeof opts === "boolean") opts = {
|
1291
|
+
noGlobals: opts
|
1292
|
+
};
|
1293
|
+
}
|
1294
|
+
if (this.parentHasBinding(name, opts)) return true;
|
1295
|
+
if (!opts?.noUids && this.hasUid(name)) return true;
|
1296
|
+
if (!opts?.noGlobals && Scope.globals.includes(name)) return true;
|
1297
|
+
if (!opts?.noGlobals && Scope.contextVariables.includes(name)) return true;
|
1298
|
+
return false;
|
1299
|
+
}
|
1300
|
+
parentHasBinding(name, opts) {
|
1301
|
+
return this.parent?.hasBinding(name, opts);
|
1302
|
+
}
|
1303
|
+
moveBindingTo(name, scope) {
|
1304
|
+
const info = this.getBinding(name);
|
1305
|
+
if (info) {
|
1306
|
+
info.scope.removeOwnBinding(name);
|
1307
|
+
info.scope = scope;
|
1308
|
+
scope.bindings[name] = info;
|
1309
|
+
}
|
1310
|
+
}
|
1311
|
+
removeOwnBinding(name) {
|
1312
|
+
delete this.bindings[name];
|
1313
|
+
}
|
1314
|
+
removeBinding(name) {
|
1315
|
+
this.getBinding(name)?.scope.removeOwnBinding(name);
|
1316
|
+
let scope = this;
|
1317
|
+
do {
|
1318
|
+
if (scope.uids[name]) {
|
1319
|
+
scope.uids[name] = false;
|
1320
|
+
}
|
1321
|
+
} while (scope = scope.parent);
|
1322
|
+
}
|
1323
|
+
}
|
1324
|
+
|
1325
|
+
const {
|
1326
|
+
VISITOR_KEYS: VISITOR_KEYS$4
|
1327
|
+
} = _t;
|
1328
|
+
function findParent(callback) {
|
1329
|
+
let path = this;
|
1330
|
+
while (path = path.parentPath) {
|
1331
|
+
if (callback(path)) return path;
|
1332
|
+
}
|
1333
|
+
return null;
|
1334
|
+
}
|
1335
|
+
function find(callback) {
|
1336
|
+
let path = this;
|
1337
|
+
do {
|
1338
|
+
if (callback(path)) return path;
|
1339
|
+
} while (path = path.parentPath);
|
1340
|
+
return null;
|
1341
|
+
}
|
1342
|
+
function getFunctionParent() {
|
1343
|
+
return this.findParent(p => p.isFunction());
|
1344
|
+
}
|
1345
|
+
function getStatementParent() {
|
1346
|
+
let path = this;
|
1347
|
+
do {
|
1348
|
+
if (!path.parentPath || Array.isArray(path.container) && path.isStatement()) {
|
1349
|
+
break;
|
1350
|
+
} else {
|
1351
|
+
path = path.parentPath;
|
1352
|
+
}
|
1353
|
+
} while (path);
|
1354
|
+
if (path && (path.isProgram() || path.isFile())) {
|
1355
|
+
throw new Error("File/Program node, we can't possibly find a statement parent to this");
|
1356
|
+
}
|
1357
|
+
return path;
|
1358
|
+
}
|
1359
|
+
function getEarliestCommonAncestorFrom(paths) {
|
1360
|
+
return this.getDeepestCommonAncestorFrom(paths, function (deepest, i, ancestries) {
|
1361
|
+
let earliest;
|
1362
|
+
const keys = VISITOR_KEYS$4[deepest.type];
|
1363
|
+
for (const ancestry of ancestries) {
|
1364
|
+
const path = ancestry[i + 1];
|
1365
|
+
if (!earliest) {
|
1366
|
+
earliest = path;
|
1367
|
+
continue;
|
1368
|
+
}
|
1369
|
+
if (path.listKey && earliest.listKey === path.listKey) {
|
1370
|
+
if (path.key < earliest.key) {
|
1371
|
+
earliest = path;
|
1372
|
+
continue;
|
1373
|
+
}
|
1374
|
+
}
|
1375
|
+
const earliestKeyIndex = keys.indexOf(earliest.parentKey);
|
1376
|
+
const currentKeyIndex = keys.indexOf(path.parentKey);
|
1377
|
+
if (earliestKeyIndex > currentKeyIndex) {
|
1378
|
+
earliest = path;
|
1379
|
+
}
|
1380
|
+
}
|
1381
|
+
return earliest;
|
1382
|
+
});
|
1383
|
+
}
|
1384
|
+
function getDeepestCommonAncestorFrom(paths, filter) {
|
1385
|
+
if (!paths.length) {
|
1386
|
+
return this;
|
1387
|
+
}
|
1388
|
+
if (paths.length === 1) {
|
1389
|
+
return paths[0];
|
1390
|
+
}
|
1391
|
+
let minDepth = Infinity;
|
1392
|
+
let lastCommonIndex, lastCommon;
|
1393
|
+
const ancestries = paths.map(path => {
|
1394
|
+
const ancestry = [];
|
1395
|
+
do {
|
1396
|
+
ancestry.unshift(path);
|
1397
|
+
} while ((path = path.parentPath) && path !== this);
|
1398
|
+
if (ancestry.length < minDepth) {
|
1399
|
+
minDepth = ancestry.length;
|
1400
|
+
}
|
1401
|
+
return ancestry;
|
1402
|
+
});
|
1403
|
+
const first = ancestries[0];
|
1404
|
+
depthLoop: for (let i = 0; i < minDepth; i++) {
|
1405
|
+
const shouldMatch = first[i];
|
1406
|
+
for (const ancestry of ancestries) {
|
1407
|
+
if (ancestry[i] !== shouldMatch) {
|
1408
|
+
break depthLoop;
|
1409
|
+
}
|
1410
|
+
}
|
1411
|
+
lastCommonIndex = i;
|
1412
|
+
lastCommon = shouldMatch;
|
1413
|
+
}
|
1414
|
+
if (lastCommon) {
|
1415
|
+
if (filter) {
|
1416
|
+
return filter(lastCommon, lastCommonIndex, ancestries);
|
1417
|
+
} else {
|
1418
|
+
return lastCommon;
|
1419
|
+
}
|
1420
|
+
} else {
|
1421
|
+
throw new Error("Couldn't find intersection");
|
1422
|
+
}
|
1423
|
+
}
|
1424
|
+
function getAncestry() {
|
1425
|
+
let path = this;
|
1426
|
+
const paths = [];
|
1427
|
+
do {
|
1428
|
+
paths.push(path);
|
1429
|
+
} while (path = path.parentPath);
|
1430
|
+
return paths;
|
1431
|
+
}
|
1432
|
+
function isAncestor(maybeDescendant) {
|
1433
|
+
return maybeDescendant.isDescendant(this);
|
1434
|
+
}
|
1435
|
+
function isDescendant(maybeAncestor) {
|
1436
|
+
return !!this.findParent(parent => parent === maybeAncestor);
|
1437
|
+
}
|
1438
|
+
function inType(...candidateTypes) {
|
1439
|
+
let path = this;
|
1440
|
+
while (path) {
|
1441
|
+
for (const type of candidateTypes) {
|
1442
|
+
if (path.node.type === type) return true;
|
1443
|
+
}
|
1444
|
+
path = path.parentPath;
|
1445
|
+
}
|
1446
|
+
return false;
|
1447
|
+
}
|
1448
|
+
|
1449
|
+
var NodePath_ancestry = /*#__PURE__*/Object.freeze({
|
1450
|
+
__proto__: null,
|
1451
|
+
findParent: findParent,
|
1452
|
+
find: find,
|
1453
|
+
getFunctionParent: getFunctionParent,
|
1454
|
+
getStatementParent: getStatementParent,
|
1455
|
+
getEarliestCommonAncestorFrom: getEarliestCommonAncestorFrom,
|
1456
|
+
getDeepestCommonAncestorFrom: getDeepestCommonAncestorFrom,
|
1457
|
+
getAncestry: getAncestry,
|
1458
|
+
isAncestor: isAncestor,
|
1459
|
+
isDescendant: isDescendant,
|
1460
|
+
inType: inType
|
1461
|
+
});
|
1462
|
+
|
1463
|
+
const {
|
1464
|
+
createFlowUnionType,
|
1465
|
+
createTSUnionType,
|
1466
|
+
createUnionTypeAnnotation,
|
1467
|
+
isFlowType,
|
1468
|
+
isTSType
|
1469
|
+
} = _t;
|
1470
|
+
function createUnionType(types) {
|
1471
|
+
{
|
1472
|
+
if (isFlowType(types[0])) {
|
1473
|
+
return createFlowUnionType(types);
|
1474
|
+
}
|
1475
|
+
if (isTSType(types[0])) {
|
1476
|
+
return createTSUnionType(types);
|
1477
|
+
}
|
1478
|
+
}
|
1479
|
+
}
|
1480
|
+
|
1481
|
+
const {
|
1482
|
+
BOOLEAN_NUMBER_BINARY_OPERATORS,
|
1483
|
+
createTypeAnnotationBasedOnTypeof,
|
1484
|
+
numberTypeAnnotation: numberTypeAnnotation$1,
|
1485
|
+
voidTypeAnnotation: voidTypeAnnotation$2
|
1486
|
+
} = _t;
|
1487
|
+
function infererReference (node) {
|
1488
|
+
if (!this.isReferenced()) return;
|
1489
|
+
const binding = this.scope.getBinding(node.name);
|
1490
|
+
if (binding) {
|
1491
|
+
if (binding.identifier.typeAnnotation) {
|
1492
|
+
return binding.identifier.typeAnnotation;
|
1493
|
+
} else {
|
1494
|
+
return getTypeAnnotationBindingConstantViolations(binding, this, node.name);
|
1495
|
+
}
|
1496
|
+
}
|
1497
|
+
if (node.name === "undefined") {
|
1498
|
+
return voidTypeAnnotation$2();
|
1499
|
+
} else if (node.name === "NaN" || node.name === "Infinity") {
|
1500
|
+
return numberTypeAnnotation$1();
|
1501
|
+
} else if (node.name === "arguments") ;
|
1502
|
+
}
|
1503
|
+
function getTypeAnnotationBindingConstantViolations(binding, path, name) {
|
1504
|
+
const types = [];
|
1505
|
+
const functionConstantViolations = [];
|
1506
|
+
let constantViolations = getConstantViolationsBefore(binding, path, functionConstantViolations);
|
1507
|
+
const testType = getConditionalAnnotation(binding, path, name);
|
1508
|
+
if (testType) {
|
1509
|
+
const testConstantViolations = getConstantViolationsBefore(binding, testType.ifStatement);
|
1510
|
+
constantViolations = constantViolations.filter(path => testConstantViolations.indexOf(path) < 0);
|
1511
|
+
types.push(testType.typeAnnotation);
|
1512
|
+
}
|
1513
|
+
if (constantViolations.length) {
|
1514
|
+
constantViolations.push(...functionConstantViolations);
|
1515
|
+
for (const violation of constantViolations) {
|
1516
|
+
types.push(violation.getTypeAnnotation());
|
1517
|
+
}
|
1518
|
+
}
|
1519
|
+
if (!types.length) {
|
1520
|
+
return;
|
1521
|
+
}
|
1522
|
+
return createUnionType(types);
|
1523
|
+
}
|
1524
|
+
function getConstantViolationsBefore(binding, path, functions) {
|
1525
|
+
const violations = binding.constantViolations.slice();
|
1526
|
+
violations.unshift(binding.path);
|
1527
|
+
return violations.filter(violation => {
|
1528
|
+
violation = violation.resolve();
|
1529
|
+
const status = violation._guessExecutionStatusRelativeTo(path);
|
1530
|
+
if (functions && status === "unknown") functions.push(violation);
|
1531
|
+
return status === "before";
|
1532
|
+
});
|
1533
|
+
}
|
1534
|
+
function inferAnnotationFromBinaryExpression(name, path) {
|
1535
|
+
const operator = path.node.operator;
|
1536
|
+
const right = path.get("right").resolve();
|
1537
|
+
const left = path.get("left").resolve();
|
1538
|
+
let target;
|
1539
|
+
if (left.isIdentifier({
|
1540
|
+
name
|
1541
|
+
})) {
|
1542
|
+
target = right;
|
1543
|
+
} else if (right.isIdentifier({
|
1544
|
+
name
|
1545
|
+
})) {
|
1546
|
+
target = left;
|
1547
|
+
}
|
1548
|
+
if (target) {
|
1549
|
+
if (operator === "===") {
|
1550
|
+
return target.getTypeAnnotation();
|
1551
|
+
}
|
1552
|
+
if (BOOLEAN_NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
|
1553
|
+
return numberTypeAnnotation$1();
|
1554
|
+
}
|
1555
|
+
return;
|
1556
|
+
}
|
1557
|
+
if (operator !== "===" && operator !== "==") return;
|
1558
|
+
let typeofPath;
|
1559
|
+
let typePath;
|
1560
|
+
if (left.isUnaryExpression({
|
1561
|
+
operator: "typeof"
|
1562
|
+
})) {
|
1563
|
+
typeofPath = left;
|
1564
|
+
typePath = right;
|
1565
|
+
} else if (right.isUnaryExpression({
|
1566
|
+
operator: "typeof"
|
1567
|
+
})) {
|
1568
|
+
typeofPath = right;
|
1569
|
+
typePath = left;
|
1570
|
+
}
|
1571
|
+
if (!typeofPath) return;
|
1572
|
+
if (!typeofPath.get("argument").isIdentifier({
|
1573
|
+
name
|
1574
|
+
})) return;
|
1575
|
+
typePath = typePath.resolve();
|
1576
|
+
if (!typePath.isLiteral()) return;
|
1577
|
+
const typeValue = typePath.node.value;
|
1578
|
+
if (typeof typeValue !== "string") return;
|
1579
|
+
return createTypeAnnotationBasedOnTypeof(typeValue);
|
1580
|
+
}
|
1581
|
+
function getParentConditionalPath(binding, path, name) {
|
1582
|
+
let parentPath;
|
1583
|
+
while (parentPath = path.parentPath) {
|
1584
|
+
if (parentPath.isIfStatement() || parentPath.isConditionalExpression()) {
|
1585
|
+
if (path.key === "test") {
|
1586
|
+
return;
|
1587
|
+
}
|
1588
|
+
return parentPath;
|
1589
|
+
}
|
1590
|
+
if (parentPath.isFunction()) {
|
1591
|
+
if (parentPath.parentPath.scope.getBinding(name) !== binding) return;
|
1592
|
+
}
|
1593
|
+
path = parentPath;
|
1594
|
+
}
|
1595
|
+
}
|
1596
|
+
function getConditionalAnnotation(binding, path, name) {
|
1597
|
+
const ifStatement = getParentConditionalPath(binding, path, name);
|
1598
|
+
if (!ifStatement) return;
|
1599
|
+
const test = ifStatement.get("test");
|
1600
|
+
const paths = [test];
|
1601
|
+
const types = [];
|
1602
|
+
for (let i = 0; i < paths.length; i++) {
|
1603
|
+
const path = paths[i];
|
1604
|
+
if (path.isLogicalExpression()) {
|
1605
|
+
if (path.node.operator === "&&") {
|
1606
|
+
paths.push(path.get("left"));
|
1607
|
+
paths.push(path.get("right"));
|
1608
|
+
}
|
1609
|
+
} else if (path.isBinaryExpression()) {
|
1610
|
+
const type = inferAnnotationFromBinaryExpression(name, path);
|
1611
|
+
if (type) types.push(type);
|
1612
|
+
}
|
1613
|
+
}
|
1614
|
+
if (types.length) {
|
1615
|
+
return {
|
1616
|
+
typeAnnotation: createUnionType(types),
|
1617
|
+
ifStatement
|
1618
|
+
};
|
1619
|
+
}
|
1620
|
+
return getConditionalAnnotation(binding, ifStatement, name);
|
1621
|
+
}
|
1622
|
+
|
1623
|
+
const {
|
1624
|
+
BOOLEAN_BINARY_OPERATORS,
|
1625
|
+
BOOLEAN_UNARY_OPERATORS,
|
1626
|
+
NUMBER_BINARY_OPERATORS,
|
1627
|
+
NUMBER_UNARY_OPERATORS,
|
1628
|
+
STRING_UNARY_OPERATORS,
|
1629
|
+
anyTypeAnnotation: anyTypeAnnotation$1,
|
1630
|
+
arrayTypeAnnotation,
|
1631
|
+
booleanTypeAnnotation,
|
1632
|
+
buildMatchMemberExpression,
|
1633
|
+
genericTypeAnnotation,
|
1634
|
+
identifier: identifier$2,
|
1635
|
+
nullLiteralTypeAnnotation,
|
1636
|
+
numberTypeAnnotation,
|
1637
|
+
stringTypeAnnotation: stringTypeAnnotation$1,
|
1638
|
+
tupleTypeAnnotation,
|
1639
|
+
unionTypeAnnotation,
|
1640
|
+
voidTypeAnnotation: voidTypeAnnotation$1,
|
1641
|
+
isIdentifier: isIdentifier$5
|
1642
|
+
} = _t;
|
1643
|
+
function VariableDeclarator() {
|
1644
|
+
if (!this.get("id").isIdentifier()) return;
|
1645
|
+
return this.get("init").getTypeAnnotation();
|
1646
|
+
}
|
1647
|
+
function TypeCastExpression(node) {
|
1648
|
+
return node.typeAnnotation;
|
1649
|
+
}
|
1650
|
+
TypeCastExpression.validParent = true;
|
1651
|
+
function TSAsExpression(node) {
|
1652
|
+
return node.typeAnnotation;
|
1653
|
+
}
|
1654
|
+
TSAsExpression.validParent = true;
|
1655
|
+
function TSNonNullExpression() {
|
1656
|
+
return this.get("expression").getTypeAnnotation();
|
1657
|
+
}
|
1658
|
+
function NewExpression(node) {
|
1659
|
+
if (node.callee.type === "Identifier") {
|
1660
|
+
return genericTypeAnnotation(node.callee);
|
1661
|
+
}
|
1662
|
+
}
|
1663
|
+
function TemplateLiteral() {
|
1664
|
+
return stringTypeAnnotation$1();
|
1665
|
+
}
|
1666
|
+
function UnaryExpression(node) {
|
1667
|
+
const operator = node.operator;
|
1668
|
+
if (operator === "void") {
|
1669
|
+
return voidTypeAnnotation$1();
|
1670
|
+
} else if (NUMBER_UNARY_OPERATORS.indexOf(operator) >= 0) {
|
1671
|
+
return numberTypeAnnotation();
|
1672
|
+
} else if (STRING_UNARY_OPERATORS.indexOf(operator) >= 0) {
|
1673
|
+
return stringTypeAnnotation$1();
|
1674
|
+
} else if (BOOLEAN_UNARY_OPERATORS.indexOf(operator) >= 0) {
|
1675
|
+
return booleanTypeAnnotation();
|
1676
|
+
}
|
1677
|
+
}
|
1678
|
+
function BinaryExpression(node) {
|
1679
|
+
const operator = node.operator;
|
1680
|
+
if (NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
|
1681
|
+
return numberTypeAnnotation();
|
1682
|
+
} else if (BOOLEAN_BINARY_OPERATORS.indexOf(operator) >= 0) {
|
1683
|
+
return booleanTypeAnnotation();
|
1684
|
+
} else if (operator === "+") {
|
1685
|
+
const right = this.get("right");
|
1686
|
+
const left = this.get("left");
|
1687
|
+
if (left.isBaseType("number") && right.isBaseType("number")) {
|
1688
|
+
return numberTypeAnnotation();
|
1689
|
+
} else if (left.isBaseType("string") || right.isBaseType("string")) {
|
1690
|
+
return stringTypeAnnotation$1();
|
1691
|
+
}
|
1692
|
+
return unionTypeAnnotation([stringTypeAnnotation$1(), numberTypeAnnotation()]);
|
1693
|
+
}
|
1694
|
+
}
|
1695
|
+
function LogicalExpression() {
|
1696
|
+
const argumentTypes = [this.get("left").getTypeAnnotation(), this.get("right").getTypeAnnotation()];
|
1697
|
+
return createUnionType(argumentTypes);
|
1698
|
+
}
|
1699
|
+
function ConditionalExpression() {
|
1700
|
+
const argumentTypes = [this.get("consequent").getTypeAnnotation(), this.get("alternate").getTypeAnnotation()];
|
1701
|
+
return createUnionType(argumentTypes);
|
1702
|
+
}
|
1703
|
+
function SequenceExpression() {
|
1704
|
+
return this.get("expressions").pop().getTypeAnnotation();
|
1705
|
+
}
|
1706
|
+
function ParenthesizedExpression() {
|
1707
|
+
return this.get("expression").getTypeAnnotation();
|
1708
|
+
}
|
1709
|
+
function AssignmentExpression() {
|
1710
|
+
return this.get("right").getTypeAnnotation();
|
1711
|
+
}
|
1712
|
+
function UpdateExpression(node) {
|
1713
|
+
const operator = node.operator;
|
1714
|
+
if (operator === "++" || operator === "--") {
|
1715
|
+
return numberTypeAnnotation();
|
1716
|
+
}
|
1717
|
+
}
|
1718
|
+
function StringLiteral() {
|
1719
|
+
return stringTypeAnnotation$1();
|
1720
|
+
}
|
1721
|
+
function NumericLiteral() {
|
1722
|
+
return numberTypeAnnotation();
|
1723
|
+
}
|
1724
|
+
function BooleanLiteral() {
|
1725
|
+
return booleanTypeAnnotation();
|
1726
|
+
}
|
1727
|
+
function NullLiteral() {
|
1728
|
+
return nullLiteralTypeAnnotation();
|
1729
|
+
}
|
1730
|
+
function RegExpLiteral() {
|
1731
|
+
return genericTypeAnnotation(identifier$2("RegExp"));
|
1732
|
+
}
|
1733
|
+
function ObjectExpression() {
|
1734
|
+
return genericTypeAnnotation(identifier$2("Object"));
|
1735
|
+
}
|
1736
|
+
function ArrayExpression() {
|
1737
|
+
return genericTypeAnnotation(identifier$2("Array"));
|
1738
|
+
}
|
1739
|
+
function RestElement() {
|
1740
|
+
return ArrayExpression();
|
1741
|
+
}
|
1742
|
+
RestElement.validParent = true;
|
1743
|
+
function Func() {
|
1744
|
+
return genericTypeAnnotation(identifier$2("Function"));
|
1745
|
+
}
|
1746
|
+
const isArrayFrom = buildMatchMemberExpression("Array.from");
|
1747
|
+
const isObjectKeys = buildMatchMemberExpression("Object.keys");
|
1748
|
+
const isObjectValues = buildMatchMemberExpression("Object.values");
|
1749
|
+
const isObjectEntries = buildMatchMemberExpression("Object.entries");
|
1750
|
+
function CallExpression() {
|
1751
|
+
const {
|
1752
|
+
callee
|
1753
|
+
} = this.node;
|
1754
|
+
if (isObjectKeys(callee)) {
|
1755
|
+
return arrayTypeAnnotation(stringTypeAnnotation$1());
|
1756
|
+
} else if (isArrayFrom(callee) || isObjectValues(callee) || isIdentifier$5(callee, {
|
1757
|
+
name: "Array"
|
1758
|
+
})) {
|
1759
|
+
return arrayTypeAnnotation(anyTypeAnnotation$1());
|
1760
|
+
} else if (isObjectEntries(callee)) {
|
1761
|
+
return arrayTypeAnnotation(tupleTypeAnnotation([stringTypeAnnotation$1(), anyTypeAnnotation$1()]));
|
1762
|
+
}
|
1763
|
+
return resolveCall(this.get("callee"));
|
1764
|
+
}
|
1765
|
+
function TaggedTemplateExpression() {
|
1766
|
+
return resolveCall(this.get("tag"));
|
1767
|
+
}
|
1768
|
+
function resolveCall(callee) {
|
1769
|
+
callee = callee.resolve();
|
1770
|
+
if (callee.isFunction()) {
|
1771
|
+
const {
|
1772
|
+
node
|
1773
|
+
} = callee;
|
1774
|
+
if (node.async) {
|
1775
|
+
if (node.generator) {
|
1776
|
+
return genericTypeAnnotation(identifier$2("AsyncIterator"));
|
1777
|
+
} else {
|
1778
|
+
return genericTypeAnnotation(identifier$2("Promise"));
|
1779
|
+
}
|
1780
|
+
} else {
|
1781
|
+
if (node.generator) {
|
1782
|
+
return genericTypeAnnotation(identifier$2("Iterator"));
|
1783
|
+
} else if (callee.node.returnType) {
|
1784
|
+
return callee.node.returnType;
|
1785
|
+
} else ;
|
1786
|
+
}
|
1787
|
+
}
|
1788
|
+
}
|
1789
|
+
|
1790
|
+
var inferers = /*#__PURE__*/Object.freeze({
|
1791
|
+
__proto__: null,
|
1792
|
+
VariableDeclarator: VariableDeclarator,
|
1793
|
+
TypeCastExpression: TypeCastExpression,
|
1794
|
+
TSAsExpression: TSAsExpression,
|
1795
|
+
TSNonNullExpression: TSNonNullExpression,
|
1796
|
+
NewExpression: NewExpression,
|
1797
|
+
TemplateLiteral: TemplateLiteral,
|
1798
|
+
UnaryExpression: UnaryExpression,
|
1799
|
+
BinaryExpression: BinaryExpression,
|
1800
|
+
LogicalExpression: LogicalExpression,
|
1801
|
+
ConditionalExpression: ConditionalExpression,
|
1802
|
+
SequenceExpression: SequenceExpression,
|
1803
|
+
ParenthesizedExpression: ParenthesizedExpression,
|
1804
|
+
AssignmentExpression: AssignmentExpression,
|
1805
|
+
UpdateExpression: UpdateExpression,
|
1806
|
+
StringLiteral: StringLiteral,
|
1807
|
+
NumericLiteral: NumericLiteral,
|
1808
|
+
BooleanLiteral: BooleanLiteral,
|
1809
|
+
NullLiteral: NullLiteral,
|
1810
|
+
RegExpLiteral: RegExpLiteral,
|
1811
|
+
ObjectExpression: ObjectExpression,
|
1812
|
+
ArrayExpression: ArrayExpression,
|
1813
|
+
RestElement: RestElement,
|
1814
|
+
FunctionExpression: Func,
|
1815
|
+
ArrowFunctionExpression: Func,
|
1816
|
+
FunctionDeclaration: Func,
|
1817
|
+
ClassExpression: Func,
|
1818
|
+
ClassDeclaration: Func,
|
1819
|
+
CallExpression: CallExpression,
|
1820
|
+
TaggedTemplateExpression: TaggedTemplateExpression,
|
1821
|
+
Identifier: infererReference
|
1822
|
+
});
|
1823
|
+
|
1824
|
+
const {
|
1825
|
+
anyTypeAnnotation,
|
1826
|
+
isAnyTypeAnnotation,
|
1827
|
+
isArrayTypeAnnotation,
|
1828
|
+
isBooleanTypeAnnotation,
|
1829
|
+
isEmptyTypeAnnotation,
|
1830
|
+
isFlowBaseAnnotation,
|
1831
|
+
isGenericTypeAnnotation,
|
1832
|
+
isIdentifier: isIdentifier$4,
|
1833
|
+
isMixedTypeAnnotation,
|
1834
|
+
isNumberTypeAnnotation,
|
1835
|
+
isStringTypeAnnotation,
|
1836
|
+
isTSArrayType,
|
1837
|
+
isTSTypeAnnotation,
|
1838
|
+
isTSTypeReference,
|
1839
|
+
isTupleTypeAnnotation,
|
1840
|
+
isTypeAnnotation,
|
1841
|
+
isUnionTypeAnnotation,
|
1842
|
+
isVoidTypeAnnotation,
|
1843
|
+
stringTypeAnnotation,
|
1844
|
+
voidTypeAnnotation
|
1845
|
+
} = _t;
|
1846
|
+
function getTypeAnnotation() {
|
1847
|
+
let type = this.getData("typeAnnotation");
|
1848
|
+
if (type != null) {
|
1849
|
+
return type;
|
1850
|
+
}
|
1851
|
+
type = this._getTypeAnnotation() || anyTypeAnnotation();
|
1852
|
+
if (isTypeAnnotation(type) || isTSTypeAnnotation(type)) {
|
1853
|
+
type = type.typeAnnotation;
|
1854
|
+
}
|
1855
|
+
this.setData("typeAnnotation", type);
|
1856
|
+
return type;
|
1857
|
+
}
|
1858
|
+
const typeAnnotationInferringNodes = new WeakSet();
|
1859
|
+
function _getTypeAnnotation() {
|
1860
|
+
const node = this.node;
|
1861
|
+
if (!node) {
|
1862
|
+
if (this.key === "init" && this.parentPath.isVariableDeclarator()) {
|
1863
|
+
const declar = this.parentPath.parentPath;
|
1864
|
+
const declarParent = declar.parentPath;
|
1865
|
+
if (declar.key === "left" && declarParent.isForInStatement()) {
|
1866
|
+
return stringTypeAnnotation();
|
1867
|
+
}
|
1868
|
+
if (declar.key === "left" && declarParent.isForOfStatement()) {
|
1869
|
+
return anyTypeAnnotation();
|
1870
|
+
}
|
1871
|
+
return voidTypeAnnotation();
|
1872
|
+
} else {
|
1873
|
+
return;
|
1874
|
+
}
|
1875
|
+
}
|
1876
|
+
if (node.typeAnnotation) {
|
1877
|
+
return node.typeAnnotation;
|
1878
|
+
}
|
1879
|
+
if (typeAnnotationInferringNodes.has(node)) {
|
1880
|
+
return;
|
1881
|
+
}
|
1882
|
+
typeAnnotationInferringNodes.add(node);
|
1883
|
+
try {
|
1884
|
+
let inferer = inferers[node.type];
|
1885
|
+
if (inferer) {
|
1886
|
+
return inferer.call(this, node);
|
1887
|
+
}
|
1888
|
+
inferer = inferers[this.parentPath.type];
|
1889
|
+
if (inferer?.validParent) {
|
1890
|
+
return this.parentPath.getTypeAnnotation();
|
1891
|
+
}
|
1892
|
+
} finally {
|
1893
|
+
typeAnnotationInferringNodes.delete(node);
|
1894
|
+
}
|
1895
|
+
}
|
1896
|
+
function isBaseType(baseName, soft) {
|
1897
|
+
return _isBaseType(baseName, this.getTypeAnnotation(), soft);
|
1898
|
+
}
|
1899
|
+
function _isBaseType(baseName, type, soft) {
|
1900
|
+
if (baseName === "string") {
|
1901
|
+
return isStringTypeAnnotation(type);
|
1902
|
+
} else if (baseName === "number") {
|
1903
|
+
return isNumberTypeAnnotation(type);
|
1904
|
+
} else if (baseName === "boolean") {
|
1905
|
+
return isBooleanTypeAnnotation(type);
|
1906
|
+
} else if (baseName === "any") {
|
1907
|
+
return isAnyTypeAnnotation(type);
|
1908
|
+
} else if (baseName === "mixed") {
|
1909
|
+
return isMixedTypeAnnotation(type);
|
1910
|
+
} else if (baseName === "empty") {
|
1911
|
+
return isEmptyTypeAnnotation(type);
|
1912
|
+
} else if (baseName === "void") {
|
1913
|
+
return isVoidTypeAnnotation(type);
|
1914
|
+
} else {
|
1915
|
+
if (soft) {
|
1916
|
+
return false;
|
1917
|
+
} else {
|
1918
|
+
throw new Error(`Unknown base type ${baseName}`);
|
1919
|
+
}
|
1920
|
+
}
|
1921
|
+
}
|
1922
|
+
function couldBeBaseType(name) {
|
1923
|
+
const type = this.getTypeAnnotation();
|
1924
|
+
if (isAnyTypeAnnotation(type)) return true;
|
1925
|
+
if (isUnionTypeAnnotation(type)) {
|
1926
|
+
for (const type2 of type.types) {
|
1927
|
+
if (isAnyTypeAnnotation(type2) || _isBaseType(name, type2, true)) {
|
1928
|
+
return true;
|
1929
|
+
}
|
1930
|
+
}
|
1931
|
+
return false;
|
1932
|
+
} else {
|
1933
|
+
return _isBaseType(name, type, true);
|
1934
|
+
}
|
1935
|
+
}
|
1936
|
+
function baseTypeStrictlyMatches(rightArg) {
|
1937
|
+
const left = this.getTypeAnnotation();
|
1938
|
+
const right = rightArg.getTypeAnnotation();
|
1939
|
+
if (!isAnyTypeAnnotation(left) && isFlowBaseAnnotation(left)) {
|
1940
|
+
return right.type === left.type;
|
1941
|
+
}
|
1942
|
+
return false;
|
1943
|
+
}
|
1944
|
+
function isGenericType(genericName) {
|
1945
|
+
const type = this.getTypeAnnotation();
|
1946
|
+
if (genericName === "Array") {
|
1947
|
+
if (isTSArrayType(type) || isArrayTypeAnnotation(type) || isTupleTypeAnnotation(type)) {
|
1948
|
+
return true;
|
1949
|
+
}
|
1950
|
+
}
|
1951
|
+
return isGenericTypeAnnotation(type) && isIdentifier$4(type.id, {
|
1952
|
+
name: genericName
|
1953
|
+
}) || isTSTypeReference(type) && isIdentifier$4(type.typeName, {
|
1954
|
+
name: genericName
|
1955
|
+
});
|
1956
|
+
}
|
1957
|
+
|
1958
|
+
var NodePath_inference = /*#__PURE__*/Object.freeze({
|
1959
|
+
__proto__: null,
|
1960
|
+
getTypeAnnotation: getTypeAnnotation,
|
1961
|
+
_getTypeAnnotation: _getTypeAnnotation,
|
1962
|
+
isBaseType: isBaseType,
|
1963
|
+
couldBeBaseType: couldBeBaseType,
|
1964
|
+
baseTypeStrictlyMatches: baseTypeStrictlyMatches,
|
1965
|
+
isGenericType: isGenericType
|
1966
|
+
});
|
1967
|
+
|
1968
|
+
const {
|
1969
|
+
FUNCTION_TYPES,
|
1970
|
+
arrowFunctionExpression: arrowFunctionExpression$2,
|
1971
|
+
assignmentExpression: assignmentExpression$2,
|
1972
|
+
awaitExpression,
|
1973
|
+
blockStatement: blockStatement$2,
|
1974
|
+
callExpression: callExpression$2,
|
1975
|
+
cloneNode: cloneNode$2,
|
1976
|
+
expressionStatement: expressionStatement$2,
|
1977
|
+
identifier: identifier$1,
|
1978
|
+
inheritLeadingComments,
|
1979
|
+
inheritTrailingComments,
|
1980
|
+
inheritsComments,
|
1981
|
+
isExpression: isExpression$3,
|
1982
|
+
isProgram,
|
1983
|
+
isStatement: isStatement$1,
|
1984
|
+
removeComments,
|
1985
|
+
returnStatement: returnStatement$1,
|
1986
|
+
toSequenceExpression,
|
1987
|
+
validate: validate$1,
|
1988
|
+
yieldExpression
|
1989
|
+
} = _t;
|
1990
|
+
function replaceWithMultiple(nodes) {
|
1991
|
+
this.resync();
|
1992
|
+
nodes = this._verifyNodeList(nodes);
|
1993
|
+
inheritLeadingComments(nodes[0], this.node);
|
1994
|
+
inheritTrailingComments(nodes[nodes.length - 1], this.node);
|
1995
|
+
path.get(this.parent)?.delete(this.node);
|
1996
|
+
this.node = this.container[this.key] = null;
|
1997
|
+
const paths = this.insertAfter(nodes);
|
1998
|
+
if (this.node) {
|
1999
|
+
this.requeue();
|
2000
|
+
} else {
|
2001
|
+
this.remove();
|
2002
|
+
}
|
2003
|
+
return paths;
|
2004
|
+
}
|
2005
|
+
function replaceWithSourceString(replacement) {
|
2006
|
+
this.resync();
|
2007
|
+
let ast;
|
2008
|
+
try {
|
2009
|
+
replacement = `(${replacement})`;
|
2010
|
+
ast = parse(replacement);
|
2011
|
+
} catch (err) {
|
2012
|
+
const loc = err.loc;
|
2013
|
+
if (loc) {
|
2014
|
+
err.message += " - make sure this is an expression.\n" + codeFrameColumns(replacement, {
|
2015
|
+
start: {
|
2016
|
+
line: loc.line,
|
2017
|
+
column: loc.column + 1
|
2018
|
+
}
|
2019
|
+
});
|
2020
|
+
err.code = "BABEL_REPLACE_SOURCE_ERROR";
|
2021
|
+
}
|
2022
|
+
throw err;
|
2023
|
+
}
|
2024
|
+
const expressionAST = ast.program.body[0].expression;
|
2025
|
+
traverse.removeProperties(expressionAST);
|
2026
|
+
return this.replaceWith(expressionAST);
|
2027
|
+
}
|
2028
|
+
function replaceWith(replacementPath) {
|
2029
|
+
this.resync();
|
2030
|
+
if (this.removed) {
|
2031
|
+
throw new Error("You can't replace this node, we've already removed it");
|
2032
|
+
}
|
2033
|
+
let replacement = replacementPath instanceof NodePath ? replacementPath.node : replacementPath;
|
2034
|
+
if (!replacement) {
|
2035
|
+
throw new Error("You passed `path.replaceWith()` a falsy node, use `path.remove()` instead");
|
2036
|
+
}
|
2037
|
+
if (this.node === replacement) {
|
2038
|
+
return [this];
|
2039
|
+
}
|
2040
|
+
if (this.isProgram() && !isProgram(replacement)) {
|
2041
|
+
throw new Error("You can only replace a Program root node with another Program node");
|
2042
|
+
}
|
2043
|
+
if (Array.isArray(replacement)) {
|
2044
|
+
throw new Error("Don't use `path.replaceWith()` with an array of nodes, use `path.replaceWithMultiple()`");
|
2045
|
+
}
|
2046
|
+
if (typeof replacement === "string") {
|
2047
|
+
throw new Error("Don't use `path.replaceWith()` with a source string, use `path.replaceWithSourceString()`");
|
2048
|
+
}
|
2049
|
+
let nodePath = "";
|
2050
|
+
if (this.isNodeType("Statement") && isExpression$3(replacement)) {
|
2051
|
+
if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement) && !this.parentPath.isExportDefaultDeclaration()) {
|
2052
|
+
replacement = expressionStatement$2(replacement);
|
2053
|
+
nodePath = "expression";
|
2054
|
+
}
|
2055
|
+
}
|
2056
|
+
if (this.isNodeType("Expression") && isStatement$1(replacement)) {
|
2057
|
+
if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement)) {
|
2058
|
+
return this.replaceExpressionWithStatements([replacement]);
|
2059
|
+
}
|
2060
|
+
}
|
2061
|
+
const oldNode = this.node;
|
2062
|
+
if (oldNode) {
|
2063
|
+
inheritsComments(replacement, oldNode);
|
2064
|
+
removeComments(oldNode);
|
2065
|
+
}
|
2066
|
+
this._replaceWith(replacement);
|
2067
|
+
this.type = replacement.type;
|
2068
|
+
this.setScope();
|
2069
|
+
this.requeue();
|
2070
|
+
return [nodePath ? this.get(nodePath) : this];
|
2071
|
+
}
|
2072
|
+
function _replaceWith(node) {
|
2073
|
+
if (!this.container) {
|
2074
|
+
throw new ReferenceError("Container is falsy");
|
2075
|
+
}
|
2076
|
+
if (this.inList) {
|
2077
|
+
validate$1(this.parent, this.key, [node]);
|
2078
|
+
} else {
|
2079
|
+
validate$1(this.parent, this.key, node);
|
2080
|
+
}
|
2081
|
+
this.debug(`Replace with ${node?.type}`);
|
2082
|
+
path.get(this.parent)?.set(node, this).delete(this.node);
|
2083
|
+
this.node = this.container[this.key] = node;
|
2084
|
+
}
|
2085
|
+
function replaceExpressionWithStatements(nodes) {
|
2086
|
+
this.resync();
|
2087
|
+
const nodesAsSequenceExpression = toSequenceExpression(nodes, this.scope);
|
2088
|
+
if (nodesAsSequenceExpression) {
|
2089
|
+
return this.replaceWith(nodesAsSequenceExpression)[0].get("expressions");
|
2090
|
+
}
|
2091
|
+
const functionParent = this.getFunctionParent();
|
2092
|
+
const isParentAsync = functionParent?.is("async");
|
2093
|
+
const isParentGenerator = functionParent?.is("generator");
|
2094
|
+
const container = arrowFunctionExpression$2([], blockStatement$2(nodes));
|
2095
|
+
this.replaceWith(callExpression$2(container, []));
|
2096
|
+
const callee = this.get("callee");
|
2097
|
+
hoistVariables(callee.get("body"), id => {
|
2098
|
+
this.scope.push({
|
2099
|
+
id
|
2100
|
+
});
|
2101
|
+
}, "var");
|
2102
|
+
const completionRecords = this.get("callee").getCompletionRecords();
|
2103
|
+
for (const path of completionRecords) {
|
2104
|
+
if (!path.isExpressionStatement()) continue;
|
2105
|
+
const loop = path.findParent(path => path.isLoop());
|
2106
|
+
if (loop) {
|
2107
|
+
let uid = loop.getData("expressionReplacementReturnUid");
|
2108
|
+
if (!uid) {
|
2109
|
+
uid = callee.scope.generateDeclaredUidIdentifier("ret");
|
2110
|
+
callee.get("body").pushContainer("body", returnStatement$1(cloneNode$2(uid)));
|
2111
|
+
loop.setData("expressionReplacementReturnUid", uid);
|
2112
|
+
} else {
|
2113
|
+
uid = identifier$1(uid.name);
|
2114
|
+
}
|
2115
|
+
path.get("expression").replaceWith(assignmentExpression$2("=", cloneNode$2(uid), path.node.expression));
|
2116
|
+
} else {
|
2117
|
+
path.replaceWith(returnStatement$1(path.node.expression));
|
2118
|
+
}
|
2119
|
+
}
|
2120
|
+
callee.arrowFunctionToExpression();
|
2121
|
+
const newCallee = callee;
|
2122
|
+
const needToAwaitFunction = isParentAsync && traverse.hasType(this.get("callee.body").node, "AwaitExpression", FUNCTION_TYPES);
|
2123
|
+
const needToYieldFunction = isParentGenerator && traverse.hasType(this.get("callee.body").node, "YieldExpression", FUNCTION_TYPES);
|
2124
|
+
if (needToAwaitFunction) {
|
2125
|
+
newCallee.set("async", true);
|
2126
|
+
if (!needToYieldFunction) {
|
2127
|
+
this.replaceWith(awaitExpression(this.node));
|
2128
|
+
}
|
2129
|
+
}
|
2130
|
+
if (needToYieldFunction) {
|
2131
|
+
newCallee.set("generator", true);
|
2132
|
+
this.replaceWith(yieldExpression(this.node, true));
|
2133
|
+
}
|
2134
|
+
return newCallee.get("body.body");
|
2135
|
+
}
|
2136
|
+
function replaceInline(nodes) {
|
2137
|
+
this.resync();
|
2138
|
+
if (Array.isArray(nodes)) {
|
2139
|
+
if (Array.isArray(this.container)) {
|
2140
|
+
nodes = this._verifyNodeList(nodes);
|
2141
|
+
const paths = this._containerInsertAfter(nodes);
|
2142
|
+
this.remove();
|
2143
|
+
return paths;
|
2144
|
+
} else {
|
2145
|
+
return this.replaceWithMultiple(nodes);
|
2146
|
+
}
|
2147
|
+
} else {
|
2148
|
+
return this.replaceWith(nodes);
|
2149
|
+
}
|
2150
|
+
}
|
2151
|
+
|
2152
|
+
var NodePath_replacement = /*#__PURE__*/Object.freeze({
|
2153
|
+
__proto__: null,
|
2154
|
+
replaceWithMultiple: replaceWithMultiple,
|
2155
|
+
replaceWithSourceString: replaceWithSourceString,
|
2156
|
+
replaceWith: replaceWith,
|
2157
|
+
_replaceWith: _replaceWith,
|
2158
|
+
replaceExpressionWithStatements: replaceExpressionWithStatements,
|
2159
|
+
replaceInline: replaceInline
|
2160
|
+
});
|
2161
|
+
|
2162
|
+
const VALID_CALLEES = ["String", "Number", "Math"];
|
2163
|
+
const INVALID_METHODS = ["random"];
|
2164
|
+
function isValidCallee(val) {
|
2165
|
+
return VALID_CALLEES.includes(val);
|
2166
|
+
}
|
2167
|
+
function isInvalidMethod(val) {
|
2168
|
+
return INVALID_METHODS.includes(val);
|
2169
|
+
}
|
2170
|
+
function evaluateTruthy() {
|
2171
|
+
const res = this.evaluate();
|
2172
|
+
if (res.confident) return !!res.value;
|
2173
|
+
}
|
2174
|
+
function deopt(path, state) {
|
2175
|
+
if (!state.confident) return;
|
2176
|
+
state.deoptPath = path;
|
2177
|
+
state.confident = false;
|
2178
|
+
}
|
2179
|
+
const Globals = new Map([["undefined", undefined], ["Infinity", Infinity], ["NaN", NaN]]);
|
2180
|
+
function evaluateCached(path, state) {
|
2181
|
+
const {
|
2182
|
+
node
|
2183
|
+
} = path;
|
2184
|
+
const {
|
2185
|
+
seen
|
2186
|
+
} = state;
|
2187
|
+
if (seen.has(node)) {
|
2188
|
+
const existing = seen.get(node);
|
2189
|
+
if (existing.resolved) {
|
2190
|
+
return existing.value;
|
2191
|
+
} else {
|
2192
|
+
deopt(path, state);
|
2193
|
+
return;
|
2194
|
+
}
|
2195
|
+
} else {
|
2196
|
+
const item = {
|
2197
|
+
resolved: false
|
2198
|
+
};
|
2199
|
+
seen.set(node, item);
|
2200
|
+
const val = _evaluate(path, state);
|
2201
|
+
if (state.confident) {
|
2202
|
+
item.resolved = true;
|
2203
|
+
item.value = val;
|
2204
|
+
}
|
2205
|
+
return val;
|
2206
|
+
}
|
2207
|
+
}
|
2208
|
+
function _evaluate(path, state) {
|
2209
|
+
if (!state.confident) return;
|
2210
|
+
if (path.isSequenceExpression()) {
|
2211
|
+
const exprs = path.get("expressions");
|
2212
|
+
return evaluateCached(exprs[exprs.length - 1], state);
|
2213
|
+
}
|
2214
|
+
if (path.isStringLiteral() || path.isNumericLiteral() || path.isBooleanLiteral()) {
|
2215
|
+
return path.node.value;
|
2216
|
+
}
|
2217
|
+
if (path.isNullLiteral()) {
|
2218
|
+
return null;
|
2219
|
+
}
|
2220
|
+
if (path.isTemplateLiteral()) {
|
2221
|
+
return evaluateQuasis(path, path.node.quasis, state);
|
2222
|
+
}
|
2223
|
+
if (path.isTaggedTemplateExpression() && path.get("tag").isMemberExpression()) {
|
2224
|
+
const object = path.get("tag.object");
|
2225
|
+
const {
|
2226
|
+
node: {
|
2227
|
+
name
|
2228
|
+
}
|
2229
|
+
} = object;
|
2230
|
+
const property = path.get("tag.property");
|
2231
|
+
if (object.isIdentifier() && name === "String" && !path.scope.getBinding(name) && property.isIdentifier() && property.node.name === "raw") {
|
2232
|
+
return evaluateQuasis(path, path.node.quasi.quasis, state, true);
|
2233
|
+
}
|
2234
|
+
}
|
2235
|
+
if (path.isConditionalExpression()) {
|
2236
|
+
const testResult = evaluateCached(path.get("test"), state);
|
2237
|
+
if (!state.confident) return;
|
2238
|
+
if (testResult) {
|
2239
|
+
return evaluateCached(path.get("consequent"), state);
|
2240
|
+
} else {
|
2241
|
+
return evaluateCached(path.get("alternate"), state);
|
2242
|
+
}
|
2243
|
+
}
|
2244
|
+
if (path.isExpressionWrapper()) {
|
2245
|
+
return evaluateCached(path.get("expression"), state);
|
2246
|
+
}
|
2247
|
+
if (path.isMemberExpression() && !path.parentPath.isCallExpression({
|
2248
|
+
callee: path.node
|
2249
|
+
})) {
|
2250
|
+
const property = path.get("property");
|
2251
|
+
const object = path.get("object");
|
2252
|
+
if (object.isLiteral()) {
|
2253
|
+
const value = object.node.value;
|
2254
|
+
const type = typeof value;
|
2255
|
+
let key = null;
|
2256
|
+
if (path.node.computed) {
|
2257
|
+
key = evaluateCached(property, state);
|
2258
|
+
if (!state.confident) return;
|
2259
|
+
} else if (property.isIdentifier()) {
|
2260
|
+
key = property.node.name;
|
2261
|
+
}
|
2262
|
+
if ((type === "number" || type === "string") && key != null && (typeof key === "number" || typeof key === "string")) {
|
2263
|
+
return value[key];
|
2264
|
+
}
|
2265
|
+
}
|
2266
|
+
}
|
2267
|
+
if (path.isReferencedIdentifier()) {
|
2268
|
+
const binding = path.scope.getBinding(path.node.name);
|
2269
|
+
if (binding) {
|
2270
|
+
if (binding.constantViolations.length > 0 || path.node.start < binding.path.node.end) {
|
2271
|
+
deopt(binding.path, state);
|
2272
|
+
return;
|
2273
|
+
}
|
2274
|
+
if (binding.hasValue) {
|
2275
|
+
return binding.value;
|
2276
|
+
}
|
2277
|
+
}
|
2278
|
+
const name = path.node.name;
|
2279
|
+
if (Globals.has(name)) {
|
2280
|
+
if (!binding) {
|
2281
|
+
return Globals.get(name);
|
2282
|
+
}
|
2283
|
+
deopt(binding.path, state);
|
2284
|
+
return;
|
2285
|
+
}
|
2286
|
+
const resolved = path.resolve();
|
2287
|
+
if (resolved === path) {
|
2288
|
+
deopt(path, state);
|
2289
|
+
return;
|
2290
|
+
} else {
|
2291
|
+
return evaluateCached(resolved, state);
|
2292
|
+
}
|
2293
|
+
}
|
2294
|
+
if (path.isUnaryExpression({
|
2295
|
+
prefix: true
|
2296
|
+
})) {
|
2297
|
+
if (path.node.operator === "void") {
|
2298
|
+
return undefined;
|
2299
|
+
}
|
2300
|
+
const argument = path.get("argument");
|
2301
|
+
if (path.node.operator === "typeof" && (argument.isFunction() || argument.isClass())) {
|
2302
|
+
return "function";
|
2303
|
+
}
|
2304
|
+
const arg = evaluateCached(argument, state);
|
2305
|
+
if (!state.confident) return;
|
2306
|
+
switch (path.node.operator) {
|
2307
|
+
case "!":
|
2308
|
+
return !arg;
|
2309
|
+
case "+":
|
2310
|
+
return +arg;
|
2311
|
+
case "-":
|
2312
|
+
return -arg;
|
2313
|
+
case "~":
|
2314
|
+
return ~arg;
|
2315
|
+
case "typeof":
|
2316
|
+
return typeof arg;
|
2317
|
+
}
|
2318
|
+
}
|
2319
|
+
if (path.isArrayExpression()) {
|
2320
|
+
const arr = [];
|
2321
|
+
const elems = path.get("elements");
|
2322
|
+
for (const elem of elems) {
|
2323
|
+
const elemValue = elem.evaluate();
|
2324
|
+
if (elemValue.confident) {
|
2325
|
+
arr.push(elemValue.value);
|
2326
|
+
} else {
|
2327
|
+
deopt(elemValue.deopt, state);
|
2328
|
+
return;
|
2329
|
+
}
|
2330
|
+
}
|
2331
|
+
return arr;
|
2332
|
+
}
|
2333
|
+
if (path.isObjectExpression()) {
|
2334
|
+
const obj = {};
|
2335
|
+
const props = path.get("properties");
|
2336
|
+
for (const prop of props) {
|
2337
|
+
if (prop.isObjectMethod() || prop.isSpreadElement()) {
|
2338
|
+
deopt(prop, state);
|
2339
|
+
return;
|
2340
|
+
}
|
2341
|
+
const keyPath = prop.get("key");
|
2342
|
+
let key;
|
2343
|
+
if (prop.node.computed) {
|
2344
|
+
key = keyPath.evaluate();
|
2345
|
+
if (!key.confident) {
|
2346
|
+
deopt(key.deopt, state);
|
2347
|
+
return;
|
2348
|
+
}
|
2349
|
+
key = key.value;
|
2350
|
+
} else if (keyPath.isIdentifier()) {
|
2351
|
+
key = keyPath.node.name;
|
2352
|
+
} else {
|
2353
|
+
key = keyPath.node.value;
|
2354
|
+
}
|
2355
|
+
const valuePath = prop.get("value");
|
2356
|
+
let value = valuePath.evaluate();
|
2357
|
+
if (!value.confident) {
|
2358
|
+
deopt(value.deopt, state);
|
2359
|
+
return;
|
2360
|
+
}
|
2361
|
+
value = value.value;
|
2362
|
+
obj[key] = value;
|
2363
|
+
}
|
2364
|
+
return obj;
|
2365
|
+
}
|
2366
|
+
if (path.isLogicalExpression()) {
|
2367
|
+
const wasConfident = state.confident;
|
2368
|
+
const left = evaluateCached(path.get("left"), state);
|
2369
|
+
const leftConfident = state.confident;
|
2370
|
+
state.confident = wasConfident;
|
2371
|
+
const right = evaluateCached(path.get("right"), state);
|
2372
|
+
const rightConfident = state.confident;
|
2373
|
+
switch (path.node.operator) {
|
2374
|
+
case "||":
|
2375
|
+
state.confident = leftConfident && (!!left || rightConfident);
|
2376
|
+
if (!state.confident) return;
|
2377
|
+
return left || right;
|
2378
|
+
case "&&":
|
2379
|
+
state.confident = leftConfident && (!left || rightConfident);
|
2380
|
+
if (!state.confident) return;
|
2381
|
+
return left && right;
|
2382
|
+
case "??":
|
2383
|
+
state.confident = leftConfident && (left != null || rightConfident);
|
2384
|
+
if (!state.confident) return;
|
2385
|
+
return left ?? right;
|
2386
|
+
}
|
2387
|
+
}
|
2388
|
+
if (path.isBinaryExpression()) {
|
2389
|
+
const left = evaluateCached(path.get("left"), state);
|
2390
|
+
if (!state.confident) return;
|
2391
|
+
const right = evaluateCached(path.get("right"), state);
|
2392
|
+
if (!state.confident) return;
|
2393
|
+
switch (path.node.operator) {
|
2394
|
+
case "-":
|
2395
|
+
return left - right;
|
2396
|
+
case "+":
|
2397
|
+
return left + right;
|
2398
|
+
case "/":
|
2399
|
+
return left / right;
|
2400
|
+
case "*":
|
2401
|
+
return left * right;
|
2402
|
+
case "%":
|
2403
|
+
return left % right;
|
2404
|
+
case "**":
|
2405
|
+
return left ** right;
|
2406
|
+
case "<":
|
2407
|
+
return left < right;
|
2408
|
+
case ">":
|
2409
|
+
return left > right;
|
2410
|
+
case "<=":
|
2411
|
+
return left <= right;
|
2412
|
+
case ">=":
|
2413
|
+
return left >= right;
|
2414
|
+
case "==":
|
2415
|
+
return left == right;
|
2416
|
+
case "!=":
|
2417
|
+
return left != right;
|
2418
|
+
case "===":
|
2419
|
+
return left === right;
|
2420
|
+
case "!==":
|
2421
|
+
return left !== right;
|
2422
|
+
case "|":
|
2423
|
+
return left | right;
|
2424
|
+
case "&":
|
2425
|
+
return left & right;
|
2426
|
+
case "^":
|
2427
|
+
return left ^ right;
|
2428
|
+
case "<<":
|
2429
|
+
return left << right;
|
2430
|
+
case ">>":
|
2431
|
+
return left >> right;
|
2432
|
+
case ">>>":
|
2433
|
+
return left >>> right;
|
2434
|
+
}
|
2435
|
+
}
|
2436
|
+
if (path.isCallExpression()) {
|
2437
|
+
const callee = path.get("callee");
|
2438
|
+
let context;
|
2439
|
+
let func;
|
2440
|
+
if (callee.isIdentifier() && !path.scope.getBinding(callee.node.name) && isValidCallee(callee.node.name)) {
|
2441
|
+
func = global[callee.node.name];
|
2442
|
+
}
|
2443
|
+
if (callee.isMemberExpression()) {
|
2444
|
+
const object = callee.get("object");
|
2445
|
+
const property = callee.get("property");
|
2446
|
+
if (object.isIdentifier() && property.isIdentifier() && isValidCallee(object.node.name) && !isInvalidMethod(property.node.name)) {
|
2447
|
+
context = global[object.node.name];
|
2448
|
+
func = context[property.node.name];
|
2449
|
+
}
|
2450
|
+
if (object.isLiteral() && property.isIdentifier()) {
|
2451
|
+
const type = typeof object.node.value;
|
2452
|
+
if (type === "string" || type === "number") {
|
2453
|
+
context = object.node.value;
|
2454
|
+
func = context[property.node.name];
|
2455
|
+
}
|
2456
|
+
}
|
2457
|
+
}
|
2458
|
+
if (func) {
|
2459
|
+
const args = path.get("arguments").map(arg => evaluateCached(arg, state));
|
2460
|
+
if (!state.confident) return;
|
2461
|
+
return func.apply(context, args);
|
2462
|
+
}
|
2463
|
+
}
|
2464
|
+
deopt(path, state);
|
2465
|
+
}
|
2466
|
+
function evaluateQuasis(path, quasis, state, raw = false) {
|
2467
|
+
let str = "";
|
2468
|
+
let i = 0;
|
2469
|
+
const exprs = path.isTemplateLiteral() ? path.get("expressions") : path.get("quasi.expressions");
|
2470
|
+
for (const elem of quasis) {
|
2471
|
+
if (!state.confident) break;
|
2472
|
+
str += raw ? elem.value.raw : elem.value.cooked;
|
2473
|
+
const expr = exprs[i++];
|
2474
|
+
if (expr) str += String(evaluateCached(expr, state));
|
2475
|
+
}
|
2476
|
+
if (!state.confident) return;
|
2477
|
+
return str;
|
2478
|
+
}
|
2479
|
+
function evaluate() {
|
2480
|
+
const state = {
|
2481
|
+
confident: true,
|
2482
|
+
deoptPath: null,
|
2483
|
+
seen: new Map()
|
2484
|
+
};
|
2485
|
+
let value = evaluateCached(this, state);
|
2486
|
+
if (!state.confident) value = undefined;
|
2487
|
+
return {
|
2488
|
+
confident: state.confident,
|
2489
|
+
deopt: state.deoptPath,
|
2490
|
+
value: value
|
2491
|
+
};
|
2492
|
+
}
|
2493
|
+
|
2494
|
+
var NodePath_evaluation = /*#__PURE__*/Object.freeze({
|
2495
|
+
__proto__: null,
|
2496
|
+
evaluateTruthy: evaluateTruthy,
|
2497
|
+
evaluate: evaluate
|
2498
|
+
});
|
2499
|
+
|
2500
|
+
const {
|
2501
|
+
arrowFunctionExpression: arrowFunctionExpression$1,
|
2502
|
+
assignmentExpression: assignmentExpression$1,
|
2503
|
+
binaryExpression,
|
2504
|
+
blockStatement: blockStatement$1,
|
2505
|
+
callExpression: callExpression$1,
|
2506
|
+
conditionalExpression,
|
2507
|
+
expressionStatement: expressionStatement$1,
|
2508
|
+
identifier,
|
2509
|
+
isIdentifier: isIdentifier$3,
|
2510
|
+
jsxIdentifier,
|
2511
|
+
logicalExpression,
|
2512
|
+
LOGICAL_OPERATORS,
|
2513
|
+
memberExpression,
|
2514
|
+
metaProperty,
|
2515
|
+
numericLiteral: numericLiteral$1,
|
2516
|
+
objectExpression,
|
2517
|
+
restElement,
|
2518
|
+
returnStatement,
|
2519
|
+
sequenceExpression,
|
2520
|
+
spreadElement,
|
2521
|
+
stringLiteral,
|
2522
|
+
super: _super,
|
2523
|
+
thisExpression: thisExpression$1,
|
2524
|
+
toExpression,
|
2525
|
+
unaryExpression: unaryExpression$1
|
2526
|
+
} = _t;
|
2527
|
+
function toComputedKey() {
|
2528
|
+
let key;
|
2529
|
+
if (this.isMemberExpression()) {
|
2530
|
+
key = this.node.property;
|
2531
|
+
} else if (this.isProperty() || this.isMethod()) {
|
2532
|
+
key = this.node.key;
|
2533
|
+
} else {
|
2534
|
+
throw new ReferenceError("todo");
|
2535
|
+
}
|
2536
|
+
if (!this.node.computed) {
|
2537
|
+
if (isIdentifier$3(key)) key = stringLiteral(key.name);
|
2538
|
+
}
|
2539
|
+
return key;
|
2540
|
+
}
|
2541
|
+
function ensureBlock() {
|
2542
|
+
const body = this.get("body");
|
2543
|
+
const bodyNode = body.node;
|
2544
|
+
if (Array.isArray(body)) {
|
2545
|
+
throw new Error("Can't convert array path to a block statement");
|
2546
|
+
}
|
2547
|
+
if (!bodyNode) {
|
2548
|
+
throw new Error("Can't convert node without a body");
|
2549
|
+
}
|
2550
|
+
if (body.isBlockStatement()) {
|
2551
|
+
return bodyNode;
|
2552
|
+
}
|
2553
|
+
const statements = [];
|
2554
|
+
let stringPath = "body";
|
2555
|
+
let key;
|
2556
|
+
let listKey;
|
2557
|
+
if (body.isStatement()) {
|
2558
|
+
listKey = "body";
|
2559
|
+
key = 0;
|
2560
|
+
statements.push(body.node);
|
2561
|
+
} else {
|
2562
|
+
stringPath += ".body.0";
|
2563
|
+
if (this.isFunction()) {
|
2564
|
+
key = "argument";
|
2565
|
+
statements.push(returnStatement(body.node));
|
2566
|
+
} else {
|
2567
|
+
key = "expression";
|
2568
|
+
statements.push(expressionStatement$1(body.node));
|
2569
|
+
}
|
2570
|
+
}
|
2571
|
+
this.node.body = blockStatement$1(statements);
|
2572
|
+
const parentPath = this.get(stringPath);
|
2573
|
+
body.setup(parentPath, listKey ? parentPath.node[listKey] : parentPath.node, listKey, key);
|
2574
|
+
return this.node;
|
2575
|
+
}
|
2576
|
+
function unwrapFunctionEnvironment() {
|
2577
|
+
if (!this.isArrowFunctionExpression() && !this.isFunctionExpression() && !this.isFunctionDeclaration()) {
|
2578
|
+
throw this.buildCodeFrameError("Can only unwrap the environment of a function.");
|
2579
|
+
}
|
2580
|
+
hoistFunctionEnvironment(this);
|
2581
|
+
}
|
2582
|
+
function setType(path, type) {
|
2583
|
+
path.node.type = type;
|
2584
|
+
}
|
2585
|
+
function arrowFunctionToExpression({
|
2586
|
+
allowInsertArrow = true,
|
2587
|
+
allowInsertArrowWithRest = allowInsertArrow,
|
2588
|
+
noNewArrows = true
|
2589
|
+
} = {}) {
|
2590
|
+
if (!this.isArrowFunctionExpression()) {
|
2591
|
+
throw this.buildCodeFrameError("Cannot convert non-arrow function to a function expression.");
|
2592
|
+
}
|
2593
|
+
const {
|
2594
|
+
thisBinding,
|
2595
|
+
fnPath: fn
|
2596
|
+
} = hoistFunctionEnvironment(this, noNewArrows, allowInsertArrow, allowInsertArrowWithRest);
|
2597
|
+
fn.ensureBlock();
|
2598
|
+
setType(fn, "FunctionExpression");
|
2599
|
+
if (!noNewArrows) {
|
2600
|
+
const checkBinding = thisBinding ? null : fn.scope.generateUidIdentifier("arrowCheckId");
|
2601
|
+
if (checkBinding) {
|
2602
|
+
fn.parentPath.scope.push({
|
2603
|
+
id: checkBinding,
|
2604
|
+
init: objectExpression([])
|
2605
|
+
});
|
2606
|
+
}
|
2607
|
+
fn.get("body").unshiftContainer("body", expressionStatement$1(callExpression$1(this.hub.addHelper("newArrowCheck"), [thisExpression$1(), checkBinding ? identifier(checkBinding.name) : identifier(thisBinding)])));
|
2608
|
+
fn.replaceWith(callExpression$1(memberExpression(nameFunction(this, true) || fn.node, identifier("bind")), [checkBinding ? identifier(checkBinding.name) : thisExpression$1()]));
|
2609
|
+
return fn.get("callee.object");
|
2610
|
+
}
|
2611
|
+
return fn;
|
2612
|
+
}
|
2613
|
+
const getSuperCallsVisitor = merge([{
|
2614
|
+
CallExpression(child, {
|
2615
|
+
allSuperCalls
|
2616
|
+
}) {
|
2617
|
+
if (!child.get("callee").isSuper()) return;
|
2618
|
+
allSuperCalls.push(child);
|
2619
|
+
}
|
2620
|
+
}, environmentVisitor]);
|
2621
|
+
function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow = true, allowInsertArrowWithRest = true) {
|
2622
|
+
let arrowParent;
|
2623
|
+
let thisEnvFn = fnPath.findParent(p => {
|
2624
|
+
if (p.isArrowFunctionExpression()) {
|
2625
|
+
arrowParent ??= p;
|
2626
|
+
return false;
|
2627
|
+
}
|
2628
|
+
return p.isFunction() || p.isProgram() || p.isClassProperty({
|
2629
|
+
static: false
|
2630
|
+
}) || p.isClassPrivateProperty({
|
2631
|
+
static: false
|
2632
|
+
});
|
2633
|
+
});
|
2634
|
+
const inConstructor = thisEnvFn.isClassMethod({
|
2635
|
+
kind: "constructor"
|
2636
|
+
});
|
2637
|
+
if (thisEnvFn.isClassProperty() || thisEnvFn.isClassPrivateProperty()) {
|
2638
|
+
if (arrowParent) {
|
2639
|
+
thisEnvFn = arrowParent;
|
2640
|
+
} else if (allowInsertArrow) {
|
2641
|
+
fnPath.replaceWith(callExpression$1(arrowFunctionExpression$1([], toExpression(fnPath.node)), []));
|
2642
|
+
thisEnvFn = fnPath.get("callee");
|
2643
|
+
fnPath = thisEnvFn.get("body");
|
2644
|
+
} else {
|
2645
|
+
throw fnPath.buildCodeFrameError("Unable to transform arrow inside class property");
|
2646
|
+
}
|
2647
|
+
}
|
2648
|
+
const {
|
2649
|
+
thisPaths,
|
2650
|
+
argumentsPaths,
|
2651
|
+
newTargetPaths,
|
2652
|
+
superProps,
|
2653
|
+
superCalls
|
2654
|
+
} = getScopeInformation(fnPath);
|
2655
|
+
if (inConstructor && superCalls.length > 0) {
|
2656
|
+
if (!allowInsertArrow) {
|
2657
|
+
throw superCalls[0].buildCodeFrameError("When using '@babel/plugin-transform-arrow-functions', " + "it's not possible to compile `super()` in an arrow function without compiling classes.\n" + "Please add '@babel/plugin-transform-classes' to your Babel configuration.");
|
2658
|
+
}
|
2659
|
+
if (!allowInsertArrowWithRest) {
|
2660
|
+
throw superCalls[0].buildCodeFrameError("When using '@babel/plugin-transform-parameters', " + "it's not possible to compile `super()` in an arrow function with default or rest parameters without compiling classes.\n" + "Please add '@babel/plugin-transform-classes' to your Babel configuration.");
|
2661
|
+
}
|
2662
|
+
const allSuperCalls = [];
|
2663
|
+
thisEnvFn.traverse(getSuperCallsVisitor, {
|
2664
|
+
allSuperCalls
|
2665
|
+
});
|
2666
|
+
const superBinding = getSuperBinding(thisEnvFn);
|
2667
|
+
allSuperCalls.forEach(superCall => {
|
2668
|
+
const callee = identifier(superBinding);
|
2669
|
+
callee.loc = superCall.node.callee.loc;
|
2670
|
+
superCall.get("callee").replaceWith(callee);
|
2671
|
+
});
|
2672
|
+
}
|
2673
|
+
if (argumentsPaths.length > 0) {
|
2674
|
+
const argumentsBinding = getBinding(thisEnvFn, "arguments", () => {
|
2675
|
+
const args = () => identifier("arguments");
|
2676
|
+
if (thisEnvFn.scope.path.isProgram()) {
|
2677
|
+
return conditionalExpression(binaryExpression("===", unaryExpression$1("typeof", args()), stringLiteral("undefined")), thisEnvFn.scope.buildUndefinedNode(), args());
|
2678
|
+
} else {
|
2679
|
+
return args();
|
2680
|
+
}
|
2681
|
+
});
|
2682
|
+
argumentsPaths.forEach(argumentsChild => {
|
2683
|
+
const argsRef = identifier(argumentsBinding);
|
2684
|
+
argsRef.loc = argumentsChild.node.loc;
|
2685
|
+
argumentsChild.replaceWith(argsRef);
|
2686
|
+
});
|
2687
|
+
}
|
2688
|
+
if (newTargetPaths.length > 0) {
|
2689
|
+
const newTargetBinding = getBinding(thisEnvFn, "newtarget", () => metaProperty(identifier("new"), identifier("target")));
|
2690
|
+
newTargetPaths.forEach(targetChild => {
|
2691
|
+
const targetRef = identifier(newTargetBinding);
|
2692
|
+
targetRef.loc = targetChild.node.loc;
|
2693
|
+
targetChild.replaceWith(targetRef);
|
2694
|
+
});
|
2695
|
+
}
|
2696
|
+
if (superProps.length > 0) {
|
2697
|
+
if (!allowInsertArrow) {
|
2698
|
+
throw superProps[0].buildCodeFrameError("When using '@babel/plugin-transform-arrow-functions', " + "it's not possible to compile `super.prop` in an arrow function without compiling classes.\n" + "Please add '@babel/plugin-transform-classes' to your Babel configuration.");
|
2699
|
+
}
|
2700
|
+
const flatSuperProps = superProps.reduce((acc, superProp) => acc.concat(standardizeSuperProperty(superProp)), []);
|
2701
|
+
flatSuperProps.forEach(superProp => {
|
2702
|
+
const key = superProp.node.computed ? "" : superProp.get("property").node.name;
|
2703
|
+
const superParentPath = superProp.parentPath;
|
2704
|
+
const isAssignment = superParentPath.isAssignmentExpression({
|
2705
|
+
left: superProp.node
|
2706
|
+
});
|
2707
|
+
const isCall = superParentPath.isCallExpression({
|
2708
|
+
callee: superProp.node
|
2709
|
+
});
|
2710
|
+
const isTaggedTemplate = superParentPath.isTaggedTemplateExpression({
|
2711
|
+
tag: superProp.node
|
2712
|
+
});
|
2713
|
+
const superBinding = getSuperPropBinding(thisEnvFn, isAssignment, key);
|
2714
|
+
const args = [];
|
2715
|
+
if (superProp.node.computed) {
|
2716
|
+
args.push(superProp.get("property").node);
|
2717
|
+
}
|
2718
|
+
if (isAssignment) {
|
2719
|
+
const value = superParentPath.node.right;
|
2720
|
+
args.push(value);
|
2721
|
+
}
|
2722
|
+
const call = callExpression$1(identifier(superBinding), args);
|
2723
|
+
if (isCall) {
|
2724
|
+
superParentPath.unshiftContainer("arguments", thisExpression$1());
|
2725
|
+
superProp.replaceWith(memberExpression(call, identifier("call")));
|
2726
|
+
thisPaths.push(superParentPath.get("arguments.0"));
|
2727
|
+
} else if (isAssignment) {
|
2728
|
+
superParentPath.replaceWith(call);
|
2729
|
+
} else if (isTaggedTemplate) {
|
2730
|
+
superProp.replaceWith(callExpression$1(memberExpression(call, identifier("bind"), false), [thisExpression$1()]));
|
2731
|
+
thisPaths.push(superProp.get("arguments.0"));
|
2732
|
+
} else {
|
2733
|
+
superProp.replaceWith(call);
|
2734
|
+
}
|
2735
|
+
});
|
2736
|
+
}
|
2737
|
+
let thisBinding;
|
2738
|
+
if (thisPaths.length > 0 || !noNewArrows) {
|
2739
|
+
thisBinding = getThisBinding(thisEnvFn, inConstructor);
|
2740
|
+
if (noNewArrows || inConstructor && hasSuperClass(thisEnvFn)) {
|
2741
|
+
thisPaths.forEach(thisChild => {
|
2742
|
+
const thisRef = thisChild.isJSX() ? jsxIdentifier(thisBinding) : identifier(thisBinding);
|
2743
|
+
thisRef.loc = thisChild.node.loc;
|
2744
|
+
thisChild.replaceWith(thisRef);
|
2745
|
+
});
|
2746
|
+
if (!noNewArrows) thisBinding = null;
|
2747
|
+
}
|
2748
|
+
}
|
2749
|
+
return {
|
2750
|
+
thisBinding,
|
2751
|
+
fnPath
|
2752
|
+
};
|
2753
|
+
}
|
2754
|
+
function isLogicalOp(op) {
|
2755
|
+
return LOGICAL_OPERATORS.includes(op);
|
2756
|
+
}
|
2757
|
+
function standardizeSuperProperty(superProp) {
|
2758
|
+
if (superProp.parentPath.isAssignmentExpression() && superProp.parentPath.node.operator !== "=") {
|
2759
|
+
const assignmentPath = superProp.parentPath;
|
2760
|
+
const op = assignmentPath.node.operator.slice(0, -1);
|
2761
|
+
const value = assignmentPath.node.right;
|
2762
|
+
const isLogicalAssignment = isLogicalOp(op);
|
2763
|
+
if (superProp.node.computed) {
|
2764
|
+
const tmp = superProp.scope.generateDeclaredUidIdentifier("tmp");
|
2765
|
+
const object = superProp.node.object;
|
2766
|
+
const property = superProp.node.property;
|
2767
|
+
assignmentPath.get("left").replaceWith(memberExpression(object, assignmentExpression$1("=", tmp, property), true));
|
2768
|
+
assignmentPath.get("right").replaceWith(rightExpression(isLogicalAssignment ? "=" : op, memberExpression(object, identifier(tmp.name), true), value));
|
2769
|
+
} else {
|
2770
|
+
const object = superProp.node.object;
|
2771
|
+
const property = superProp.node.property;
|
2772
|
+
assignmentPath.get("left").replaceWith(memberExpression(object, property));
|
2773
|
+
assignmentPath.get("right").replaceWith(rightExpression(isLogicalAssignment ? "=" : op, memberExpression(object, identifier(property.name)), value));
|
2774
|
+
}
|
2775
|
+
if (isLogicalAssignment) {
|
2776
|
+
assignmentPath.replaceWith(logicalExpression(op, assignmentPath.node.left, assignmentPath.node.right));
|
2777
|
+
} else {
|
2778
|
+
assignmentPath.node.operator = "=";
|
2779
|
+
}
|
2780
|
+
return [assignmentPath.get("left"), assignmentPath.get("right").get("left")];
|
2781
|
+
} else if (superProp.parentPath.isUpdateExpression()) {
|
2782
|
+
const updateExpr = superProp.parentPath;
|
2783
|
+
const tmp = superProp.scope.generateDeclaredUidIdentifier("tmp");
|
2784
|
+
const computedKey = superProp.node.computed ? superProp.scope.generateDeclaredUidIdentifier("prop") : null;
|
2785
|
+
const parts = [assignmentExpression$1("=", tmp, memberExpression(superProp.node.object, computedKey ? assignmentExpression$1("=", computedKey, superProp.node.property) : superProp.node.property, superProp.node.computed)), assignmentExpression$1("=", memberExpression(superProp.node.object, computedKey ? identifier(computedKey.name) : superProp.node.property, superProp.node.computed), binaryExpression(superProp.parentPath.node.operator[0], identifier(tmp.name), numericLiteral$1(1)))];
|
2786
|
+
if (!superProp.parentPath.node.prefix) {
|
2787
|
+
parts.push(identifier(tmp.name));
|
2788
|
+
}
|
2789
|
+
updateExpr.replaceWith(sequenceExpression(parts));
|
2790
|
+
const left = updateExpr.get("expressions.0.right");
|
2791
|
+
const right = updateExpr.get("expressions.1.left");
|
2792
|
+
return [left, right];
|
2793
|
+
}
|
2794
|
+
return [superProp];
|
2795
|
+
function rightExpression(op, left, right) {
|
2796
|
+
if (op === "=") {
|
2797
|
+
return assignmentExpression$1("=", left, right);
|
2798
|
+
} else {
|
2799
|
+
return binaryExpression(op, left, right);
|
2800
|
+
}
|
2801
|
+
}
|
2802
|
+
}
|
2803
|
+
function hasSuperClass(thisEnvFn) {
|
2804
|
+
return thisEnvFn.isClassMethod() && !!thisEnvFn.parentPath.parentPath.node.superClass;
|
2805
|
+
}
|
2806
|
+
const assignSuperThisVisitor = merge([{
|
2807
|
+
CallExpression(child, {
|
2808
|
+
supers,
|
2809
|
+
thisBinding
|
2810
|
+
}) {
|
2811
|
+
if (!child.get("callee").isSuper()) return;
|
2812
|
+
if (supers.has(child.node)) return;
|
2813
|
+
supers.add(child.node);
|
2814
|
+
child.replaceWithMultiple([child.node, assignmentExpression$1("=", identifier(thisBinding), identifier("this"))]);
|
2815
|
+
}
|
2816
|
+
}, environmentVisitor]);
|
2817
|
+
function getThisBinding(thisEnvFn, inConstructor) {
|
2818
|
+
return getBinding(thisEnvFn, "this", thisBinding => {
|
2819
|
+
if (!inConstructor || !hasSuperClass(thisEnvFn)) return thisExpression$1();
|
2820
|
+
thisEnvFn.traverse(assignSuperThisVisitor, {
|
2821
|
+
supers: new WeakSet(),
|
2822
|
+
thisBinding
|
2823
|
+
});
|
2824
|
+
});
|
2825
|
+
}
|
2826
|
+
function getSuperBinding(thisEnvFn) {
|
2827
|
+
return getBinding(thisEnvFn, "supercall", () => {
|
2828
|
+
const argsBinding = thisEnvFn.scope.generateUidIdentifier("args");
|
2829
|
+
return arrowFunctionExpression$1([restElement(argsBinding)], callExpression$1(_super(), [spreadElement(identifier(argsBinding.name))]));
|
2830
|
+
});
|
2831
|
+
}
|
2832
|
+
function getSuperPropBinding(thisEnvFn, isAssignment, propName) {
|
2833
|
+
const op = isAssignment ? "set" : "get";
|
2834
|
+
return getBinding(thisEnvFn, `superprop_${op}:${propName || ""}`, () => {
|
2835
|
+
const argsList = [];
|
2836
|
+
let fnBody;
|
2837
|
+
if (propName) {
|
2838
|
+
fnBody = memberExpression(_super(), identifier(propName));
|
2839
|
+
} else {
|
2840
|
+
const method = thisEnvFn.scope.generateUidIdentifier("prop");
|
2841
|
+
argsList.unshift(method);
|
2842
|
+
fnBody = memberExpression(_super(), identifier(method.name), true);
|
2843
|
+
}
|
2844
|
+
if (isAssignment) {
|
2845
|
+
const valueIdent = thisEnvFn.scope.generateUidIdentifier("value");
|
2846
|
+
argsList.push(valueIdent);
|
2847
|
+
fnBody = assignmentExpression$1("=", fnBody, identifier(valueIdent.name));
|
2848
|
+
}
|
2849
|
+
return arrowFunctionExpression$1(argsList, fnBody);
|
2850
|
+
});
|
2851
|
+
}
|
2852
|
+
function getBinding(thisEnvFn, key, init) {
|
2853
|
+
const cacheKey = "binding:" + key;
|
2854
|
+
let data = thisEnvFn.getData(cacheKey);
|
2855
|
+
if (!data) {
|
2856
|
+
const id = thisEnvFn.scope.generateUidIdentifier(key);
|
2857
|
+
data = id.name;
|
2858
|
+
thisEnvFn.setData(cacheKey, data);
|
2859
|
+
thisEnvFn.scope.push({
|
2860
|
+
id: id,
|
2861
|
+
init: init(data)
|
2862
|
+
});
|
2863
|
+
}
|
2864
|
+
return data;
|
2865
|
+
}
|
2866
|
+
const getScopeInformationVisitor = merge([{
|
2867
|
+
ThisExpression(child, {
|
2868
|
+
thisPaths
|
2869
|
+
}) {
|
2870
|
+
thisPaths.push(child);
|
2871
|
+
},
|
2872
|
+
JSXIdentifier(child, {
|
2873
|
+
thisPaths
|
2874
|
+
}) {
|
2875
|
+
if (child.node.name !== "this") return;
|
2876
|
+
if (!child.parentPath.isJSXMemberExpression({
|
2877
|
+
object: child.node
|
2878
|
+
}) && !child.parentPath.isJSXOpeningElement({
|
2879
|
+
name: child.node
|
2880
|
+
})) {
|
2881
|
+
return;
|
2882
|
+
}
|
2883
|
+
thisPaths.push(child);
|
2884
|
+
},
|
2885
|
+
CallExpression(child, {
|
2886
|
+
superCalls
|
2887
|
+
}) {
|
2888
|
+
if (child.get("callee").isSuper()) superCalls.push(child);
|
2889
|
+
},
|
2890
|
+
MemberExpression(child, {
|
2891
|
+
superProps
|
2892
|
+
}) {
|
2893
|
+
if (child.get("object").isSuper()) superProps.push(child);
|
2894
|
+
},
|
2895
|
+
Identifier(child, {
|
2896
|
+
argumentsPaths
|
2897
|
+
}) {
|
2898
|
+
if (!child.isReferencedIdentifier({
|
2899
|
+
name: "arguments"
|
2900
|
+
})) return;
|
2901
|
+
let curr = child.scope;
|
2902
|
+
do {
|
2903
|
+
if (curr.hasOwnBinding("arguments")) {
|
2904
|
+
curr.rename("arguments");
|
2905
|
+
return;
|
2906
|
+
}
|
2907
|
+
if (curr.path.isFunction() && !curr.path.isArrowFunctionExpression()) {
|
2908
|
+
break;
|
2909
|
+
}
|
2910
|
+
} while (curr = curr.parent);
|
2911
|
+
argumentsPaths.push(child);
|
2912
|
+
},
|
2913
|
+
MetaProperty(child, {
|
2914
|
+
newTargetPaths
|
2915
|
+
}) {
|
2916
|
+
if (!child.get("meta").isIdentifier({
|
2917
|
+
name: "new"
|
2918
|
+
})) return;
|
2919
|
+
if (!child.get("property").isIdentifier({
|
2920
|
+
name: "target"
|
2921
|
+
})) return;
|
2922
|
+
newTargetPaths.push(child);
|
2923
|
+
}
|
2924
|
+
}, environmentVisitor]);
|
2925
|
+
function getScopeInformation(fnPath) {
|
2926
|
+
const thisPaths = [];
|
2927
|
+
const argumentsPaths = [];
|
2928
|
+
const newTargetPaths = [];
|
2929
|
+
const superProps = [];
|
2930
|
+
const superCalls = [];
|
2931
|
+
fnPath.traverse(getScopeInformationVisitor, {
|
2932
|
+
thisPaths,
|
2933
|
+
argumentsPaths,
|
2934
|
+
newTargetPaths,
|
2935
|
+
superProps,
|
2936
|
+
superCalls
|
2937
|
+
});
|
2938
|
+
return {
|
2939
|
+
thisPaths,
|
2940
|
+
argumentsPaths,
|
2941
|
+
newTargetPaths,
|
2942
|
+
superProps,
|
2943
|
+
superCalls
|
2944
|
+
};
|
2945
|
+
}
|
2946
|
+
|
2947
|
+
var NodePath_conversion = /*#__PURE__*/Object.freeze({
|
2948
|
+
__proto__: null,
|
2949
|
+
toComputedKey: toComputedKey,
|
2950
|
+
ensureBlock: ensureBlock,
|
2951
|
+
unwrapFunctionEnvironment: unwrapFunctionEnvironment,
|
2952
|
+
arrowFunctionToExpression: arrowFunctionToExpression
|
2953
|
+
});
|
2954
|
+
|
2955
|
+
const {
|
2956
|
+
STATEMENT_OR_BLOCK_KEYS,
|
2957
|
+
VISITOR_KEYS: VISITOR_KEYS$3,
|
2958
|
+
isBlockStatement,
|
2959
|
+
isExpression: isExpression$2,
|
2960
|
+
isIdentifier: isIdentifier$2,
|
2961
|
+
isLiteral,
|
2962
|
+
isStringLiteral,
|
2963
|
+
isType,
|
2964
|
+
matchesPattern: _matchesPattern
|
2965
|
+
} = _t;
|
2966
|
+
function matchesPattern(pattern, allowPartial) {
|
2967
|
+
return _matchesPattern(this.node, pattern, allowPartial);
|
2968
|
+
}
|
2969
|
+
function has(key) {
|
2970
|
+
const val = this.node && this.node[key];
|
2971
|
+
if (val && Array.isArray(val)) {
|
2972
|
+
return !!val.length;
|
2973
|
+
} else {
|
2974
|
+
return !!val;
|
2975
|
+
}
|
2976
|
+
}
|
2977
|
+
function isStatic() {
|
2978
|
+
return this.scope.isStatic(this.node);
|
2979
|
+
}
|
2980
|
+
const is = has;
|
2981
|
+
function isnt(key) {
|
2982
|
+
return !this.has(key);
|
2983
|
+
}
|
2984
|
+
function equals(key, value) {
|
2985
|
+
return this.node[key] === value;
|
2986
|
+
}
|
2987
|
+
function isNodeType(type) {
|
2988
|
+
return isType(this.type, type);
|
2989
|
+
}
|
2990
|
+
function canHaveVariableDeclarationOrExpression() {
|
2991
|
+
return (this.key === "init" || this.key === "left") && this.parentPath.isFor();
|
2992
|
+
}
|
2993
|
+
function canSwapBetweenExpressionAndStatement(replacement) {
|
2994
|
+
if (this.key !== "body" || !this.parentPath.isArrowFunctionExpression()) {
|
2995
|
+
return false;
|
2996
|
+
}
|
2997
|
+
if (this.isExpression()) {
|
2998
|
+
return isBlockStatement(replacement);
|
2999
|
+
} else if (this.isBlockStatement()) {
|
3000
|
+
return isExpression$2(replacement);
|
3001
|
+
}
|
3002
|
+
return false;
|
3003
|
+
}
|
3004
|
+
function isCompletionRecord(allowInsideFunction) {
|
3005
|
+
let path = this;
|
3006
|
+
let first = true;
|
3007
|
+
do {
|
3008
|
+
const {
|
3009
|
+
type,
|
3010
|
+
container
|
3011
|
+
} = path;
|
3012
|
+
if (!first && (path.isFunction() || type === "StaticBlock")) {
|
3013
|
+
return !!allowInsideFunction;
|
3014
|
+
}
|
3015
|
+
first = false;
|
3016
|
+
if (Array.isArray(container) && path.key !== container.length - 1) {
|
3017
|
+
return false;
|
3018
|
+
}
|
3019
|
+
} while ((path = path.parentPath) && !path.isProgram() && !path.isDoExpression());
|
3020
|
+
return true;
|
3021
|
+
}
|
3022
|
+
function isStatementOrBlock() {
|
3023
|
+
if (this.parentPath.isLabeledStatement() || isBlockStatement(this.container)) {
|
3024
|
+
return false;
|
3025
|
+
} else {
|
3026
|
+
return STATEMENT_OR_BLOCK_KEYS.includes(this.key);
|
3027
|
+
}
|
3028
|
+
}
|
3029
|
+
function referencesImport(moduleSource, importName) {
|
3030
|
+
if (!this.isReferencedIdentifier()) {
|
3031
|
+
if (this.isJSXMemberExpression() && this.node.property.name === importName || (this.isMemberExpression() || this.isOptionalMemberExpression()) && (this.node.computed ? isStringLiteral(this.node.property, {
|
3032
|
+
value: importName
|
3033
|
+
}) : this.node.property.name === importName)) {
|
3034
|
+
const object = this.get("object");
|
3035
|
+
return object.isReferencedIdentifier() && object.referencesImport(moduleSource, "*");
|
3036
|
+
}
|
3037
|
+
return false;
|
3038
|
+
}
|
3039
|
+
const binding = this.scope.getBinding(this.node.name);
|
3040
|
+
if (!binding || binding.kind !== "module") return false;
|
3041
|
+
const path = binding.path;
|
3042
|
+
const parent = path.parentPath;
|
3043
|
+
if (!parent.isImportDeclaration()) return false;
|
3044
|
+
if (parent.node.source.value === moduleSource) {
|
3045
|
+
if (!importName) return true;
|
3046
|
+
} else {
|
3047
|
+
return false;
|
3048
|
+
}
|
3049
|
+
if (path.isImportDefaultSpecifier() && importName === "default") {
|
3050
|
+
return true;
|
3051
|
+
}
|
3052
|
+
if (path.isImportNamespaceSpecifier() && importName === "*") {
|
3053
|
+
return true;
|
3054
|
+
}
|
3055
|
+
if (path.isImportSpecifier() && isIdentifier$2(path.node.imported, {
|
3056
|
+
name: importName
|
3057
|
+
})) {
|
3058
|
+
return true;
|
3059
|
+
}
|
3060
|
+
return false;
|
3061
|
+
}
|
3062
|
+
function getSource() {
|
3063
|
+
const node = this.node;
|
3064
|
+
if (node.end) {
|
3065
|
+
const code = this.hub.getCode();
|
3066
|
+
if (code) return code.slice(node.start, node.end);
|
3067
|
+
}
|
3068
|
+
return "";
|
3069
|
+
}
|
3070
|
+
function willIMaybeExecuteBefore(target) {
|
3071
|
+
return this._guessExecutionStatusRelativeTo(target) !== "after";
|
3072
|
+
}
|
3073
|
+
function getOuterFunction(path) {
|
3074
|
+
return path.isProgram() ? path : (path.parentPath.scope.getFunctionParent() || path.parentPath.scope.getProgramParent()).path;
|
3075
|
+
}
|
3076
|
+
function isExecutionUncertain(type, key) {
|
3077
|
+
switch (type) {
|
3078
|
+
case "LogicalExpression":
|
3079
|
+
return key === "right";
|
3080
|
+
case "ConditionalExpression":
|
3081
|
+
case "IfStatement":
|
3082
|
+
return key === "consequent" || key === "alternate";
|
3083
|
+
case "WhileStatement":
|
3084
|
+
case "DoWhileStatement":
|
3085
|
+
case "ForInStatement":
|
3086
|
+
case "ForOfStatement":
|
3087
|
+
return key === "body";
|
3088
|
+
case "ForStatement":
|
3089
|
+
return key === "body" || key === "update";
|
3090
|
+
case "SwitchStatement":
|
3091
|
+
return key === "cases";
|
3092
|
+
case "TryStatement":
|
3093
|
+
return key === "handler";
|
3094
|
+
case "AssignmentPattern":
|
3095
|
+
return key === "right";
|
3096
|
+
case "OptionalMemberExpression":
|
3097
|
+
return key === "property";
|
3098
|
+
case "OptionalCallExpression":
|
3099
|
+
return key === "arguments";
|
3100
|
+
default:
|
3101
|
+
return false;
|
3102
|
+
}
|
3103
|
+
}
|
3104
|
+
function isExecutionUncertainInList(paths, maxIndex) {
|
3105
|
+
for (let i = 0; i < maxIndex; i++) {
|
3106
|
+
const path = paths[i];
|
3107
|
+
if (isExecutionUncertain(path.parent.type, path.parentKey)) {
|
3108
|
+
return true;
|
3109
|
+
}
|
3110
|
+
}
|
3111
|
+
return false;
|
3112
|
+
}
|
3113
|
+
const SYMBOL_CHECKING = Symbol();
|
3114
|
+
function _guessExecutionStatusRelativeTo(target) {
|
3115
|
+
return _guessExecutionStatusRelativeToCached(this, target, new Map());
|
3116
|
+
}
|
3117
|
+
function _guessExecutionStatusRelativeToCached(base, target, cache) {
|
3118
|
+
const funcParent = {
|
3119
|
+
this: getOuterFunction(base),
|
3120
|
+
target: getOuterFunction(target)
|
3121
|
+
};
|
3122
|
+
if (funcParent.target.node !== funcParent.this.node) {
|
3123
|
+
return _guessExecutionStatusRelativeToDifferentFunctionsCached(base, funcParent.target, cache);
|
3124
|
+
}
|
3125
|
+
const paths = {
|
3126
|
+
target: target.getAncestry(),
|
3127
|
+
this: base.getAncestry()
|
3128
|
+
};
|
3129
|
+
if (paths.target.indexOf(base) >= 0) return "after";
|
3130
|
+
if (paths.this.indexOf(target) >= 0) return "before";
|
3131
|
+
let commonPath;
|
3132
|
+
const commonIndex = {
|
3133
|
+
target: 0,
|
3134
|
+
this: 0
|
3135
|
+
};
|
3136
|
+
while (!commonPath && commonIndex.this < paths.this.length) {
|
3137
|
+
const path = paths.this[commonIndex.this];
|
3138
|
+
commonIndex.target = paths.target.indexOf(path);
|
3139
|
+
if (commonIndex.target >= 0) {
|
3140
|
+
commonPath = path;
|
3141
|
+
} else {
|
3142
|
+
commonIndex.this++;
|
3143
|
+
}
|
3144
|
+
}
|
3145
|
+
if (!commonPath) {
|
3146
|
+
throw new Error("Internal Babel error - The two compared nodes" + " don't appear to belong to the same program.");
|
3147
|
+
}
|
3148
|
+
if (isExecutionUncertainInList(paths.this, commonIndex.this - 1) || isExecutionUncertainInList(paths.target, commonIndex.target - 1)) {
|
3149
|
+
return "unknown";
|
3150
|
+
}
|
3151
|
+
const divergence = {
|
3152
|
+
this: paths.this[commonIndex.this - 1],
|
3153
|
+
target: paths.target[commonIndex.target - 1]
|
3154
|
+
};
|
3155
|
+
if (divergence.target.listKey && divergence.this.listKey && divergence.target.container === divergence.this.container) {
|
3156
|
+
return divergence.target.key > divergence.this.key ? "before" : "after";
|
3157
|
+
}
|
3158
|
+
const keys = VISITOR_KEYS$3[commonPath.type];
|
3159
|
+
const keyPosition = {
|
3160
|
+
this: keys.indexOf(divergence.this.parentKey),
|
3161
|
+
target: keys.indexOf(divergence.target.parentKey)
|
3162
|
+
};
|
3163
|
+
return keyPosition.target > keyPosition.this ? "before" : "after";
|
3164
|
+
}
|
3165
|
+
function _guessExecutionStatusRelativeToDifferentFunctionsInternal(base, target, cache) {
|
3166
|
+
if (!target.isFunctionDeclaration()) {
|
3167
|
+
if (_guessExecutionStatusRelativeToCached(base, target, cache) === "before") {
|
3168
|
+
return "before";
|
3169
|
+
}
|
3170
|
+
return "unknown";
|
3171
|
+
} else if (target.parentPath.isExportDeclaration()) {
|
3172
|
+
return "unknown";
|
3173
|
+
}
|
3174
|
+
const binding = target.scope.getBinding(target.node.id.name);
|
3175
|
+
if (!binding.references) return "before";
|
3176
|
+
const referencePaths = binding.referencePaths;
|
3177
|
+
let allStatus;
|
3178
|
+
for (const path of referencePaths) {
|
3179
|
+
const childOfFunction = !!path.find(path => path.node === target.node);
|
3180
|
+
if (childOfFunction) continue;
|
3181
|
+
if (path.key !== "callee" || !path.parentPath.isCallExpression()) {
|
3182
|
+
return "unknown";
|
3183
|
+
}
|
3184
|
+
const status = _guessExecutionStatusRelativeToCached(base, path, cache);
|
3185
|
+
if (allStatus && allStatus !== status) {
|
3186
|
+
return "unknown";
|
3187
|
+
} else {
|
3188
|
+
allStatus = status;
|
3189
|
+
}
|
3190
|
+
}
|
3191
|
+
return allStatus;
|
3192
|
+
}
|
3193
|
+
function _guessExecutionStatusRelativeToDifferentFunctionsCached(base, target, cache) {
|
3194
|
+
let nodeMap = cache.get(base.node);
|
3195
|
+
let cached;
|
3196
|
+
if (!nodeMap) {
|
3197
|
+
cache.set(base.node, nodeMap = new Map());
|
3198
|
+
} else if (cached = nodeMap.get(target.node)) {
|
3199
|
+
if (cached === SYMBOL_CHECKING) {
|
3200
|
+
return "unknown";
|
3201
|
+
}
|
3202
|
+
return cached;
|
3203
|
+
}
|
3204
|
+
nodeMap.set(target.node, SYMBOL_CHECKING);
|
3205
|
+
const result = _guessExecutionStatusRelativeToDifferentFunctionsInternal(base, target, cache);
|
3206
|
+
nodeMap.set(target.node, result);
|
3207
|
+
return result;
|
3208
|
+
}
|
3209
|
+
function resolve(dangerous, resolved) {
|
3210
|
+
return this._resolve(dangerous, resolved) || this;
|
3211
|
+
}
|
3212
|
+
function _resolve(dangerous, resolved) {
|
3213
|
+
if (resolved && resolved.indexOf(this) >= 0) return;
|
3214
|
+
resolved = resolved || [];
|
3215
|
+
resolved.push(this);
|
3216
|
+
if (this.isVariableDeclarator()) {
|
3217
|
+
if (this.get("id").isIdentifier()) {
|
3218
|
+
return this.get("init").resolve(dangerous, resolved);
|
3219
|
+
}
|
3220
|
+
} else if (this.isReferencedIdentifier()) {
|
3221
|
+
const binding = this.scope.getBinding(this.node.name);
|
3222
|
+
if (!binding) return;
|
3223
|
+
if (!binding.constant) return;
|
3224
|
+
if (binding.kind === "module") return;
|
3225
|
+
if (binding.path !== this) {
|
3226
|
+
const ret = binding.path.resolve(dangerous, resolved);
|
3227
|
+
if (this.find(parent => parent.node === ret.node)) return;
|
3228
|
+
return ret;
|
3229
|
+
}
|
3230
|
+
} else if (this.isTypeCastExpression()) {
|
3231
|
+
return this.get("expression").resolve(dangerous, resolved);
|
3232
|
+
} else if (dangerous && this.isMemberExpression()) {
|
3233
|
+
const targetKey = this.toComputedKey();
|
3234
|
+
if (!isLiteral(targetKey)) return;
|
3235
|
+
const targetName = targetKey.value;
|
3236
|
+
const target = this.get("object").resolve(dangerous, resolved);
|
3237
|
+
if (target.isObjectExpression()) {
|
3238
|
+
const props = target.get("properties");
|
3239
|
+
for (const prop of props) {
|
3240
|
+
if (!prop.isProperty()) continue;
|
3241
|
+
const key = prop.get("key");
|
3242
|
+
let match = prop.isnt("computed") && key.isIdentifier({
|
3243
|
+
name: targetName
|
3244
|
+
});
|
3245
|
+
match = match || key.isLiteral({
|
3246
|
+
value: targetName
|
3247
|
+
});
|
3248
|
+
if (match) return prop.get("value").resolve(dangerous, resolved);
|
3249
|
+
}
|
3250
|
+
} else if (target.isArrayExpression() && !isNaN(+targetName)) {
|
3251
|
+
const elems = target.get("elements");
|
3252
|
+
const elem = elems[targetName];
|
3253
|
+
if (elem) return elem.resolve(dangerous, resolved);
|
3254
|
+
}
|
3255
|
+
}
|
3256
|
+
}
|
3257
|
+
function isConstantExpression() {
|
3258
|
+
if (this.isIdentifier()) {
|
3259
|
+
const binding = this.scope.getBinding(this.node.name);
|
3260
|
+
if (!binding) return false;
|
3261
|
+
return binding.constant;
|
3262
|
+
}
|
3263
|
+
if (this.isLiteral()) {
|
3264
|
+
if (this.isRegExpLiteral()) {
|
3265
|
+
return false;
|
3266
|
+
}
|
3267
|
+
if (this.isTemplateLiteral()) {
|
3268
|
+
return this.get("expressions").every(expression => expression.isConstantExpression());
|
3269
|
+
}
|
3270
|
+
return true;
|
3271
|
+
}
|
3272
|
+
if (this.isUnaryExpression()) {
|
3273
|
+
if (this.node.operator !== "void") {
|
3274
|
+
return false;
|
3275
|
+
}
|
3276
|
+
return this.get("argument").isConstantExpression();
|
3277
|
+
}
|
3278
|
+
if (this.isBinaryExpression()) {
|
3279
|
+
const {
|
3280
|
+
operator
|
3281
|
+
} = this.node;
|
3282
|
+
return operator !== "in" && operator !== "instanceof" && this.get("left").isConstantExpression() && this.get("right").isConstantExpression();
|
3283
|
+
}
|
3284
|
+
return false;
|
3285
|
+
}
|
3286
|
+
function isInStrictMode() {
|
3287
|
+
const start = this.isProgram() ? this : this.parentPath;
|
3288
|
+
const strictParent = start.find(path => {
|
3289
|
+
if (path.isProgram({
|
3290
|
+
sourceType: "module"
|
3291
|
+
})) return true;
|
3292
|
+
if (path.isClass()) return true;
|
3293
|
+
if (path.isArrowFunctionExpression() && !path.get("body").isBlockStatement()) {
|
3294
|
+
return false;
|
3295
|
+
}
|
3296
|
+
let body;
|
3297
|
+
if (path.isFunction()) {
|
3298
|
+
body = path.node.body;
|
3299
|
+
} else if (path.isProgram()) {
|
3300
|
+
body = path.node;
|
3301
|
+
} else {
|
3302
|
+
return false;
|
3303
|
+
}
|
3304
|
+
for (const directive of body.directives) {
|
3305
|
+
if (directive.value.value === "use strict") {
|
3306
|
+
return true;
|
3307
|
+
}
|
3308
|
+
}
|
3309
|
+
});
|
3310
|
+
return !!strictParent;
|
3311
|
+
}
|
3312
|
+
|
3313
|
+
var NodePath_introspection = /*#__PURE__*/Object.freeze({
|
3314
|
+
__proto__: null,
|
3315
|
+
matchesPattern: matchesPattern,
|
3316
|
+
has: has,
|
3317
|
+
isStatic: isStatic,
|
3318
|
+
is: is,
|
3319
|
+
isnt: isnt,
|
3320
|
+
equals: equals,
|
3321
|
+
isNodeType: isNodeType,
|
3322
|
+
canHaveVariableDeclarationOrExpression: canHaveVariableDeclarationOrExpression,
|
3323
|
+
canSwapBetweenExpressionAndStatement: canSwapBetweenExpressionAndStatement,
|
3324
|
+
isCompletionRecord: isCompletionRecord,
|
3325
|
+
isStatementOrBlock: isStatementOrBlock,
|
3326
|
+
referencesImport: referencesImport,
|
3327
|
+
getSource: getSource,
|
3328
|
+
willIMaybeExecuteBefore: willIMaybeExecuteBefore,
|
3329
|
+
_guessExecutionStatusRelativeTo: _guessExecutionStatusRelativeTo,
|
3330
|
+
resolve: resolve,
|
3331
|
+
_resolve: _resolve,
|
3332
|
+
isConstantExpression: isConstantExpression,
|
3333
|
+
isInStrictMode: isInStrictMode
|
3334
|
+
});
|
3335
|
+
|
3336
|
+
function call(key) {
|
3337
|
+
const opts = this.opts;
|
3338
|
+
this.debug(key);
|
3339
|
+
if (this.node) {
|
3340
|
+
if (this._call(opts[key])) return true;
|
3341
|
+
}
|
3342
|
+
if (this.node) {
|
3343
|
+
return this._call(opts[this.node.type]?.[key]);
|
3344
|
+
}
|
3345
|
+
return false;
|
3346
|
+
}
|
3347
|
+
function _call(fns) {
|
3348
|
+
if (!fns) return false;
|
3349
|
+
for (const fn of fns) {
|
3350
|
+
if (!fn) continue;
|
3351
|
+
const node = this.node;
|
3352
|
+
if (!node) return true;
|
3353
|
+
const ret = fn.call(this.state, this, this.state);
|
3354
|
+
if (ret && typeof ret === "object" && typeof ret.then === "function") {
|
3355
|
+
throw new Error(`You appear to be using a plugin with an async traversal visitor, ` + `which your current version of Babel does not support. ` + `If you're using a published plugin, you may need to upgrade ` + `your @babel/core version.`);
|
3356
|
+
}
|
3357
|
+
if (ret) {
|
3358
|
+
throw new Error(`Unexpected return value from visitor method ${fn}`);
|
3359
|
+
}
|
3360
|
+
if (this.node !== node) return true;
|
3361
|
+
if (this._traverseFlags > 0) return true;
|
3362
|
+
}
|
3363
|
+
return false;
|
3364
|
+
}
|
3365
|
+
function isDenylisted() {
|
3366
|
+
const denylist = this.opts.denylist ?? this.opts.blacklist;
|
3367
|
+
return denylist && denylist.indexOf(this.node.type) > -1;
|
3368
|
+
}
|
3369
|
+
function restoreContext(path, context) {
|
3370
|
+
if (path.context !== context) {
|
3371
|
+
path.context = context;
|
3372
|
+
path.state = context.state;
|
3373
|
+
path.opts = context.opts;
|
3374
|
+
}
|
3375
|
+
}
|
3376
|
+
function visit() {
|
3377
|
+
if (!this.node) {
|
3378
|
+
return false;
|
3379
|
+
}
|
3380
|
+
if (this.isDenylisted()) {
|
3381
|
+
return false;
|
3382
|
+
}
|
3383
|
+
if (this.opts.shouldSkip?.(this)) {
|
3384
|
+
return false;
|
3385
|
+
}
|
3386
|
+
const currentContext = this.context;
|
3387
|
+
if (this.shouldSkip || this.call("enter")) {
|
3388
|
+
this.debug("Skip...");
|
3389
|
+
return this.shouldStop;
|
3390
|
+
}
|
3391
|
+
restoreContext(this, currentContext);
|
3392
|
+
this.debug("Recursing into...");
|
3393
|
+
this.shouldStop = traverseNode(this.node, this.opts, this.scope, this.state, this, this.skipKeys);
|
3394
|
+
restoreContext(this, currentContext);
|
3395
|
+
this.call("exit");
|
3396
|
+
return this.shouldStop;
|
3397
|
+
}
|
3398
|
+
function skip() {
|
3399
|
+
this.shouldSkip = true;
|
3400
|
+
}
|
3401
|
+
function skipKey(key) {
|
3402
|
+
if (this.skipKeys == null) {
|
3403
|
+
this.skipKeys = {};
|
3404
|
+
}
|
3405
|
+
this.skipKeys[key] = true;
|
3406
|
+
}
|
3407
|
+
function stop() {
|
3408
|
+
this._traverseFlags |= SHOULD_SKIP | SHOULD_STOP;
|
3409
|
+
}
|
3410
|
+
function setScope() {
|
3411
|
+
if (this.opts?.noScope) return;
|
3412
|
+
let path = this.parentPath;
|
3413
|
+
if ((this.key === "key" || this.listKey === "decorators") && path.isMethod() || this.key === "discriminant" && path.isSwitchStatement()) {
|
3414
|
+
path = path.parentPath;
|
3415
|
+
}
|
3416
|
+
let target;
|
3417
|
+
while (path && !target) {
|
3418
|
+
if (path.opts?.noScope) return;
|
3419
|
+
target = path.scope;
|
3420
|
+
path = path.parentPath;
|
3421
|
+
}
|
3422
|
+
this.scope = this.getScope(target);
|
3423
|
+
this.scope?.init();
|
3424
|
+
}
|
3425
|
+
function setContext(context) {
|
3426
|
+
if (this.skipKeys != null) {
|
3427
|
+
this.skipKeys = {};
|
3428
|
+
}
|
3429
|
+
this._traverseFlags = 0;
|
3430
|
+
if (context) {
|
3431
|
+
this.context = context;
|
3432
|
+
this.state = context.state;
|
3433
|
+
this.opts = context.opts;
|
3434
|
+
}
|
3435
|
+
this.setScope();
|
3436
|
+
return this;
|
3437
|
+
}
|
3438
|
+
function resync() {
|
3439
|
+
if (this.removed) return;
|
3440
|
+
this._resyncParent();
|
3441
|
+
this._resyncList();
|
3442
|
+
this._resyncKey();
|
3443
|
+
}
|
3444
|
+
function _resyncParent() {
|
3445
|
+
if (this.parentPath) {
|
3446
|
+
this.parent = this.parentPath.node;
|
3447
|
+
}
|
3448
|
+
}
|
3449
|
+
function _resyncKey() {
|
3450
|
+
if (!this.container) return;
|
3451
|
+
if (this.node === this.container[this.key]) {
|
3452
|
+
return;
|
3453
|
+
}
|
3454
|
+
if (Array.isArray(this.container)) {
|
3455
|
+
for (let i = 0; i < this.container.length; i++) {
|
3456
|
+
if (this.container[i] === this.node) {
|
3457
|
+
this.setKey(i);
|
3458
|
+
return;
|
3459
|
+
}
|
3460
|
+
}
|
3461
|
+
} else {
|
3462
|
+
for (const key of Object.keys(this.container)) {
|
3463
|
+
if (this.container[key] === this.node) {
|
3464
|
+
this.setKey(key);
|
3465
|
+
return;
|
3466
|
+
}
|
3467
|
+
}
|
3468
|
+
}
|
3469
|
+
this.key = null;
|
3470
|
+
}
|
3471
|
+
function _resyncList() {
|
3472
|
+
if (!this.parent || !this.inList) return;
|
3473
|
+
const newContainer = this.parent[this.listKey];
|
3474
|
+
if (this.container === newContainer) return;
|
3475
|
+
this.container = newContainer || null;
|
3476
|
+
}
|
3477
|
+
function _resyncRemoved() {
|
3478
|
+
if (this.key == null || !this.container || this.container[this.key] !== this.node) {
|
3479
|
+
this._markRemoved();
|
3480
|
+
}
|
3481
|
+
}
|
3482
|
+
function popContext() {
|
3483
|
+
this.contexts.pop();
|
3484
|
+
if (this.contexts.length > 0) {
|
3485
|
+
this.setContext(this.contexts[this.contexts.length - 1]);
|
3486
|
+
} else {
|
3487
|
+
this.setContext(undefined);
|
3488
|
+
}
|
3489
|
+
}
|
3490
|
+
function pushContext(context) {
|
3491
|
+
this.contexts.push(context);
|
3492
|
+
this.setContext(context);
|
3493
|
+
}
|
3494
|
+
function setup(parentPath, container, listKey, key) {
|
3495
|
+
this.listKey = listKey;
|
3496
|
+
this.container = container;
|
3497
|
+
this.parentPath = parentPath || this.parentPath;
|
3498
|
+
this.setKey(key);
|
3499
|
+
}
|
3500
|
+
function setKey(key) {
|
3501
|
+
this.key = key;
|
3502
|
+
this.node = this.container[this.key];
|
3503
|
+
this.type = this.node?.type;
|
3504
|
+
}
|
3505
|
+
function requeue(pathToQueue = this) {
|
3506
|
+
if (pathToQueue.removed) return;
|
3507
|
+
{
|
3508
|
+
pathToQueue.shouldSkip = false;
|
3509
|
+
}
|
3510
|
+
const contexts = this.contexts;
|
3511
|
+
for (const context of contexts) {
|
3512
|
+
context.maybeQueue(pathToQueue);
|
3513
|
+
}
|
3514
|
+
}
|
3515
|
+
function _getQueueContexts() {
|
3516
|
+
let path = this;
|
3517
|
+
let contexts = this.contexts;
|
3518
|
+
while (!contexts.length) {
|
3519
|
+
path = path.parentPath;
|
3520
|
+
if (!path) break;
|
3521
|
+
contexts = path.contexts;
|
3522
|
+
}
|
3523
|
+
return contexts;
|
3524
|
+
}
|
3525
|
+
|
3526
|
+
var NodePath_context = /*#__PURE__*/Object.freeze({
|
3527
|
+
__proto__: null,
|
3528
|
+
call: call,
|
3529
|
+
_call: _call,
|
3530
|
+
isDenylisted: isDenylisted,
|
3531
|
+
isBlacklisted: isDenylisted,
|
3532
|
+
visit: visit,
|
3533
|
+
skip: skip,
|
3534
|
+
skipKey: skipKey,
|
3535
|
+
stop: stop,
|
3536
|
+
setScope: setScope,
|
3537
|
+
setContext: setContext,
|
3538
|
+
resync: resync,
|
3539
|
+
_resyncParent: _resyncParent,
|
3540
|
+
_resyncKey: _resyncKey,
|
3541
|
+
_resyncList: _resyncList,
|
3542
|
+
_resyncRemoved: _resyncRemoved,
|
3543
|
+
popContext: popContext,
|
3544
|
+
pushContext: pushContext,
|
3545
|
+
setup: setup,
|
3546
|
+
setKey: setKey,
|
3547
|
+
requeue: requeue,
|
3548
|
+
_getQueueContexts: _getQueueContexts
|
3549
|
+
});
|
3550
|
+
|
3551
|
+
const hooks = [function (self, parent) {
|
3552
|
+
const removeParent = self.key === "test" && (parent.isWhile() || parent.isSwitchCase()) || self.key === "declaration" && parent.isExportDeclaration() || self.key === "body" && parent.isLabeledStatement() || self.listKey === "declarations" && parent.isVariableDeclaration() && parent.node.declarations.length === 1 || self.key === "expression" && parent.isExpressionStatement();
|
3553
|
+
if (removeParent) {
|
3554
|
+
parent.remove();
|
3555
|
+
return true;
|
3556
|
+
}
|
3557
|
+
}, function (self, parent) {
|
3558
|
+
if (parent.isSequenceExpression() && parent.node.expressions.length === 1) {
|
3559
|
+
parent.replaceWith(parent.node.expressions[0]);
|
3560
|
+
return true;
|
3561
|
+
}
|
3562
|
+
}, function (self, parent) {
|
3563
|
+
if (parent.isBinary()) {
|
3564
|
+
if (self.key === "left") {
|
3565
|
+
parent.replaceWith(parent.node.right);
|
3566
|
+
} else {
|
3567
|
+
parent.replaceWith(parent.node.left);
|
3568
|
+
}
|
3569
|
+
return true;
|
3570
|
+
}
|
3571
|
+
}, function (self, parent) {
|
3572
|
+
if (parent.isIfStatement() && self.key === "consequent" || self.key === "body" && (parent.isLoop() || parent.isArrowFunctionExpression())) {
|
3573
|
+
self.replaceWith({
|
3574
|
+
type: "BlockStatement",
|
3575
|
+
body: []
|
3576
|
+
});
|
3577
|
+
return true;
|
3578
|
+
}
|
3579
|
+
}];
|
3580
|
+
|
3581
|
+
function remove() {
|
3582
|
+
this._assertUnremoved();
|
3583
|
+
this.resync();
|
3584
|
+
if (!this.opts?.noScope) {
|
3585
|
+
this._removeFromScope();
|
3586
|
+
}
|
3587
|
+
if (this._callRemovalHooks()) {
|
3588
|
+
this._markRemoved();
|
3589
|
+
return;
|
3590
|
+
}
|
3591
|
+
this.shareCommentsWithSiblings();
|
3592
|
+
this._remove();
|
3593
|
+
this._markRemoved();
|
3594
|
+
}
|
3595
|
+
function _removeFromScope() {
|
3596
|
+
const bindings = this.getBindingIdentifiers();
|
3597
|
+
Object.keys(bindings).forEach(name => this.scope.removeBinding(name));
|
3598
|
+
}
|
3599
|
+
function _callRemovalHooks() {
|
3600
|
+
for (const fn of hooks) {
|
3601
|
+
if (fn(this, this.parentPath)) return true;
|
3602
|
+
}
|
3603
|
+
}
|
3604
|
+
function _remove() {
|
3605
|
+
if (Array.isArray(this.container)) {
|
3606
|
+
this.container.splice(this.key, 1);
|
3607
|
+
this.updateSiblingKeys(this.key, -1);
|
3608
|
+
} else {
|
3609
|
+
this._replaceWith(null);
|
3610
|
+
}
|
3611
|
+
}
|
3612
|
+
function _markRemoved() {
|
3613
|
+
this._traverseFlags |= SHOULD_SKIP | REMOVED;
|
3614
|
+
if (this.parent) path.get(this.parent).delete(this.node);
|
3615
|
+
this.node = null;
|
3616
|
+
}
|
3617
|
+
function _assertUnremoved() {
|
3618
|
+
if (this.removed) {
|
3619
|
+
throw this.buildCodeFrameError("NodePath has been removed so is read-only.");
|
3620
|
+
}
|
3621
|
+
}
|
3622
|
+
|
3623
|
+
var NodePath_removal = /*#__PURE__*/Object.freeze({
|
3624
|
+
__proto__: null,
|
3625
|
+
remove: remove,
|
3626
|
+
_removeFromScope: _removeFromScope,
|
3627
|
+
_callRemovalHooks: _callRemovalHooks,
|
3628
|
+
_remove: _remove,
|
3629
|
+
_markRemoved: _markRemoved,
|
3630
|
+
_assertUnremoved: _assertUnremoved
|
3631
|
+
});
|
3632
|
+
|
3633
|
+
const {
|
3634
|
+
react: react$1
|
3635
|
+
} = _t;
|
3636
|
+
const {
|
3637
|
+
cloneNode: cloneNode$1,
|
3638
|
+
jsxExpressionContainer,
|
3639
|
+
variableDeclaration,
|
3640
|
+
variableDeclarator
|
3641
|
+
} = _t;
|
3642
|
+
const referenceVisitor = {
|
3643
|
+
ReferencedIdentifier(path, state) {
|
3644
|
+
if (path.isJSXIdentifier() && react$1.isCompatTag(path.node.name) && !path.parentPath.isJSXMemberExpression()) {
|
3645
|
+
return;
|
3646
|
+
}
|
3647
|
+
if (path.node.name === "this") {
|
3648
|
+
let scope = path.scope;
|
3649
|
+
do {
|
3650
|
+
if (scope.path.isFunction() && !scope.path.isArrowFunctionExpression()) {
|
3651
|
+
break;
|
3652
|
+
}
|
3653
|
+
} while (scope = scope.parent);
|
3654
|
+
if (scope) state.breakOnScopePaths.push(scope.path);
|
3655
|
+
}
|
3656
|
+
const binding = path.scope.getBinding(path.node.name);
|
3657
|
+
if (!binding) return;
|
3658
|
+
for (const violation of binding.constantViolations) {
|
3659
|
+
if (violation.scope !== binding.path.scope) {
|
3660
|
+
state.mutableBinding = true;
|
3661
|
+
path.stop();
|
3662
|
+
return;
|
3663
|
+
}
|
3664
|
+
}
|
3665
|
+
if (binding !== state.scope.getBinding(path.node.name)) return;
|
3666
|
+
state.bindings[path.node.name] = binding;
|
3667
|
+
}
|
3668
|
+
};
|
3669
|
+
class PathHoister {
|
3670
|
+
breakOnScopePaths;
|
3671
|
+
bindings;
|
3672
|
+
mutableBinding;
|
3673
|
+
scopes;
|
3674
|
+
scope;
|
3675
|
+
path;
|
3676
|
+
attachAfter;
|
3677
|
+
constructor(path, scope) {
|
3678
|
+
this.breakOnScopePaths = [];
|
3679
|
+
this.bindings = {};
|
3680
|
+
this.mutableBinding = false;
|
3681
|
+
this.scopes = [];
|
3682
|
+
this.scope = scope;
|
3683
|
+
this.path = path;
|
3684
|
+
this.attachAfter = false;
|
3685
|
+
}
|
3686
|
+
isCompatibleScope(scope) {
|
3687
|
+
for (const key of Object.keys(this.bindings)) {
|
3688
|
+
const binding = this.bindings[key];
|
3689
|
+
if (!scope.bindingIdentifierEquals(key, binding.identifier)) {
|
3690
|
+
return false;
|
3691
|
+
}
|
3692
|
+
}
|
3693
|
+
return true;
|
3694
|
+
}
|
3695
|
+
getCompatibleScopes() {
|
3696
|
+
let scope = this.path.scope;
|
3697
|
+
do {
|
3698
|
+
if (this.isCompatibleScope(scope)) {
|
3699
|
+
this.scopes.push(scope);
|
3700
|
+
} else {
|
3701
|
+
break;
|
3702
|
+
}
|
3703
|
+
if (this.breakOnScopePaths.indexOf(scope.path) >= 0) {
|
3704
|
+
break;
|
3705
|
+
}
|
3706
|
+
} while (scope = scope.parent);
|
3707
|
+
}
|
3708
|
+
getAttachmentPath() {
|
3709
|
+
let path = this._getAttachmentPath();
|
3710
|
+
if (!path) return;
|
3711
|
+
let targetScope = path.scope;
|
3712
|
+
if (targetScope.path === path) {
|
3713
|
+
targetScope = path.scope.parent;
|
3714
|
+
}
|
3715
|
+
if (targetScope.path.isProgram() || targetScope.path.isFunction()) {
|
3716
|
+
for (const name of Object.keys(this.bindings)) {
|
3717
|
+
if (!targetScope.hasOwnBinding(name)) continue;
|
3718
|
+
const binding = this.bindings[name];
|
3719
|
+
if (binding.kind === "param" || binding.path.parentKey === "params") {
|
3720
|
+
continue;
|
3721
|
+
}
|
3722
|
+
const bindingParentPath = this.getAttachmentParentForPath(binding.path);
|
3723
|
+
if (bindingParentPath.key >= path.key) {
|
3724
|
+
this.attachAfter = true;
|
3725
|
+
path = binding.path;
|
3726
|
+
for (const violationPath of binding.constantViolations) {
|
3727
|
+
if (this.getAttachmentParentForPath(violationPath).key > path.key) {
|
3728
|
+
path = violationPath;
|
3729
|
+
}
|
3730
|
+
}
|
3731
|
+
}
|
3732
|
+
}
|
3733
|
+
}
|
3734
|
+
return path;
|
3735
|
+
}
|
3736
|
+
_getAttachmentPath() {
|
3737
|
+
const scopes = this.scopes;
|
3738
|
+
const scope = scopes.pop();
|
3739
|
+
if (!scope) return;
|
3740
|
+
if (scope.path.isFunction()) {
|
3741
|
+
if (this.hasOwnParamBindings(scope)) {
|
3742
|
+
if (this.scope === scope) return;
|
3743
|
+
const bodies = scope.path.get("body").get("body");
|
3744
|
+
for (let i = 0; i < bodies.length; i++) {
|
3745
|
+
if (bodies[i].node._blockHoist) continue;
|
3746
|
+
return bodies[i];
|
3747
|
+
}
|
3748
|
+
} else {
|
3749
|
+
return this.getNextScopeAttachmentParent();
|
3750
|
+
}
|
3751
|
+
} else if (scope.path.isProgram()) {
|
3752
|
+
return this.getNextScopeAttachmentParent();
|
3753
|
+
}
|
3754
|
+
}
|
3755
|
+
getNextScopeAttachmentParent() {
|
3756
|
+
const scope = this.scopes.pop();
|
3757
|
+
if (scope) return this.getAttachmentParentForPath(scope.path);
|
3758
|
+
}
|
3759
|
+
getAttachmentParentForPath(path) {
|
3760
|
+
do {
|
3761
|
+
if (!path.parentPath || Array.isArray(path.container) && path.isStatement()) {
|
3762
|
+
return path;
|
3763
|
+
}
|
3764
|
+
} while (path = path.parentPath);
|
3765
|
+
}
|
3766
|
+
hasOwnParamBindings(scope) {
|
3767
|
+
for (const name of Object.keys(this.bindings)) {
|
3768
|
+
if (!scope.hasOwnBinding(name)) continue;
|
3769
|
+
const binding = this.bindings[name];
|
3770
|
+
if (binding.kind === "param" && binding.constant) return true;
|
3771
|
+
}
|
3772
|
+
return false;
|
3773
|
+
}
|
3774
|
+
run() {
|
3775
|
+
this.path.traverse(referenceVisitor, this);
|
3776
|
+
if (this.mutableBinding) return;
|
3777
|
+
this.getCompatibleScopes();
|
3778
|
+
const attachTo = this.getAttachmentPath();
|
3779
|
+
if (!attachTo) return;
|
3780
|
+
if (attachTo.getFunctionParent() === this.path.getFunctionParent()) return;
|
3781
|
+
let uid = attachTo.scope.generateUidIdentifier("ref");
|
3782
|
+
const declarator = variableDeclarator(uid, this.path.node);
|
3783
|
+
const insertFn = this.attachAfter ? "insertAfter" : "insertBefore";
|
3784
|
+
const [attached] = attachTo[insertFn]([attachTo.isVariableDeclarator() ? declarator : variableDeclaration("var", [declarator])]);
|
3785
|
+
const parent = this.path.parentPath;
|
3786
|
+
if (parent.isJSXElement() && this.path.container === parent.node.children) {
|
3787
|
+
uid = jsxExpressionContainer(uid);
|
3788
|
+
}
|
3789
|
+
this.path.replaceWith(cloneNode$1(uid));
|
3790
|
+
return attachTo.isVariableDeclarator() ? attached.get("init") : attached.get("declarations.0.init");
|
3791
|
+
}
|
3792
|
+
}
|
3793
|
+
|
3794
|
+
const {
|
3795
|
+
arrowFunctionExpression,
|
3796
|
+
assertExpression,
|
3797
|
+
assignmentExpression,
|
3798
|
+
blockStatement,
|
3799
|
+
callExpression,
|
3800
|
+
cloneNode,
|
3801
|
+
expressionStatement,
|
3802
|
+
isAssignmentExpression,
|
3803
|
+
isCallExpression,
|
3804
|
+
isExportNamedDeclaration,
|
3805
|
+
isExpression: isExpression$1,
|
3806
|
+
isIdentifier: isIdentifier$1,
|
3807
|
+
isSequenceExpression,
|
3808
|
+
isSuper,
|
3809
|
+
thisExpression
|
3810
|
+
} = _t;
|
3811
|
+
function insertBefore(nodes_) {
|
3812
|
+
this._assertUnremoved();
|
3813
|
+
const nodes = this._verifyNodeList(nodes_);
|
3814
|
+
const {
|
3815
|
+
parentPath,
|
3816
|
+
parent
|
3817
|
+
} = this;
|
3818
|
+
if (parentPath.isExpressionStatement() || parentPath.isLabeledStatement() || isExportNamedDeclaration(parent) || parentPath.isExportDefaultDeclaration() && this.isDeclaration()) {
|
3819
|
+
return parentPath.insertBefore(nodes);
|
3820
|
+
} else if (this.isNodeType("Expression") && !this.isJSXElement() || parentPath.isForStatement() && this.key === "init") {
|
3821
|
+
if (this.node) nodes.push(this.node);
|
3822
|
+
return this.replaceExpressionWithStatements(nodes);
|
3823
|
+
} else if (Array.isArray(this.container)) {
|
3824
|
+
return this._containerInsertBefore(nodes);
|
3825
|
+
} else if (this.isStatementOrBlock()) {
|
3826
|
+
const node = this.node;
|
3827
|
+
const shouldInsertCurrentNode = node && (!this.isExpressionStatement() || node.expression != null);
|
3828
|
+
this.replaceWith(blockStatement(shouldInsertCurrentNode ? [node] : []));
|
3829
|
+
return this.unshiftContainer("body", nodes);
|
3830
|
+
} else {
|
3831
|
+
throw new Error("We don't know what to do with this node type. " + "We were previously a Statement but we can't fit in here?");
|
3832
|
+
}
|
3833
|
+
}
|
3834
|
+
function _containerInsert(from, nodes) {
|
3835
|
+
this.updateSiblingKeys(from, nodes.length);
|
3836
|
+
const paths = [];
|
3837
|
+
this.container.splice(from, 0, ...nodes);
|
3838
|
+
for (let i = 0; i < nodes.length; i++) {
|
3839
|
+
const to = from + i;
|
3840
|
+
const path = this.getSibling(to);
|
3841
|
+
paths.push(path);
|
3842
|
+
if (this.context?.queue) {
|
3843
|
+
path.pushContext(this.context);
|
3844
|
+
}
|
3845
|
+
}
|
3846
|
+
const contexts = this._getQueueContexts();
|
3847
|
+
for (const path of paths) {
|
3848
|
+
path.setScope();
|
3849
|
+
path.debug("Inserted.");
|
3850
|
+
for (const context of contexts) {
|
3851
|
+
context.maybeQueue(path, true);
|
3852
|
+
}
|
3853
|
+
}
|
3854
|
+
return paths;
|
3855
|
+
}
|
3856
|
+
function _containerInsertBefore(nodes) {
|
3857
|
+
return this._containerInsert(this.key, nodes);
|
3858
|
+
}
|
3859
|
+
function _containerInsertAfter(nodes) {
|
3860
|
+
return this._containerInsert(this.key + 1, nodes);
|
3861
|
+
}
|
3862
|
+
const last = arr => arr[arr.length - 1];
|
3863
|
+
function isHiddenInSequenceExpression(path) {
|
3864
|
+
return isSequenceExpression(path.parent) && (last(path.parent.expressions) !== path.node || isHiddenInSequenceExpression(path.parentPath));
|
3865
|
+
}
|
3866
|
+
function isAlmostConstantAssignment(node, scope) {
|
3867
|
+
if (!isAssignmentExpression(node) || !isIdentifier$1(node.left)) {
|
3868
|
+
return false;
|
3869
|
+
}
|
3870
|
+
const blockScope = scope.getBlockParent();
|
3871
|
+
return blockScope.hasOwnBinding(node.left.name) && blockScope.getOwnBinding(node.left.name).constantViolations.length <= 1;
|
3872
|
+
}
|
3873
|
+
function insertAfter(nodes_) {
|
3874
|
+
this._assertUnremoved();
|
3875
|
+
if (this.isSequenceExpression()) {
|
3876
|
+
return last(this.get("expressions")).insertAfter(nodes_);
|
3877
|
+
}
|
3878
|
+
const nodes = this._verifyNodeList(nodes_);
|
3879
|
+
const {
|
3880
|
+
parentPath,
|
3881
|
+
parent
|
3882
|
+
} = this;
|
3883
|
+
if (parentPath.isExpressionStatement() || parentPath.isLabeledStatement() || isExportNamedDeclaration(parent) || parentPath.isExportDefaultDeclaration() && this.isDeclaration()) {
|
3884
|
+
return parentPath.insertAfter(nodes.map(node => {
|
3885
|
+
return isExpression$1(node) ? expressionStatement(node) : node;
|
3886
|
+
}));
|
3887
|
+
} else if (this.isNodeType("Expression") && !this.isJSXElement() && !parentPath.isJSXElement() || parentPath.isForStatement() && this.key === "init") {
|
3888
|
+
if (this.node) {
|
3889
|
+
const node = this.node;
|
3890
|
+
let {
|
3891
|
+
scope
|
3892
|
+
} = this;
|
3893
|
+
if (scope.path.isPattern()) {
|
3894
|
+
assertExpression(node);
|
3895
|
+
this.replaceWith(callExpression(arrowFunctionExpression([], node), []));
|
3896
|
+
this.get("callee.body").insertAfter(nodes);
|
3897
|
+
return [this];
|
3898
|
+
}
|
3899
|
+
if (isHiddenInSequenceExpression(this)) {
|
3900
|
+
nodes.unshift(node);
|
3901
|
+
} else if (isCallExpression(node) && isSuper(node.callee)) {
|
3902
|
+
nodes.unshift(node);
|
3903
|
+
nodes.push(thisExpression());
|
3904
|
+
} else if (isAlmostConstantAssignment(node, scope)) {
|
3905
|
+
nodes.unshift(node);
|
3906
|
+
nodes.push(cloneNode(node.left));
|
3907
|
+
} else if (scope.isPure(node, true)) {
|
3908
|
+
nodes.push(node);
|
3909
|
+
} else {
|
3910
|
+
if (parentPath.isMethod({
|
3911
|
+
computed: true,
|
3912
|
+
key: node
|
3913
|
+
})) {
|
3914
|
+
scope = scope.parent;
|
3915
|
+
}
|
3916
|
+
const temp = scope.generateDeclaredUidIdentifier();
|
3917
|
+
nodes.unshift(expressionStatement(assignmentExpression("=", cloneNode(temp), node)));
|
3918
|
+
nodes.push(expressionStatement(cloneNode(temp)));
|
3919
|
+
}
|
3920
|
+
}
|
3921
|
+
return this.replaceExpressionWithStatements(nodes);
|
3922
|
+
} else if (Array.isArray(this.container)) {
|
3923
|
+
return this._containerInsertAfter(nodes);
|
3924
|
+
} else if (this.isStatementOrBlock()) {
|
3925
|
+
const node = this.node;
|
3926
|
+
const shouldInsertCurrentNode = node && (!this.isExpressionStatement() || node.expression != null);
|
3927
|
+
this.replaceWith(blockStatement(shouldInsertCurrentNode ? [node] : []));
|
3928
|
+
return this.pushContainer("body", nodes);
|
3929
|
+
} else {
|
3930
|
+
throw new Error("We don't know what to do with this node type. " + "We were previously a Statement but we can't fit in here?");
|
3931
|
+
}
|
3932
|
+
}
|
3933
|
+
function updateSiblingKeys(fromIndex, incrementBy) {
|
3934
|
+
if (!this.parent) return;
|
3935
|
+
const paths = path.get(this.parent);
|
3936
|
+
for (const [, path] of paths) {
|
3937
|
+
if (typeof path.key === "number" && path.key >= fromIndex) {
|
3938
|
+
path.key += incrementBy;
|
3939
|
+
}
|
3940
|
+
}
|
3941
|
+
}
|
3942
|
+
function _verifyNodeList(nodes) {
|
3943
|
+
if (!nodes) {
|
3944
|
+
return [];
|
3945
|
+
}
|
3946
|
+
if (!Array.isArray(nodes)) {
|
3947
|
+
nodes = [nodes];
|
3948
|
+
}
|
3949
|
+
for (let i = 0; i < nodes.length; i++) {
|
3950
|
+
const node = nodes[i];
|
3951
|
+
let msg;
|
3952
|
+
if (!node) {
|
3953
|
+
msg = "has falsy node";
|
3954
|
+
} else if (typeof node !== "object") {
|
3955
|
+
msg = "contains a non-object node";
|
3956
|
+
} else if (!node.type) {
|
3957
|
+
msg = "without a type";
|
3958
|
+
} else if (node instanceof NodePath) {
|
3959
|
+
msg = "has a NodePath when it expected a raw object";
|
3960
|
+
}
|
3961
|
+
if (msg) {
|
3962
|
+
const type = Array.isArray(node) ? "array" : typeof node;
|
3963
|
+
throw new Error(`Node list ${msg} with the index of ${i} and type of ${type}`);
|
3964
|
+
}
|
3965
|
+
}
|
3966
|
+
return nodes;
|
3967
|
+
}
|
3968
|
+
function unshiftContainer(listKey, nodes) {
|
3969
|
+
this._assertUnremoved();
|
3970
|
+
nodes = this._verifyNodeList(nodes);
|
3971
|
+
const path = NodePath.get({
|
3972
|
+
parentPath: this,
|
3973
|
+
parent: this.node,
|
3974
|
+
container: this.node[listKey],
|
3975
|
+
listKey,
|
3976
|
+
key: 0
|
3977
|
+
}).setContext(this.context);
|
3978
|
+
return path._containerInsertBefore(nodes);
|
3979
|
+
}
|
3980
|
+
function pushContainer(listKey, nodes) {
|
3981
|
+
this._assertUnremoved();
|
3982
|
+
const verifiedNodes = this._verifyNodeList(nodes);
|
3983
|
+
const container = this.node[listKey];
|
3984
|
+
const path = NodePath.get({
|
3985
|
+
parentPath: this,
|
3986
|
+
parent: this.node,
|
3987
|
+
container: container,
|
3988
|
+
listKey,
|
3989
|
+
key: container.length
|
3990
|
+
}).setContext(this.context);
|
3991
|
+
return path.replaceWithMultiple(verifiedNodes);
|
3992
|
+
}
|
3993
|
+
function hoist(scope = this.scope) {
|
3994
|
+
const hoister = new PathHoister(this, scope);
|
3995
|
+
return hoister.run();
|
3996
|
+
}
|
3997
|
+
|
3998
|
+
var NodePath_modification = /*#__PURE__*/Object.freeze({
|
3999
|
+
__proto__: null,
|
4000
|
+
insertBefore: insertBefore,
|
4001
|
+
_containerInsert: _containerInsert,
|
4002
|
+
_containerInsertBefore: _containerInsertBefore,
|
4003
|
+
_containerInsertAfter: _containerInsertAfter,
|
4004
|
+
insertAfter: insertAfter,
|
4005
|
+
updateSiblingKeys: updateSiblingKeys,
|
4006
|
+
_verifyNodeList: _verifyNodeList,
|
4007
|
+
unshiftContainer: unshiftContainer,
|
4008
|
+
pushContainer: pushContainer,
|
4009
|
+
hoist: hoist
|
5
4010
|
});
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4011
|
+
|
4012
|
+
const {
|
4013
|
+
getBindingIdentifiers: _getBindingIdentifiers,
|
4014
|
+
getOuterBindingIdentifiers: _getOuterBindingIdentifiers,
|
4015
|
+
isDeclaration,
|
4016
|
+
numericLiteral,
|
4017
|
+
unaryExpression
|
4018
|
+
} = _t;
|
4019
|
+
const NORMAL_COMPLETION = 0;
|
4020
|
+
const BREAK_COMPLETION = 1;
|
4021
|
+
function NormalCompletion(path) {
|
4022
|
+
return {
|
4023
|
+
type: NORMAL_COMPLETION,
|
4024
|
+
path
|
4025
|
+
};
|
4026
|
+
}
|
4027
|
+
function BreakCompletion(path) {
|
4028
|
+
return {
|
4029
|
+
type: BREAK_COMPLETION,
|
4030
|
+
path
|
4031
|
+
};
|
4032
|
+
}
|
4033
|
+
function getOpposite() {
|
4034
|
+
if (this.key === "left") {
|
4035
|
+
return this.getSibling("right");
|
4036
|
+
} else if (this.key === "right") {
|
4037
|
+
return this.getSibling("left");
|
4038
|
+
}
|
4039
|
+
return null;
|
4040
|
+
}
|
4041
|
+
function addCompletionRecords(path, records, context) {
|
4042
|
+
if (path) {
|
4043
|
+
records.push(..._getCompletionRecords(path, context));
|
4044
|
+
}
|
4045
|
+
return records;
|
4046
|
+
}
|
4047
|
+
function completionRecordForSwitch(cases, records, context) {
|
4048
|
+
let lastNormalCompletions = [];
|
4049
|
+
for (let i = 0; i < cases.length; i++) {
|
4050
|
+
const casePath = cases[i];
|
4051
|
+
const caseCompletions = _getCompletionRecords(casePath, context);
|
4052
|
+
const normalCompletions = [];
|
4053
|
+
const breakCompletions = [];
|
4054
|
+
for (const c of caseCompletions) {
|
4055
|
+
if (c.type === NORMAL_COMPLETION) {
|
4056
|
+
normalCompletions.push(c);
|
4057
|
+
}
|
4058
|
+
if (c.type === BREAK_COMPLETION) {
|
4059
|
+
breakCompletions.push(c);
|
4060
|
+
}
|
4061
|
+
}
|
4062
|
+
if (normalCompletions.length) {
|
4063
|
+
lastNormalCompletions = normalCompletions;
|
4064
|
+
}
|
4065
|
+
records.push(...breakCompletions);
|
4066
|
+
}
|
4067
|
+
records.push(...lastNormalCompletions);
|
4068
|
+
return records;
|
4069
|
+
}
|
4070
|
+
function normalCompletionToBreak(completions) {
|
4071
|
+
completions.forEach(c => {
|
4072
|
+
c.type = BREAK_COMPLETION;
|
4073
|
+
});
|
4074
|
+
}
|
4075
|
+
function replaceBreakStatementInBreakCompletion(completions, reachable) {
|
4076
|
+
completions.forEach(c => {
|
4077
|
+
if (c.path.isBreakStatement({
|
4078
|
+
label: null
|
4079
|
+
})) {
|
4080
|
+
if (reachable) {
|
4081
|
+
c.path.replaceWith(unaryExpression("void", numericLiteral(0)));
|
4082
|
+
} else {
|
4083
|
+
c.path.remove();
|
4084
|
+
}
|
4085
|
+
}
|
4086
|
+
});
|
4087
|
+
}
|
4088
|
+
function getStatementListCompletion(paths, context) {
|
4089
|
+
const completions = [];
|
4090
|
+
if (context.canHaveBreak) {
|
4091
|
+
let lastNormalCompletions = [];
|
4092
|
+
for (let i = 0; i < paths.length; i++) {
|
4093
|
+
const path = paths[i];
|
4094
|
+
const newContext = Object.assign({}, context, {
|
4095
|
+
inCaseClause: false
|
4096
|
+
});
|
4097
|
+
if (path.isBlockStatement() && (context.inCaseClause || context.shouldPopulateBreak)) {
|
4098
|
+
newContext.shouldPopulateBreak = true;
|
4099
|
+
} else {
|
4100
|
+
newContext.shouldPopulateBreak = false;
|
4101
|
+
}
|
4102
|
+
const statementCompletions = _getCompletionRecords(path, newContext);
|
4103
|
+
if (statementCompletions.length > 0 && statementCompletions.every(c => c.type === BREAK_COMPLETION)) {
|
4104
|
+
if (lastNormalCompletions.length > 0 && statementCompletions.every(c => c.path.isBreakStatement({
|
4105
|
+
label: null
|
4106
|
+
}))) {
|
4107
|
+
normalCompletionToBreak(lastNormalCompletions);
|
4108
|
+
completions.push(...lastNormalCompletions);
|
4109
|
+
if (lastNormalCompletions.some(c => c.path.isDeclaration())) {
|
4110
|
+
completions.push(...statementCompletions);
|
4111
|
+
replaceBreakStatementInBreakCompletion(statementCompletions, true);
|
4112
|
+
}
|
4113
|
+
replaceBreakStatementInBreakCompletion(statementCompletions, false);
|
4114
|
+
} else {
|
4115
|
+
completions.push(...statementCompletions);
|
4116
|
+
if (!context.shouldPopulateBreak) {
|
4117
|
+
replaceBreakStatementInBreakCompletion(statementCompletions, true);
|
4118
|
+
}
|
4119
|
+
}
|
4120
|
+
break;
|
4121
|
+
}
|
4122
|
+
if (i === paths.length - 1) {
|
4123
|
+
completions.push(...statementCompletions);
|
4124
|
+
} else {
|
4125
|
+
lastNormalCompletions = [];
|
4126
|
+
for (let i = 0; i < statementCompletions.length; i++) {
|
4127
|
+
const c = statementCompletions[i];
|
4128
|
+
if (c.type === BREAK_COMPLETION) {
|
4129
|
+
completions.push(c);
|
4130
|
+
}
|
4131
|
+
if (c.type === NORMAL_COMPLETION) {
|
4132
|
+
lastNormalCompletions.push(c);
|
4133
|
+
}
|
4134
|
+
}
|
4135
|
+
}
|
4136
|
+
}
|
4137
|
+
} else if (paths.length) {
|
4138
|
+
for (let i = paths.length - 1; i >= 0; i--) {
|
4139
|
+
const pathCompletions = _getCompletionRecords(paths[i], context);
|
4140
|
+
if (pathCompletions.length > 1 || pathCompletions.length === 1 && !pathCompletions[0].path.isVariableDeclaration()) {
|
4141
|
+
completions.push(...pathCompletions);
|
4142
|
+
break;
|
4143
|
+
}
|
4144
|
+
}
|
4145
|
+
}
|
4146
|
+
return completions;
|
4147
|
+
}
|
4148
|
+
function _getCompletionRecords(path, context) {
|
4149
|
+
let records = [];
|
4150
|
+
if (path.isIfStatement()) {
|
4151
|
+
records = addCompletionRecords(path.get("consequent"), records, context);
|
4152
|
+
records = addCompletionRecords(path.get("alternate"), records, context);
|
4153
|
+
} else if (path.isDoExpression() || path.isFor() || path.isWhile() || path.isLabeledStatement()) {
|
4154
|
+
return addCompletionRecords(path.get("body"), records, context);
|
4155
|
+
} else if (path.isProgram() || path.isBlockStatement()) {
|
4156
|
+
return getStatementListCompletion(path.get("body"), context);
|
4157
|
+
} else if (path.isFunction()) {
|
4158
|
+
return _getCompletionRecords(path.get("body"), context);
|
4159
|
+
} else if (path.isTryStatement()) {
|
4160
|
+
records = addCompletionRecords(path.get("block"), records, context);
|
4161
|
+
records = addCompletionRecords(path.get("handler"), records, context);
|
4162
|
+
} else if (path.isCatchClause()) {
|
4163
|
+
return addCompletionRecords(path.get("body"), records, context);
|
4164
|
+
} else if (path.isSwitchStatement()) {
|
4165
|
+
return completionRecordForSwitch(path.get("cases"), records, context);
|
4166
|
+
} else if (path.isSwitchCase()) {
|
4167
|
+
return getStatementListCompletion(path.get("consequent"), {
|
4168
|
+
canHaveBreak: true,
|
4169
|
+
shouldPopulateBreak: false,
|
4170
|
+
inCaseClause: true
|
4171
|
+
});
|
4172
|
+
} else if (path.isBreakStatement()) {
|
4173
|
+
records.push(BreakCompletion(path));
|
4174
|
+
} else {
|
4175
|
+
records.push(NormalCompletion(path));
|
4176
|
+
}
|
4177
|
+
return records;
|
4178
|
+
}
|
4179
|
+
function getCompletionRecords() {
|
4180
|
+
const records = _getCompletionRecords(this, {
|
4181
|
+
canHaveBreak: false,
|
4182
|
+
shouldPopulateBreak: false,
|
4183
|
+
inCaseClause: false
|
4184
|
+
});
|
4185
|
+
return records.map(r => r.path);
|
4186
|
+
}
|
4187
|
+
function getSibling(key) {
|
4188
|
+
return NodePath.get({
|
4189
|
+
parentPath: this.parentPath,
|
4190
|
+
parent: this.parent,
|
4191
|
+
container: this.container,
|
4192
|
+
listKey: this.listKey,
|
4193
|
+
key: key
|
4194
|
+
}).setContext(this.context);
|
4195
|
+
}
|
4196
|
+
function getPrevSibling() {
|
4197
|
+
return this.getSibling(this.key - 1);
|
4198
|
+
}
|
4199
|
+
function getNextSibling() {
|
4200
|
+
return this.getSibling(this.key + 1);
|
4201
|
+
}
|
4202
|
+
function getAllNextSiblings() {
|
4203
|
+
let _key = this.key;
|
4204
|
+
let sibling = this.getSibling(++_key);
|
4205
|
+
const siblings = [];
|
4206
|
+
while (sibling.node) {
|
4207
|
+
siblings.push(sibling);
|
4208
|
+
sibling = this.getSibling(++_key);
|
4209
|
+
}
|
4210
|
+
return siblings;
|
4211
|
+
}
|
4212
|
+
function getAllPrevSiblings() {
|
4213
|
+
let _key = this.key;
|
4214
|
+
let sibling = this.getSibling(--_key);
|
4215
|
+
const siblings = [];
|
4216
|
+
while (sibling.node) {
|
4217
|
+
siblings.push(sibling);
|
4218
|
+
sibling = this.getSibling(--_key);
|
4219
|
+
}
|
4220
|
+
return siblings;
|
4221
|
+
}
|
4222
|
+
function get(key, context = true) {
|
4223
|
+
if (context === true) context = this.context;
|
4224
|
+
const parts = key.split(".");
|
4225
|
+
if (parts.length === 1) {
|
4226
|
+
return this._getKey(key, context);
|
4227
|
+
} else {
|
4228
|
+
return this._getPattern(parts, context);
|
4229
|
+
}
|
4230
|
+
}
|
4231
|
+
function _getKey(key, context) {
|
4232
|
+
const node = this.node;
|
4233
|
+
const container = node[key];
|
4234
|
+
if (Array.isArray(container)) {
|
4235
|
+
return container.map((_, i) => {
|
4236
|
+
return NodePath.get({
|
4237
|
+
listKey: key,
|
4238
|
+
parentPath: this,
|
4239
|
+
parent: node,
|
4240
|
+
container: container,
|
4241
|
+
key: i
|
4242
|
+
}).setContext(context);
|
4243
|
+
});
|
4244
|
+
} else {
|
4245
|
+
return NodePath.get({
|
4246
|
+
parentPath: this,
|
4247
|
+
parent: node,
|
4248
|
+
container: node,
|
4249
|
+
key: key
|
4250
|
+
}).setContext(context);
|
4251
|
+
}
|
4252
|
+
}
|
4253
|
+
function _getPattern(parts, context) {
|
4254
|
+
let path = this;
|
4255
|
+
for (const part of parts) {
|
4256
|
+
if (part === ".") {
|
4257
|
+
path = path.parentPath;
|
4258
|
+
} else {
|
4259
|
+
if (Array.isArray(path)) {
|
4260
|
+
path = path[part];
|
4261
|
+
} else {
|
4262
|
+
path = path.get(part, context);
|
4263
|
+
}
|
4264
|
+
}
|
4265
|
+
}
|
4266
|
+
return path;
|
4267
|
+
}
|
4268
|
+
function getBindingIdentifiers(duplicates) {
|
4269
|
+
return _getBindingIdentifiers(this.node, duplicates);
|
4270
|
+
}
|
4271
|
+
function getOuterBindingIdentifiers(duplicates) {
|
4272
|
+
return _getOuterBindingIdentifiers(this.node, duplicates);
|
4273
|
+
}
|
4274
|
+
function getBindingIdentifierPaths(duplicates = false, outerOnly = false) {
|
4275
|
+
const path = this;
|
4276
|
+
const search = [path];
|
4277
|
+
const ids = Object.create(null);
|
4278
|
+
while (search.length) {
|
4279
|
+
const id = search.shift();
|
4280
|
+
if (!id) continue;
|
4281
|
+
if (!id.node) continue;
|
4282
|
+
const keys = _getBindingIdentifiers.keys[id.node.type];
|
4283
|
+
if (id.isIdentifier()) {
|
4284
|
+
if (duplicates) {
|
4285
|
+
const _ids = ids[id.node.name] = ids[id.node.name] || [];
|
4286
|
+
_ids.push(id);
|
4287
|
+
} else {
|
4288
|
+
ids[id.node.name] = id;
|
4289
|
+
}
|
4290
|
+
continue;
|
4291
|
+
}
|
4292
|
+
if (id.isExportDeclaration()) {
|
4293
|
+
const declaration = id.get("declaration");
|
4294
|
+
if (isDeclaration(declaration)) {
|
4295
|
+
search.push(declaration);
|
4296
|
+
}
|
4297
|
+
continue;
|
4298
|
+
}
|
4299
|
+
if (outerOnly) {
|
4300
|
+
if (id.isFunctionDeclaration()) {
|
4301
|
+
search.push(id.get("id"));
|
4302
|
+
continue;
|
4303
|
+
}
|
4304
|
+
if (id.isFunctionExpression()) {
|
4305
|
+
continue;
|
4306
|
+
}
|
4307
|
+
}
|
4308
|
+
if (keys) {
|
4309
|
+
for (let i = 0; i < keys.length; i++) {
|
4310
|
+
const key = keys[i];
|
4311
|
+
const child = id.get(key);
|
4312
|
+
if (Array.isArray(child)) {
|
4313
|
+
search.push(...child);
|
4314
|
+
} else if (child.node) {
|
4315
|
+
search.push(child);
|
4316
|
+
}
|
4317
|
+
}
|
4318
|
+
}
|
10
4319
|
}
|
4320
|
+
return ids;
|
4321
|
+
}
|
4322
|
+
function getOuterBindingIdentifierPaths(duplicates = false) {
|
4323
|
+
return this.getBindingIdentifierPaths(duplicates, true);
|
4324
|
+
}
|
4325
|
+
|
4326
|
+
var NodePath_family = /*#__PURE__*/Object.freeze({
|
4327
|
+
__proto__: null,
|
4328
|
+
getOpposite: getOpposite,
|
4329
|
+
getCompletionRecords: getCompletionRecords,
|
4330
|
+
getSibling: getSibling,
|
4331
|
+
getPrevSibling: getPrevSibling,
|
4332
|
+
getNextSibling: getNextSibling,
|
4333
|
+
getAllNextSiblings: getAllNextSiblings,
|
4334
|
+
getAllPrevSiblings: getAllPrevSiblings,
|
4335
|
+
get: get,
|
4336
|
+
_getKey: _getKey,
|
4337
|
+
_getPattern: _getPattern,
|
4338
|
+
getBindingIdentifiers: getBindingIdentifiers,
|
4339
|
+
getOuterBindingIdentifiers: getOuterBindingIdentifiers,
|
4340
|
+
getBindingIdentifierPaths: getBindingIdentifierPaths,
|
4341
|
+
getOuterBindingIdentifierPaths: getOuterBindingIdentifierPaths
|
11
4342
|
});
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
4343
|
+
|
4344
|
+
const {
|
4345
|
+
addComment: _addComment,
|
4346
|
+
addComments: _addComments
|
4347
|
+
} = _t;
|
4348
|
+
function shareCommentsWithSiblings() {
|
4349
|
+
if (typeof this.key === "string") return;
|
4350
|
+
const node = this.node;
|
4351
|
+
if (!node) return;
|
4352
|
+
const trailing = node.trailingComments;
|
4353
|
+
const leading = node.leadingComments;
|
4354
|
+
if (!trailing && !leading) return;
|
4355
|
+
const prev = this.getSibling(this.key - 1);
|
4356
|
+
const next = this.getSibling(this.key + 1);
|
4357
|
+
const hasPrev = Boolean(prev.node);
|
4358
|
+
const hasNext = Boolean(next.node);
|
4359
|
+
if (hasPrev) {
|
4360
|
+
if (leading) {
|
4361
|
+
prev.addComments("trailing", removeIfExisting(leading, prev.node.trailingComments));
|
4362
|
+
}
|
4363
|
+
if (trailing && !hasNext) prev.addComments("trailing", trailing);
|
4364
|
+
}
|
4365
|
+
if (hasNext) {
|
4366
|
+
if (trailing) {
|
4367
|
+
next.addComments("leading", removeIfExisting(trailing, next.node.leadingComments));
|
4368
|
+
}
|
4369
|
+
if (leading && !hasPrev) next.addComments("leading", leading);
|
16
4370
|
}
|
4371
|
+
}
|
4372
|
+
function removeIfExisting(list, toRemove) {
|
4373
|
+
if (!toRemove) return list;
|
4374
|
+
let lastFoundIndex = -1;
|
4375
|
+
return list.filter(el => {
|
4376
|
+
const i = toRemove.indexOf(el, lastFoundIndex);
|
4377
|
+
if (i === -1) return true;
|
4378
|
+
lastFoundIndex = i;
|
4379
|
+
});
|
4380
|
+
}
|
4381
|
+
function addComment(type, content, line) {
|
4382
|
+
_addComment(this.node, type, content, line);
|
4383
|
+
}
|
4384
|
+
function addComments(type, comments) {
|
4385
|
+
_addComments(this.node, type, comments);
|
4386
|
+
}
|
4387
|
+
|
4388
|
+
var NodePath_comments = /*#__PURE__*/Object.freeze({
|
4389
|
+
__proto__: null,
|
4390
|
+
shareCommentsWithSiblings: shareCommentsWithSiblings,
|
4391
|
+
addComment: addComment,
|
4392
|
+
addComments: addComments
|
17
4393
|
});
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
4394
|
+
|
4395
|
+
const {
|
4396
|
+
isBinding,
|
4397
|
+
isBlockScoped: nodeIsBlockScoped,
|
4398
|
+
isExportDeclaration,
|
4399
|
+
isExpression: nodeIsExpression,
|
4400
|
+
isFlow: nodeIsFlow,
|
4401
|
+
isForStatement,
|
4402
|
+
isForXStatement,
|
4403
|
+
isIdentifier,
|
4404
|
+
isImportDeclaration,
|
4405
|
+
isImportSpecifier,
|
4406
|
+
isJSXIdentifier,
|
4407
|
+
isJSXMemberExpression,
|
4408
|
+
isMemberExpression,
|
4409
|
+
isRestElement: nodeIsRestElement,
|
4410
|
+
isReferenced: nodeIsReferenced,
|
4411
|
+
isScope: nodeIsScope,
|
4412
|
+
isStatement: nodeIsStatement,
|
4413
|
+
isVar: nodeIsVar,
|
4414
|
+
isVariableDeclaration,
|
4415
|
+
react,
|
4416
|
+
isForOfStatement
|
4417
|
+
} = _t;
|
4418
|
+
const {
|
4419
|
+
isCompatTag
|
4420
|
+
} = react;
|
4421
|
+
function isReferencedIdentifier(opts) {
|
4422
|
+
const {
|
4423
|
+
node,
|
4424
|
+
parent
|
4425
|
+
} = this;
|
4426
|
+
if (!isIdentifier(node, opts) && !isJSXMemberExpression(parent, opts)) {
|
4427
|
+
if (isJSXIdentifier(node, opts)) {
|
4428
|
+
if (isCompatTag(node.name)) return false;
|
4429
|
+
} else {
|
4430
|
+
return false;
|
4431
|
+
}
|
4432
|
+
}
|
4433
|
+
return nodeIsReferenced(node, parent, this.parentPath.parent);
|
4434
|
+
}
|
4435
|
+
function isReferencedMemberExpression() {
|
4436
|
+
const {
|
4437
|
+
node,
|
4438
|
+
parent
|
4439
|
+
} = this;
|
4440
|
+
return isMemberExpression(node) && nodeIsReferenced(node, parent);
|
4441
|
+
}
|
4442
|
+
function isBindingIdentifier() {
|
4443
|
+
const {
|
4444
|
+
node,
|
4445
|
+
parent
|
4446
|
+
} = this;
|
4447
|
+
const grandparent = this.parentPath.parent;
|
4448
|
+
return isIdentifier(node) && isBinding(node, parent, grandparent);
|
4449
|
+
}
|
4450
|
+
function isStatement() {
|
4451
|
+
const {
|
4452
|
+
node,
|
4453
|
+
parent
|
4454
|
+
} = this;
|
4455
|
+
if (nodeIsStatement(node)) {
|
4456
|
+
if (isVariableDeclaration(node)) {
|
4457
|
+
if (isForXStatement(parent, {
|
4458
|
+
left: node
|
4459
|
+
})) return false;
|
4460
|
+
if (isForStatement(parent, {
|
4461
|
+
init: node
|
4462
|
+
})) return false;
|
4463
|
+
}
|
4464
|
+
return true;
|
4465
|
+
} else {
|
4466
|
+
return false;
|
4467
|
+
}
|
4468
|
+
}
|
4469
|
+
function isExpression() {
|
4470
|
+
if (this.isIdentifier()) {
|
4471
|
+
return this.isReferencedIdentifier();
|
4472
|
+
} else {
|
4473
|
+
return nodeIsExpression(this.node);
|
4474
|
+
}
|
4475
|
+
}
|
4476
|
+
function isScope() {
|
4477
|
+
return nodeIsScope(this.node, this.parent);
|
4478
|
+
}
|
4479
|
+
function isReferenced() {
|
4480
|
+
return nodeIsReferenced(this.node, this.parent);
|
4481
|
+
}
|
4482
|
+
function isBlockScoped() {
|
4483
|
+
return nodeIsBlockScoped(this.node);
|
4484
|
+
}
|
4485
|
+
function isVar() {
|
4486
|
+
return nodeIsVar(this.node);
|
4487
|
+
}
|
4488
|
+
function isUser() {
|
4489
|
+
return this.node && !!this.node.loc;
|
4490
|
+
}
|
4491
|
+
function isGenerated() {
|
4492
|
+
return !this.isUser();
|
4493
|
+
}
|
4494
|
+
function isPure(constantsOnly) {
|
4495
|
+
return this.scope.isPure(this.node, constantsOnly);
|
4496
|
+
}
|
4497
|
+
function isFlow() {
|
4498
|
+
const {
|
4499
|
+
node
|
4500
|
+
} = this;
|
4501
|
+
if (nodeIsFlow(node)) {
|
4502
|
+
return true;
|
4503
|
+
} else if (isImportDeclaration(node)) {
|
4504
|
+
return node.importKind === "type" || node.importKind === "typeof";
|
4505
|
+
} else if (isExportDeclaration(node)) {
|
4506
|
+
return node.exportKind === "type";
|
4507
|
+
} else if (isImportSpecifier(node)) {
|
4508
|
+
return node.importKind === "type" || node.importKind === "typeof";
|
4509
|
+
} else {
|
4510
|
+
return false;
|
22
4511
|
}
|
4512
|
+
}
|
4513
|
+
function isRestProperty() {
|
4514
|
+
return nodeIsRestElement(this.node) && this.parentPath && this.parentPath.isObjectPattern();
|
4515
|
+
}
|
4516
|
+
function isSpreadProperty() {
|
4517
|
+
return nodeIsRestElement(this.node) && this.parentPath && this.parentPath.isObjectExpression();
|
4518
|
+
}
|
4519
|
+
function isForAwaitStatement() {
|
4520
|
+
return isForOfStatement(this.node, {
|
4521
|
+
await: true
|
4522
|
+
});
|
4523
|
+
}
|
4524
|
+
function isExistentialTypeParam() {
|
4525
|
+
throw new Error("`path.isExistentialTypeParam` has been renamed to `path.isExistsTypeAnnotation()` in Babel 7.");
|
4526
|
+
}
|
4527
|
+
function isNumericLiteralTypeAnnotation() {
|
4528
|
+
throw new Error("`path.isNumericLiteralTypeAnnotation()` has been renamed to `path.isNumberLiteralTypeAnnotation()` in Babel 7.");
|
4529
|
+
}
|
4530
|
+
|
4531
|
+
var NodePath_virtual_types_validator = /*#__PURE__*/Object.freeze({
|
4532
|
+
__proto__: null,
|
4533
|
+
isReferencedIdentifier: isReferencedIdentifier,
|
4534
|
+
isReferencedMemberExpression: isReferencedMemberExpression,
|
4535
|
+
isBindingIdentifier: isBindingIdentifier,
|
4536
|
+
isStatement: isStatement,
|
4537
|
+
isExpression: isExpression,
|
4538
|
+
isScope: isScope,
|
4539
|
+
isReferenced: isReferenced,
|
4540
|
+
isBlockScoped: isBlockScoped,
|
4541
|
+
isVar: isVar,
|
4542
|
+
isUser: isUser,
|
4543
|
+
isGenerated: isGenerated,
|
4544
|
+
isPure: isPure,
|
4545
|
+
isFlow: isFlow,
|
4546
|
+
isRestProperty: isRestProperty,
|
4547
|
+
isSpreadProperty: isSpreadProperty,
|
4548
|
+
isForAwaitStatement: isForAwaitStatement,
|
4549
|
+
isExistentialTypeParam: isExistentialTypeParam,
|
4550
|
+
isNumericLiteralTypeAnnotation: isNumericLiteralTypeAnnotation
|
23
4551
|
});
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
4552
|
+
|
4553
|
+
const {
|
4554
|
+
validate
|
4555
|
+
} = _t;
|
4556
|
+
const debug = buildDebug("babel");
|
4557
|
+
const REMOVED = 1 << 0;
|
4558
|
+
const SHOULD_STOP = 1 << 1;
|
4559
|
+
const SHOULD_SKIP = 1 << 2;
|
4560
|
+
class NodePath {
|
4561
|
+
constructor(hub, parent) {
|
4562
|
+
this.parent = parent;
|
4563
|
+
this.hub = hub;
|
4564
|
+
this.data = null;
|
4565
|
+
this.context = null;
|
4566
|
+
this.scope = null;
|
4567
|
+
}
|
4568
|
+
contexts = [];
|
4569
|
+
state = null;
|
4570
|
+
opts = null;
|
4571
|
+
_traverseFlags = 0;
|
4572
|
+
skipKeys = null;
|
4573
|
+
parentPath = null;
|
4574
|
+
container = null;
|
4575
|
+
listKey = null;
|
4576
|
+
key = null;
|
4577
|
+
node = null;
|
4578
|
+
type = null;
|
4579
|
+
static get({
|
4580
|
+
hub,
|
4581
|
+
parentPath,
|
4582
|
+
parent,
|
4583
|
+
container,
|
4584
|
+
listKey,
|
4585
|
+
key
|
4586
|
+
}) {
|
4587
|
+
if (!hub && parentPath) {
|
4588
|
+
hub = parentPath.hub;
|
4589
|
+
}
|
4590
|
+
if (!parent) {
|
4591
|
+
throw new Error("To get a node path the parent needs to exist");
|
4592
|
+
}
|
4593
|
+
const targetNode = container[key];
|
4594
|
+
let paths = path.get(parent);
|
4595
|
+
if (!paths) {
|
4596
|
+
paths = new Map();
|
4597
|
+
path.set(parent, paths);
|
4598
|
+
}
|
4599
|
+
let path$1 = paths.get(targetNode);
|
4600
|
+
if (!path$1) {
|
4601
|
+
path$1 = new NodePath(hub, parent);
|
4602
|
+
if (targetNode) paths.set(targetNode, path$1);
|
4603
|
+
}
|
4604
|
+
path$1.setup(parentPath, container, listKey, key);
|
4605
|
+
return path$1;
|
4606
|
+
}
|
4607
|
+
getScope(scope) {
|
4608
|
+
return this.isScope() ? new Scope(this) : scope;
|
4609
|
+
}
|
4610
|
+
setData(key, val) {
|
4611
|
+
if (this.data == null) {
|
4612
|
+
this.data = Object.create(null);
|
4613
|
+
}
|
4614
|
+
return this.data[key] = val;
|
4615
|
+
}
|
4616
|
+
getData(key, def) {
|
4617
|
+
if (this.data == null) {
|
4618
|
+
this.data = Object.create(null);
|
4619
|
+
}
|
4620
|
+
let val = this.data[key];
|
4621
|
+
if (val === undefined && def !== undefined) val = this.data[key] = def;
|
4622
|
+
return val;
|
4623
|
+
}
|
4624
|
+
hasNode() {
|
4625
|
+
return this.node != null;
|
4626
|
+
}
|
4627
|
+
buildCodeFrameError(msg, Error = SyntaxError) {
|
4628
|
+
return this.hub.buildError(this.node, msg, Error);
|
4629
|
+
}
|
4630
|
+
traverse(visitor, state) {
|
4631
|
+
traverse(this.node, visitor, this.scope, state, this);
|
4632
|
+
}
|
4633
|
+
set(key, node) {
|
4634
|
+
validate(this.node, key, node);
|
4635
|
+
this.node[key] = node;
|
4636
|
+
}
|
4637
|
+
getPathLocation() {
|
4638
|
+
const parts = [];
|
4639
|
+
let path = this;
|
4640
|
+
do {
|
4641
|
+
let key = path.key;
|
4642
|
+
if (path.inList) key = `${path.listKey}[${key}]`;
|
4643
|
+
parts.unshift(key);
|
4644
|
+
} while (path = path.parentPath);
|
4645
|
+
return parts.join(".");
|
4646
|
+
}
|
4647
|
+
debug(message) {
|
4648
|
+
if (!debug.enabled) return;
|
4649
|
+
debug(`${this.getPathLocation()} ${this.type}: ${message}`);
|
4650
|
+
}
|
4651
|
+
toString() {
|
4652
|
+
return generator(this.node).code;
|
4653
|
+
}
|
4654
|
+
get inList() {
|
4655
|
+
return !!this.listKey;
|
4656
|
+
}
|
4657
|
+
set inList(inList) {
|
4658
|
+
if (!inList) {
|
4659
|
+
this.listKey = null;
|
4660
|
+
}
|
4661
|
+
}
|
4662
|
+
get parentKey() {
|
4663
|
+
return this.listKey || this.key;
|
4664
|
+
}
|
4665
|
+
get shouldSkip() {
|
4666
|
+
return !!(this._traverseFlags & SHOULD_SKIP);
|
4667
|
+
}
|
4668
|
+
set shouldSkip(v) {
|
4669
|
+
if (v) {
|
4670
|
+
this._traverseFlags |= SHOULD_SKIP;
|
4671
|
+
} else {
|
4672
|
+
this._traverseFlags &= ~SHOULD_SKIP;
|
4673
|
+
}
|
4674
|
+
}
|
4675
|
+
get shouldStop() {
|
4676
|
+
return !!(this._traverseFlags & SHOULD_STOP);
|
4677
|
+
}
|
4678
|
+
set shouldStop(v) {
|
4679
|
+
if (v) {
|
4680
|
+
this._traverseFlags |= SHOULD_STOP;
|
4681
|
+
} else {
|
4682
|
+
this._traverseFlags &= ~SHOULD_STOP;
|
4683
|
+
}
|
4684
|
+
}
|
4685
|
+
get removed() {
|
4686
|
+
return !!(this._traverseFlags & REMOVED);
|
4687
|
+
}
|
4688
|
+
set removed(v) {
|
4689
|
+
if (v) {
|
4690
|
+
this._traverseFlags |= REMOVED;
|
4691
|
+
} else {
|
4692
|
+
this._traverseFlags &= ~REMOVED;
|
4693
|
+
}
|
4694
|
+
}
|
4695
|
+
}
|
4696
|
+
Object.assign(NodePath.prototype, NodePath_ancestry, NodePath_inference, NodePath_replacement, NodePath_evaluation, NodePath_conversion, NodePath_introspection, NodePath_context, NodePath_removal, NodePath_modification, NodePath_family, NodePath_comments);
|
4697
|
+
for (const type of _t.TYPES) {
|
4698
|
+
const typeKey = `is${type}`;
|
4699
|
+
const fn = _t[typeKey];
|
4700
|
+
NodePath.prototype[typeKey] = function (opts) {
|
4701
|
+
return fn(this.node, opts);
|
4702
|
+
};
|
4703
|
+
NodePath.prototype[`assert${type}`] = function (opts) {
|
4704
|
+
if (!fn(this.node, opts)) {
|
4705
|
+
throw new TypeError(`Expected node path of type ${type}`);
|
4706
|
+
}
|
4707
|
+
};
|
4708
|
+
}
|
4709
|
+
Object.assign(NodePath.prototype, NodePath_virtual_types_validator);
|
4710
|
+
for (const type of Object.keys(virtualTypes)) {
|
4711
|
+
if (type[0] === "_") continue;
|
4712
|
+
if (!_t.TYPES.includes(type)) _t.TYPES.push(type);
|
4713
|
+
}
|
4714
|
+
|
4715
|
+
const {
|
4716
|
+
VISITOR_KEYS: VISITOR_KEYS$2
|
4717
|
+
} = _t;
|
4718
|
+
class TraversalContext {
|
4719
|
+
constructor(scope, opts, state, parentPath) {
|
4720
|
+
this.parentPath = parentPath;
|
4721
|
+
this.scope = scope;
|
4722
|
+
this.state = state;
|
4723
|
+
this.opts = opts;
|
4724
|
+
}
|
4725
|
+
queue = null;
|
4726
|
+
priorityQueue = null;
|
4727
|
+
shouldVisit(node) {
|
4728
|
+
const opts = this.opts;
|
4729
|
+
if (opts.enter || opts.exit) return true;
|
4730
|
+
if (opts[node.type]) return true;
|
4731
|
+
const keys = VISITOR_KEYS$2[node.type];
|
4732
|
+
if (!keys?.length) return false;
|
4733
|
+
for (const key of keys) {
|
4734
|
+
if (node[key]) {
|
4735
|
+
return true;
|
4736
|
+
}
|
4737
|
+
}
|
4738
|
+
return false;
|
4739
|
+
}
|
4740
|
+
create(node, container, key, listKey) {
|
4741
|
+
return NodePath.get({
|
4742
|
+
parentPath: this.parentPath,
|
4743
|
+
parent: node,
|
4744
|
+
container,
|
4745
|
+
key: key,
|
4746
|
+
listKey
|
4747
|
+
});
|
4748
|
+
}
|
4749
|
+
maybeQueue(path, notPriority) {
|
4750
|
+
if (this.queue) {
|
4751
|
+
if (notPriority) {
|
4752
|
+
this.queue.push(path);
|
4753
|
+
} else {
|
4754
|
+
this.priorityQueue.push(path);
|
4755
|
+
}
|
4756
|
+
}
|
4757
|
+
}
|
4758
|
+
visitMultiple(container, parent, listKey) {
|
4759
|
+
if (container.length === 0) return false;
|
4760
|
+
const queue = [];
|
4761
|
+
for (let key = 0; key < container.length; key++) {
|
4762
|
+
const node = container[key];
|
4763
|
+
if (node && this.shouldVisit(node)) {
|
4764
|
+
queue.push(this.create(parent, container, key, listKey));
|
4765
|
+
}
|
4766
|
+
}
|
4767
|
+
return this.visitQueue(queue);
|
4768
|
+
}
|
4769
|
+
visitSingle(node, key) {
|
4770
|
+
if (this.shouldVisit(node[key])) {
|
4771
|
+
return this.visitQueue([this.create(node, node, key)]);
|
4772
|
+
} else {
|
4773
|
+
return false;
|
4774
|
+
}
|
4775
|
+
}
|
4776
|
+
visitQueue(queue) {
|
4777
|
+
this.queue = queue;
|
4778
|
+
this.priorityQueue = [];
|
4779
|
+
const visited = new WeakSet();
|
4780
|
+
let stop = false;
|
4781
|
+
for (const path of queue) {
|
4782
|
+
path.resync();
|
4783
|
+
if (path.contexts.length === 0 || path.contexts[path.contexts.length - 1] !== this) {
|
4784
|
+
path.pushContext(this);
|
4785
|
+
}
|
4786
|
+
if (path.key === null) continue;
|
4787
|
+
const {
|
4788
|
+
node
|
4789
|
+
} = path;
|
4790
|
+
if (visited.has(node)) continue;
|
4791
|
+
if (node) visited.add(node);
|
4792
|
+
if (path.visit()) {
|
4793
|
+
stop = true;
|
4794
|
+
break;
|
4795
|
+
}
|
4796
|
+
if (this.priorityQueue.length) {
|
4797
|
+
stop = this.visitQueue(this.priorityQueue);
|
4798
|
+
this.priorityQueue = [];
|
4799
|
+
this.queue = queue;
|
4800
|
+
if (stop) break;
|
4801
|
+
}
|
4802
|
+
}
|
4803
|
+
for (const path of queue) {
|
4804
|
+
path.popContext();
|
4805
|
+
}
|
4806
|
+
this.queue = null;
|
4807
|
+
return stop;
|
4808
|
+
}
|
4809
|
+
visit(node, key) {
|
4810
|
+
const nodes = node[key];
|
4811
|
+
if (!nodes) return false;
|
4812
|
+
if (Array.isArray(nodes)) {
|
4813
|
+
return this.visitMultiple(nodes, node, key);
|
4814
|
+
} else {
|
4815
|
+
return this.visitSingle(node, key);
|
4816
|
+
}
|
4817
|
+
}
|
4818
|
+
}
|
4819
|
+
|
4820
|
+
const {
|
4821
|
+
VISITOR_KEYS: VISITOR_KEYS$1
|
4822
|
+
} = _t;
|
4823
|
+
function traverseNode(node, opts, scope, state, path, skipKeys) {
|
4824
|
+
const keys = VISITOR_KEYS$1[node.type];
|
4825
|
+
if (!keys) return false;
|
4826
|
+
const context = new TraversalContext(scope, opts, state, path);
|
4827
|
+
for (const key of keys) {
|
4828
|
+
if (skipKeys && skipKeys[key]) continue;
|
4829
|
+
if (context.visit(node, key)) {
|
4830
|
+
return true;
|
4831
|
+
}
|
4832
|
+
}
|
4833
|
+
return false;
|
4834
|
+
}
|
4835
|
+
|
4836
|
+
class Hub {
|
4837
|
+
getCode() {}
|
4838
|
+
getScope() {}
|
4839
|
+
addHelper() {
|
4840
|
+
throw new Error("Helpers are not supported by the default hub.");
|
4841
|
+
}
|
4842
|
+
buildError(node, msg, Error = TypeError) {
|
4843
|
+
return new Error(msg);
|
4844
|
+
}
|
4845
|
+
}
|
4846
|
+
|
33
4847
|
const {
|
34
4848
|
VISITOR_KEYS,
|
35
4849
|
removeProperties,
|
@@ -45,24 +4859,22 @@ function traverse(parent, opts = {}, scope, state, parentPath) {
|
|
45
4859
|
if (!VISITOR_KEYS[parent.type]) {
|
46
4860
|
return;
|
47
4861
|
}
|
48
|
-
|
49
|
-
|
4862
|
+
explode(opts);
|
4863
|
+
traverseNode(parent, opts, scope, state, parentPath);
|
50
4864
|
}
|
51
|
-
var _default = traverse;
|
52
|
-
exports.default = _default;
|
53
4865
|
traverse.visitors = visitors;
|
54
|
-
traverse.verify =
|
55
|
-
traverse.explode =
|
4866
|
+
traverse.verify = verify;
|
4867
|
+
traverse.explode = explode;
|
56
4868
|
traverse.cheap = function (node, enter) {
|
57
4869
|
traverseFast(node, enter);
|
58
4870
|
return;
|
59
4871
|
};
|
60
4872
|
traverse.node = function (node, opts, scope, state, path, skipKeys) {
|
61
|
-
|
4873
|
+
traverseNode(node, opts, scope, state, path, skipKeys);
|
62
4874
|
};
|
63
4875
|
traverse.clearNode = function (node, opts) {
|
64
4876
|
removeProperties(node, opts);
|
65
|
-
|
4877
|
+
path.delete(node);
|
66
4878
|
};
|
67
4879
|
traverse.removeProperties = function (tree, opts) {
|
68
4880
|
traverseFast(tree, traverse.clearNode, opts);
|
@@ -75,7 +4887,7 @@ function hasDenylistedType(path, state) {
|
|
75
4887
|
}
|
76
4888
|
}
|
77
4889
|
traverse.hasType = function (tree, type, denylistTypes) {
|
78
|
-
if (denylistTypes
|
4890
|
+
if (denylistTypes?.includes(tree.type)) return false;
|
79
4891
|
if (tree.type === type) return true;
|
80
4892
|
const state = {
|
81
4893
|
has: false,
|
@@ -90,4 +4902,5 @@ traverse.hasType = function (tree, type, denylistTypes) {
|
|
90
4902
|
};
|
91
4903
|
traverse.cache = cache;
|
92
4904
|
|
4905
|
+
export { Hub, NodePath, Scope, traverse as default, visitors };
|
93
4906
|
//# sourceMappingURL=index.js.map
|