@babel/traverse 7.17.9 → 7.23.2

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.
Files changed (67) hide show
  1. package/README.md +1 -1
  2. package/lib/cache.js +27 -7
  3. package/lib/cache.js.map +1 -0
  4. package/lib/context.js +9 -31
  5. package/lib/context.js.map +1 -0
  6. package/lib/hub.js +2 -6
  7. package/lib/hub.js.map +1 -0
  8. package/lib/index.js +17 -33
  9. package/lib/index.js.map +1 -0
  10. package/lib/path/ancestry.js +3 -42
  11. package/lib/path/ancestry.js.map +1 -0
  12. package/lib/path/comments.js +23 -11
  13. package/lib/path/comments.js.map +1 -0
  14. package/lib/path/context.js +24 -65
  15. package/lib/path/context.js.map +1 -0
  16. package/lib/path/conversion.js +37 -100
  17. package/lib/path/conversion.js.map +1 -0
  18. package/lib/path/evaluation.js +69 -123
  19. package/lib/path/evaluation.js.map +1 -0
  20. package/lib/path/family.js +5 -76
  21. package/lib/path/family.js.map +1 -0
  22. package/lib/path/index.js +26 -90
  23. package/lib/path/index.js.map +1 -0
  24. package/lib/path/inference/index.js +26 -33
  25. package/lib/path/inference/index.js.map +1 -0
  26. package/lib/path/inference/inferer-reference.js +6 -61
  27. package/lib/path/inference/inferer-reference.js.map +1 -0
  28. package/lib/path/inference/inferers.js +32 -86
  29. package/lib/path/inference/inferers.js.map +1 -0
  30. package/lib/path/inference/util.js +30 -0
  31. package/lib/path/inference/util.js.map +1 -0
  32. package/lib/path/introspection.js +54 -105
  33. package/lib/path/introspection.js.map +1 -0
  34. package/lib/path/lib/hoister.js +2 -37
  35. package/lib/path/lib/hoister.js.map +1 -0
  36. package/lib/path/lib/removal-hooks.js +4 -4
  37. package/lib/path/lib/removal-hooks.js.map +1 -0
  38. package/lib/path/lib/virtual-types-validator.js +161 -0
  39. package/lib/path/lib/virtual-types-validator.js.map +1 -0
  40. package/lib/path/lib/virtual-types.js +20 -206
  41. package/lib/path/lib/virtual-types.js.map +1 -0
  42. package/lib/path/modification.js +17 -61
  43. package/lib/path/modification.js.map +1 -0
  44. package/lib/path/removal.js +9 -22
  45. package/lib/path/removal.js.map +1 -0
  46. package/lib/path/replacement.js +17 -72
  47. package/lib/path/replacement.js.map +1 -0
  48. package/lib/scope/binding.js +20 -12
  49. package/lib/scope/binding.js.map +1 -0
  50. package/lib/scope/index.js +115 -245
  51. package/lib/scope/index.js.map +1 -0
  52. package/lib/scope/lib/renamer.js +41 -74
  53. package/lib/scope/lib/renamer.js.map +1 -0
  54. package/lib/traverse-node.js +10 -11
  55. package/lib/traverse-node.js.map +1 -0
  56. package/lib/types.js +1 -3
  57. package/lib/types.js.map +1 -0
  58. package/lib/visitors.js +59 -83
  59. package/lib/visitors.js.map +1 -0
  60. package/package.json +13 -11
  61. package/lib/path/generated/asserts.js +0 -5
  62. package/lib/path/generated/validators.js +0 -5
  63. package/lib/path/generated/virtual-types.js +0 -3
  64. package/scripts/generators/asserts.js +0 -25
  65. package/scripts/generators/validators.js +0 -42
  66. package/scripts/generators/virtual-types.js +0 -24
  67. package/scripts/package.json +0 -1
@@ -4,19 +4,14 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
- var _renamer = require("./lib/renamer");
9
-
10
- var _index = require("../index");
11
-
12
- var _binding = require("./binding");
13
-
7
+ var _renamer = require("./lib/renamer.js");
8
+ var _index = require("../index.js");
9
+ var _binding = require("./binding.js");
14
10
  var _globals = require("globals");
15
-
16
11
  var _t = require("@babel/types");
17
-
18
- var _cache = require("../cache");
19
-
12
+ var t = _t;
13
+ var _cache = require("../cache.js");
14
+ var _visitors = require("../visitors.js");
20
15
  const {
21
16
  NOT_LOCAL_BINDING,
22
17
  callExpression,
@@ -36,11 +31,12 @@ const {
36
31
  isImportDeclaration,
37
32
  isLiteral,
38
33
  isMethod,
39
- isModuleDeclaration,
40
34
  isModuleSpecifier,
35
+ isNullLiteral,
41
36
  isObjectExpression,
42
37
  isProperty,
43
38
  isPureish,
39
+ isRegExpLiteral,
44
40
  isSuper,
45
41
  isTaggedTemplateExpression,
46
42
  isTemplateLiteral,
@@ -51,61 +47,60 @@ const {
51
47
  memberExpression,
52
48
  numericLiteral,
53
49
  toIdentifier,
54
- unaryExpression,
55
50
  variableDeclaration,
56
- variableDeclarator
51
+ variableDeclarator,
52
+ isRecordExpression,
53
+ isTupleExpression,
54
+ isObjectProperty,
55
+ isTopicReference,
56
+ isMetaProperty,
57
+ isPrivateName,
58
+ isExportDeclaration,
59
+ buildUndefinedNode
57
60
  } = _t;
58
-
59
61
  function gatherNodeParts(node, parts) {
60
62
  switch (node == null ? void 0 : node.type) {
61
63
  default:
62
- if (isModuleDeclaration(node)) {
64
+ if (isImportDeclaration(node) || isExportDeclaration(node)) {
65
+ var _node$specifiers;
63
66
  if ((isExportAllDeclaration(node) || isExportNamedDeclaration(node) || isImportDeclaration(node)) && node.source) {
64
67
  gatherNodeParts(node.source, parts);
65
- } else if ((isExportNamedDeclaration(node) || isImportDeclaration(node)) && node.specifiers && node.specifiers.length) {
68
+ } else if ((isExportNamedDeclaration(node) || isImportDeclaration(node)) && (_node$specifiers = node.specifiers) != null && _node$specifiers.length) {
66
69
  for (const e of node.specifiers) gatherNodeParts(e, parts);
67
70
  } else if ((isExportDefaultDeclaration(node) || isExportNamedDeclaration(node)) && node.declaration) {
68
71
  gatherNodeParts(node.declaration, parts);
69
72
  }
70
73
  } else if (isModuleSpecifier(node)) {
71
74
  gatherNodeParts(node.local, parts);
72
- } else if (isLiteral(node)) {
75
+ } else if (isLiteral(node) && !isNullLiteral(node) && !isRegExpLiteral(node) && !isTemplateLiteral(node)) {
73
76
  parts.push(node.value);
74
77
  }
75
-
76
78
  break;
77
-
78
79
  case "MemberExpression":
79
80
  case "OptionalMemberExpression":
80
81
  case "JSXMemberExpression":
81
82
  gatherNodeParts(node.object, parts);
82
83
  gatherNodeParts(node.property, parts);
83
84
  break;
84
-
85
85
  case "Identifier":
86
86
  case "JSXIdentifier":
87
87
  parts.push(node.name);
88
88
  break;
89
-
90
89
  case "CallExpression":
91
90
  case "OptionalCallExpression":
92
91
  case "NewExpression":
93
92
  gatherNodeParts(node.callee, parts);
94
93
  break;
95
-
96
94
  case "ObjectExpression":
97
95
  case "ObjectPattern":
98
96
  for (const e of node.properties) {
99
97
  gatherNodeParts(e, parts);
100
98
  }
101
-
102
99
  break;
103
-
104
100
  case "SpreadElement":
105
101
  case "RestElement":
106
102
  gatherNodeParts(node.argument, parts);
107
103
  break;
108
-
109
104
  case "ObjectProperty":
110
105
  case "ObjectMethod":
111
106
  case "ClassProperty":
@@ -114,93 +109,73 @@ function gatherNodeParts(node, parts) {
114
109
  case "ClassPrivateMethod":
115
110
  gatherNodeParts(node.key, parts);
116
111
  break;
117
-
118
112
  case "ThisExpression":
119
113
  parts.push("this");
120
114
  break;
121
-
122
115
  case "Super":
123
116
  parts.push("super");
124
117
  break;
125
-
126
118
  case "Import":
127
119
  parts.push("import");
128
120
  break;
129
-
130
121
  case "DoExpression":
131
122
  parts.push("do");
132
123
  break;
133
-
134
124
  case "YieldExpression":
135
125
  parts.push("yield");
136
126
  gatherNodeParts(node.argument, parts);
137
127
  break;
138
-
139
128
  case "AwaitExpression":
140
129
  parts.push("await");
141
130
  gatherNodeParts(node.argument, parts);
142
131
  break;
143
-
144
132
  case "AssignmentExpression":
145
133
  gatherNodeParts(node.left, parts);
146
134
  break;
147
-
148
135
  case "VariableDeclarator":
149
136
  gatherNodeParts(node.id, parts);
150
137
  break;
151
-
152
138
  case "FunctionExpression":
153
139
  case "FunctionDeclaration":
154
140
  case "ClassExpression":
155
141
  case "ClassDeclaration":
156
142
  gatherNodeParts(node.id, parts);
157
143
  break;
158
-
159
144
  case "PrivateName":
160
145
  gatherNodeParts(node.id, parts);
161
146
  break;
162
-
163
147
  case "ParenthesizedExpression":
164
148
  gatherNodeParts(node.expression, parts);
165
149
  break;
166
-
167
150
  case "UnaryExpression":
168
151
  case "UpdateExpression":
169
152
  gatherNodeParts(node.argument, parts);
170
153
  break;
171
-
172
154
  case "MetaProperty":
173
155
  gatherNodeParts(node.meta, parts);
174
156
  gatherNodeParts(node.property, parts);
175
157
  break;
176
-
177
158
  case "JSXElement":
178
159
  gatherNodeParts(node.openingElement, parts);
179
160
  break;
180
-
181
161
  case "JSXOpeningElement":
182
- parts.push(node.name);
162
+ gatherNodeParts(node.name, parts);
183
163
  break;
184
-
185
164
  case "JSXFragment":
186
165
  gatherNodeParts(node.openingFragment, parts);
187
166
  break;
188
-
189
167
  case "JSXOpeningFragment":
190
168
  parts.push("Fragment");
191
169
  break;
192
-
193
170
  case "JSXNamespacedName":
194
171
  gatherNodeParts(node.namespace, parts);
195
172
  gatherNodeParts(node.name, parts);
196
173
  break;
197
174
  }
198
175
  }
199
-
200
176
  const collectorVisitor = {
201
177
  ForStatement(path) {
202
178
  const declar = path.get("init");
203
-
204
179
  if (declar.isVar()) {
205
180
  const {
206
181
  scope
@@ -209,7 +184,6 @@ const collectorVisitor = {
209
184
  parentScope.registerBinding("var", declar);
210
185
  }
211
186
  },
212
-
213
187
  Declaration(path) {
214
188
  if (path.isBlockScoped()) return;
215
189
  if (path.isImportDeclaration()) return;
@@ -217,19 +191,15 @@ const collectorVisitor = {
217
191
  const parent = path.scope.getFunctionParent() || path.scope.getProgramParent();
218
192
  parent.registerDeclaration(path);
219
193
  },
220
-
221
194
  ImportDeclaration(path) {
222
195
  const parent = path.scope.getBlockParent();
223
196
  parent.registerDeclaration(path);
224
197
  },
225
-
226
198
  ReferencedIdentifier(path, state) {
227
199
  state.references.push(path);
228
200
  },
229
-
230
201
  ForXStatement(path, state) {
231
202
  const left = path.get("left");
232
-
233
203
  if (left.isPattern() || left.isIdentifier()) {
234
204
  state.constantViolations.push(path);
235
205
  } else if (left.isVar()) {
@@ -240,7 +210,6 @@ const collectorVisitor = {
240
210
  parentScope.registerBinding("var", left);
241
211
  }
242
212
  },
243
-
244
213
  ExportDeclaration: {
245
214
  exit(path) {
246
215
  const {
@@ -249,7 +218,6 @@ const collectorVisitor = {
249
218
  } = path;
250
219
  if (isExportAllDeclaration(node)) return;
251
220
  const declar = node.declaration;
252
-
253
221
  if (isClassDeclaration(declar) || isFunctionDeclaration(declar)) {
254
222
  const id = declar.id;
255
223
  if (!id) return;
@@ -264,65 +232,51 @@ const collectorVisitor = {
264
232
  }
265
233
  }
266
234
  }
267
-
268
235
  },
269
-
270
236
  LabeledStatement(path) {
271
237
  path.scope.getBlockParent().registerDeclaration(path);
272
238
  },
273
-
274
239
  AssignmentExpression(path, state) {
275
240
  state.assignments.push(path);
276
241
  },
277
-
278
242
  UpdateExpression(path, state) {
279
243
  state.constantViolations.push(path);
280
244
  },
281
-
282
245
  UnaryExpression(path, state) {
283
246
  if (path.node.operator === "delete") {
284
247
  state.constantViolations.push(path);
285
248
  }
286
249
  },
287
-
288
250
  BlockScoped(path) {
289
251
  let scope = path.scope;
290
252
  if (scope.path === path) scope = scope.parent;
291
253
  const parent = scope.getBlockParent();
292
254
  parent.registerDeclaration(path);
293
-
294
255
  if (path.isClassDeclaration() && path.node.id) {
295
256
  const id = path.node.id;
296
257
  const name = id.name;
297
258
  path.scope.bindings[name] = path.scope.parent.getBinding(name);
298
259
  }
299
260
  },
300
-
301
261
  CatchClause(path) {
302
262
  path.scope.registerBinding("let", path);
303
263
  },
304
-
305
264
  Function(path) {
306
265
  const params = path.get("params");
307
-
308
266
  for (const param of params) {
309
267
  path.scope.registerBinding("param", param);
310
268
  }
311
-
312
269
  if (path.isFunctionExpression() && path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
313
270
  path.scope.registerBinding("local", path.get("id"), path);
314
271
  }
315
272
  },
316
-
317
273
  ClassExpression(path) {
318
274
  if (path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
319
275
  path.scope.registerBinding("local", path);
320
276
  }
321
277
  }
322
-
323
278
  };
324
279
  let uid = 0;
325
-
326
280
  class Scope {
327
281
  constructor(path) {
328
282
  this.uid = void 0;
@@ -339,50 +293,38 @@ class Scope {
339
293
  const {
340
294
  node
341
295
  } = path;
342
-
343
296
  const cached = _cache.scope.get(node);
344
-
345
297
  if ((cached == null ? void 0 : cached.path) === path) {
346
298
  return cached;
347
299
  }
348
-
349
300
  _cache.scope.set(node, this);
350
-
351
301
  this.uid = uid++;
352
302
  this.block = node;
353
303
  this.path = path;
354
304
  this.labels = new Map();
355
305
  this.inited = false;
356
306
  }
357
-
358
307
  get parent() {
359
308
  var _parent;
360
-
361
309
  let parent,
362
- path = this.path;
363
-
310
+ path = this.path;
364
311
  do {
365
- const isKey = path.key === "key";
312
+ const shouldSkip = path.key === "key" || path.listKey === "decorators";
366
313
  path = path.parentPath;
367
- if (isKey && path.isMethod()) path = path.parentPath;
314
+ if (shouldSkip && path.isMethod()) path = path.parentPath;
368
315
  if (path && path.isScope()) parent = path;
369
316
  } while (path && !parent);
370
-
371
317
  return (_parent = parent) == null ? void 0 : _parent.scope;
372
318
  }
373
-
374
319
  get parentBlock() {
375
320
  return this.path.parent;
376
321
  }
377
-
378
322
  get hub() {
379
323
  return this.path.hub;
380
324
  }
381
-
382
325
  traverse(node, opts, state) {
383
326
  (0, _index.default)(node, opts, this, state, this.path);
384
327
  }
385
-
386
328
  generateDeclaredUidIdentifier(name) {
387
329
  const id = this.generateUidIdentifier(name);
388
330
  this.push({
@@ -390,33 +332,27 @@ class Scope {
390
332
  });
391
333
  return cloneNode(id);
392
334
  }
393
-
394
335
  generateUidIdentifier(name) {
395
336
  return identifier(this.generateUid(name));
396
337
  }
397
-
398
338
  generateUid(name = "temp") {
399
339
  name = toIdentifier(name).replace(/^_+/, "").replace(/[0-9]+$/g, "");
400
340
  let uid;
401
341
  let i = 1;
402
-
403
342
  do {
404
343
  uid = this._generateUid(name, i);
405
344
  i++;
406
345
  } while (this.hasLabel(uid) || this.hasBinding(uid) || this.hasGlobal(uid) || this.hasReference(uid));
407
-
408
346
  const program = this.getProgramParent();
409
347
  program.references[uid] = true;
410
348
  program.uids[uid] = true;
411
349
  return uid;
412
350
  }
413
-
414
351
  _generateUid(name, i) {
415
352
  let id = name;
416
353
  if (i > 1) id += i;
417
354
  return `_${id}`;
418
355
  }
419
-
420
356
  generateUidBasedOnNode(node, defaultName) {
421
357
  const parts = [];
422
358
  gatherNodeParts(node, parts);
@@ -424,80 +360,67 @@ class Scope {
424
360
  id = id.replace(/^_/, "") || defaultName || "ref";
425
361
  return this.generateUid(id.slice(0, 20));
426
362
  }
427
-
428
363
  generateUidIdentifierBasedOnNode(node, defaultName) {
429
364
  return identifier(this.generateUidBasedOnNode(node, defaultName));
430
365
  }
431
-
432
366
  isStatic(node) {
433
- if (isThisExpression(node) || isSuper(node)) {
367
+ if (isThisExpression(node) || isSuper(node) || isTopicReference(node)) {
434
368
  return true;
435
369
  }
436
-
437
370
  if (isIdentifier(node)) {
438
371
  const binding = this.getBinding(node.name);
439
-
440
372
  if (binding) {
441
373
  return binding.constant;
442
374
  } else {
443
375
  return this.hasBinding(node.name);
444
376
  }
445
377
  }
446
-
447
378
  return false;
448
379
  }
449
-
450
380
  maybeGenerateMemoised(node, dontPush) {
451
381
  if (this.isStatic(node)) {
452
382
  return null;
453
383
  } else {
454
384
  const id = this.generateUidIdentifierBasedOnNode(node);
455
-
456
385
  if (!dontPush) {
457
386
  this.push({
458
387
  id
459
388
  });
460
389
  return cloneNode(id);
461
390
  }
462
-
463
391
  return id;
464
392
  }
465
393
  }
466
-
467
394
  checkBlockScopedCollisions(local, kind, name, id) {
468
395
  if (kind === "param") return;
469
396
  if (local.kind === "local") return;
470
397
  const duplicate = kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module" || local.kind === "param" && kind === "const";
471
-
472
398
  if (duplicate) {
473
399
  throw this.hub.buildError(id, `Duplicate declaration "${name}"`, TypeError);
474
400
  }
475
401
  }
476
-
477
- rename(oldName, newName, block) {
402
+ rename(oldName, newName) {
478
403
  const binding = this.getBinding(oldName);
479
-
480
404
  if (binding) {
481
- newName = newName || this.generateUidIdentifier(oldName).name;
482
- return new _renamer.default(binding, oldName, newName).rename(block);
405
+ newName || (newName = this.generateUidIdentifier(oldName).name);
406
+ const renamer = new _renamer.default(binding, oldName, newName);
407
+ {
408
+ renamer.rename(arguments[2]);
409
+ }
483
410
  }
484
411
  }
485
-
486
412
  _renameFromMap(map, oldName, newName, value) {
487
413
  if (map[oldName]) {
488
414
  map[newName] = value;
489
415
  map[oldName] = null;
490
416
  }
491
417
  }
492
-
493
418
  dump() {
494
419
  const sep = "-".repeat(60);
495
420
  console.log(sep);
496
421
  let scope = this;
497
-
498
422
  do {
499
423
  console.log("#", scope.block.type);
500
-
501
424
  for (const name of Object.keys(scope.bindings)) {
502
425
  const binding = scope.bindings[name];
503
426
  console.log(" -", name, {
@@ -508,61 +431,48 @@ class Scope {
508
431
  });
509
432
  }
510
433
  } while (scope = scope.parent);
511
-
512
434
  console.log(sep);
513
435
  }
514
-
515
436
  toArray(node, i, arrayLikeIsIterable) {
516
437
  if (isIdentifier(node)) {
517
438
  const binding = this.getBinding(node.name);
518
-
519
439
  if (binding != null && binding.constant && binding.path.isGenericType("Array")) {
520
440
  return node;
521
441
  }
522
442
  }
523
-
524
443
  if (isArrayExpression(node)) {
525
444
  return node;
526
445
  }
527
-
528
446
  if (isIdentifier(node, {
529
447
  name: "arguments"
530
448
  })) {
531
449
  return callExpression(memberExpression(memberExpression(memberExpression(identifier("Array"), identifier("prototype")), identifier("slice")), identifier("call")), [node]);
532
450
  }
533
-
534
451
  let helperName;
535
452
  const args = [node];
536
-
537
453
  if (i === true) {
538
454
  helperName = "toConsumableArray";
539
- } else if (i) {
455
+ } else if (typeof i === "number") {
540
456
  args.push(numericLiteral(i));
541
457
  helperName = "slicedToArray";
542
458
  } else {
543
459
  helperName = "toArray";
544
460
  }
545
-
546
461
  if (arrayLikeIsIterable) {
547
462
  args.unshift(this.hub.addHelper(helperName));
548
463
  helperName = "maybeArrayLike";
549
464
  }
550
-
551
465
  return callExpression(this.hub.addHelper(helperName), args);
552
466
  }
553
-
554
467
  hasLabel(name) {
555
468
  return !!this.getLabel(name);
556
469
  }
557
-
558
470
  getLabel(name) {
559
471
  return this.labels.get(name);
560
472
  }
561
-
562
473
  registerLabel(path) {
563
474
  this.labels.set(path.node.label.name, path);
564
475
  }
565
-
566
476
  registerDeclaration(path) {
567
477
  if (path.isLabeledStatement()) {
568
478
  this.registerLabel(path);
@@ -570,22 +480,24 @@ class Scope {
570
480
  this.registerBinding("hoisted", path.get("id"), path);
571
481
  } else if (path.isVariableDeclaration()) {
572
482
  const declarations = path.get("declarations");
573
-
483
+ const {
484
+ kind
485
+ } = path.node;
574
486
  for (const declar of declarations) {
575
- this.registerBinding(path.node.kind, declar);
487
+ this.registerBinding(kind === "using" || kind === "await using" ? "const" : kind, declar);
576
488
  }
577
489
  } else if (path.isClassDeclaration()) {
578
490
  if (path.node.declare) return;
579
491
  this.registerBinding("let", path);
580
492
  } else if (path.isImportDeclaration()) {
493
+ const isTypeDeclaration = path.node.importKind === "type" || path.node.importKind === "typeof";
581
494
  const specifiers = path.get("specifiers");
582
-
583
495
  for (const specifier of specifiers) {
584
- this.registerBinding("module", specifier);
496
+ const isTypeSpecifier = isTypeDeclaration || specifier.isImportSpecifier() && (specifier.node.importKind === "type" || specifier.node.importKind === "typeof");
497
+ this.registerBinding(isTypeSpecifier ? "unknown" : "module", specifier);
585
498
  }
586
499
  } else if (path.isExportDeclaration()) {
587
500
  const declar = path.get("declaration");
588
-
589
501
  if (declar.isClassDeclaration() || declar.isFunctionDeclaration() || declar.isVariableDeclaration()) {
590
502
  this.registerDeclaration(declar);
591
503
  }
@@ -593,47 +505,35 @@ class Scope {
593
505
  this.registerBinding("unknown", path);
594
506
  }
595
507
  }
596
-
597
508
  buildUndefinedNode() {
598
- return unaryExpression("void", numericLiteral(0), true);
509
+ return buildUndefinedNode();
599
510
  }
600
-
601
511
  registerConstantViolation(path) {
602
512
  const ids = path.getBindingIdentifiers();
603
-
604
513
  for (const name of Object.keys(ids)) {
605
- const binding = this.getBinding(name);
606
- if (binding) binding.reassign(path);
514
+ var _this$getBinding;
515
+ (_this$getBinding = this.getBinding(name)) == null ? void 0 : _this$getBinding.reassign(path);
607
516
  }
608
517
  }
609
-
610
518
  registerBinding(kind, path, bindingPath = path) {
611
519
  if (!kind) throw new ReferenceError("no `kind`");
612
-
613
520
  if (path.isVariableDeclaration()) {
614
521
  const declarators = path.get("declarations");
615
-
616
522
  for (const declar of declarators) {
617
523
  this.registerBinding(kind, declar);
618
524
  }
619
-
620
525
  return;
621
526
  }
622
-
623
527
  const parent = this.getProgramParent();
624
528
  const ids = path.getOuterBindingIdentifiers(true);
625
-
626
529
  for (const name of Object.keys(ids)) {
627
530
  parent.references[name] = true;
628
-
629
531
  for (const id of ids[name]) {
630
532
  const local = this.getOwnBinding(name);
631
-
632
533
  if (local) {
633
534
  if (local.identifier === id) continue;
634
535
  this.checkBlockScopedCollisions(local, kind, name, id);
635
536
  }
636
-
637
537
  if (local) {
638
538
  this.registerConstantViolation(bindingPath);
639
539
  } else {
@@ -647,74 +547,79 @@ class Scope {
647
547
  }
648
548
  }
649
549
  }
650
-
651
550
  addGlobal(node) {
652
551
  this.globals[node.name] = node;
653
552
  }
654
-
655
553
  hasUid(name) {
656
554
  let scope = this;
657
-
658
555
  do {
659
556
  if (scope.uids[name]) return true;
660
557
  } while (scope = scope.parent);
661
-
662
558
  return false;
663
559
  }
664
-
665
560
  hasGlobal(name) {
666
561
  let scope = this;
667
-
668
562
  do {
669
563
  if (scope.globals[name]) return true;
670
564
  } while (scope = scope.parent);
671
-
672
565
  return false;
673
566
  }
674
-
675
567
  hasReference(name) {
676
568
  return !!this.getProgramParent().references[name];
677
569
  }
678
-
679
570
  isPure(node, constantsOnly) {
680
571
  if (isIdentifier(node)) {
681
572
  const binding = this.getBinding(node.name);
682
573
  if (!binding) return false;
683
574
  if (constantsOnly) return binding.constant;
684
575
  return true;
576
+ } else if (isThisExpression(node) || isMetaProperty(node) || isTopicReference(node) || isPrivateName(node)) {
577
+ return true;
685
578
  } else if (isClass(node)) {
579
+ var _node$decorators;
686
580
  if (node.superClass && !this.isPure(node.superClass, constantsOnly)) {
687
581
  return false;
688
582
  }
689
-
583
+ if (((_node$decorators = node.decorators) == null ? void 0 : _node$decorators.length) > 0) {
584
+ return false;
585
+ }
690
586
  return this.isPure(node.body, constantsOnly);
691
587
  } else if (isClassBody(node)) {
692
588
  for (const method of node.body) {
693
589
  if (!this.isPure(method, constantsOnly)) return false;
694
590
  }
695
-
696
591
  return true;
697
592
  } else if (isBinary(node)) {
698
593
  return this.isPure(node.left, constantsOnly) && this.isPure(node.right, constantsOnly);
699
- } else if (isArrayExpression(node)) {
594
+ } else if (isArrayExpression(node) || isTupleExpression(node)) {
700
595
  for (const elem of node.elements) {
701
- if (!this.isPure(elem, constantsOnly)) return false;
596
+ if (elem !== null && !this.isPure(elem, constantsOnly)) return false;
702
597
  }
703
-
704
598
  return true;
705
- } else if (isObjectExpression(node)) {
599
+ } else if (isObjectExpression(node) || isRecordExpression(node)) {
706
600
  for (const prop of node.properties) {
707
601
  if (!this.isPure(prop, constantsOnly)) return false;
708
602
  }
709
-
710
603
  return true;
711
604
  } else if (isMethod(node)) {
605
+ var _node$decorators2;
712
606
  if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
713
- if (node.kind === "get" || node.kind === "set") return false;
607
+ if (((_node$decorators2 = node.decorators) == null ? void 0 : _node$decorators2.length) > 0) {
608
+ return false;
609
+ }
714
610
  return true;
715
611
  } else if (isProperty(node)) {
612
+ var _node$decorators3;
716
613
  if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
717
- return this.isPure(node.value, constantsOnly);
614
+ if (((_node$decorators3 = node.decorators) == null ? void 0 : _node$decorators3.length) > 0) {
615
+ return false;
616
+ }
617
+ if (isObjectProperty(node) || node.static) {
618
+ if (node.value !== null && !this.isPure(node.value, constantsOnly)) {
619
+ return false;
620
+ }
621
+ }
622
+ return true;
718
623
  } else if (isUnaryExpression(node)) {
719
624
  return this.isPure(node.argument, constantsOnly);
720
625
  } else if (isTaggedTemplateExpression(node)) {
@@ -723,42 +628,34 @@ class Scope {
723
628
  for (const expression of node.expressions) {
724
629
  if (!this.isPure(expression, constantsOnly)) return false;
725
630
  }
726
-
727
631
  return true;
728
632
  } else {
729
633
  return isPureish(node);
730
634
  }
731
635
  }
732
-
733
636
  setData(key, val) {
734
637
  return this.data[key] = val;
735
638
  }
736
-
737
639
  getData(key) {
738
640
  let scope = this;
739
-
740
641
  do {
741
642
  const data = scope.data[key];
742
643
  if (data != null) return data;
743
644
  } while (scope = scope.parent);
744
645
  }
745
-
746
646
  removeData(key) {
747
647
  let scope = this;
748
-
749
648
  do {
750
649
  const data = scope.data[key];
751
650
  if (data != null) scope.data[key] = null;
752
651
  } while (scope = scope.parent);
753
652
  }
754
-
755
653
  init() {
756
654
  if (!this.inited) {
757
655
  this.inited = true;
758
656
  this.crawl();
759
657
  }
760
658
  }
761
-
762
659
  crawl() {
763
660
  const path = this.path;
764
661
  this.references = Object.create(null);
@@ -774,245 +671,218 @@ class Scope {
774
671
  assignments: []
775
672
  };
776
673
  this.crawling = true;
777
-
778
- if (path.type !== "Program" && collectorVisitor._exploded) {
674
+ if (path.type !== "Program" && (0, _visitors.isExplodedVisitor)(collectorVisitor)) {
779
675
  for (const visit of collectorVisitor.enter) {
780
- visit(path, state);
676
+ visit.call(state, path, state);
781
677
  }
782
-
783
678
  const typeVisitors = collectorVisitor[path.type];
784
-
785
679
  if (typeVisitors) {
786
680
  for (const visit of typeVisitors.enter) {
787
- visit(path, state);
681
+ visit.call(state, path, state);
788
682
  }
789
683
  }
790
684
  }
791
-
792
685
  path.traverse(collectorVisitor, state);
793
686
  this.crawling = false;
794
-
795
687
  for (const path of state.assignments) {
796
688
  const ids = path.getBindingIdentifiers();
797
-
798
689
  for (const name of Object.keys(ids)) {
799
690
  if (path.scope.getBinding(name)) continue;
800
691
  programParent.addGlobal(ids[name]);
801
692
  }
802
-
803
693
  path.scope.registerConstantViolation(path);
804
694
  }
805
-
806
695
  for (const ref of state.references) {
807
696
  const binding = ref.scope.getBinding(ref.node.name);
808
-
809
697
  if (binding) {
810
698
  binding.reference(ref);
811
699
  } else {
812
700
  programParent.addGlobal(ref.node);
813
701
  }
814
702
  }
815
-
816
703
  for (const path of state.constantViolations) {
817
704
  path.scope.registerConstantViolation(path);
818
705
  }
819
706
  }
820
-
821
707
  push(opts) {
822
708
  let path = this.path;
823
-
824
- if (!path.isBlockStatement() && !path.isProgram()) {
709
+ if (path.isPattern()) {
710
+ path = this.getPatternParent().path;
711
+ } else if (!path.isBlockStatement() && !path.isProgram()) {
825
712
  path = this.getBlockParent().path;
826
713
  }
827
-
828
714
  if (path.isSwitchStatement()) {
829
715
  path = (this.getFunctionParent() || this.getProgramParent()).path;
830
716
  }
831
-
717
+ const {
718
+ init,
719
+ unique,
720
+ kind = "var",
721
+ id
722
+ } = opts;
723
+ if (!init && !unique && (kind === "var" || kind === "let") && path.isFunction() && !path.node.name && t.isCallExpression(path.parent, {
724
+ callee: path.node
725
+ }) && path.parent.arguments.length <= path.node.params.length && t.isIdentifier(id)) {
726
+ path.pushContainer("params", id);
727
+ path.scope.registerBinding("param", path.get("params")[path.node.params.length - 1]);
728
+ return;
729
+ }
832
730
  if (path.isLoop() || path.isCatchClause() || path.isFunction()) {
833
731
  path.ensureBlock();
834
732
  path = path.get("body");
835
733
  }
836
-
837
- const unique = opts.unique;
838
- const kind = opts.kind || "var";
839
734
  const blockHoist = opts._blockHoist == null ? 2 : opts._blockHoist;
840
735
  const dataKey = `declaration:${kind}:${blockHoist}`;
841
736
  let declarPath = !unique && path.getData(dataKey);
842
-
843
737
  if (!declarPath) {
844
738
  const declar = variableDeclaration(kind, []);
845
739
  declar._blockHoist = blockHoist;
846
740
  [declarPath] = path.unshiftContainer("body", [declar]);
847
741
  if (!unique) path.setData(dataKey, declarPath);
848
742
  }
849
-
850
- const declarator = variableDeclarator(opts.id, opts.init);
743
+ const declarator = variableDeclarator(id, init);
851
744
  const len = declarPath.node.declarations.push(declarator);
852
745
  path.scope.registerBinding(kind, declarPath.get("declarations")[len - 1]);
853
746
  }
854
-
855
747
  getProgramParent() {
856
748
  let scope = this;
857
-
858
749
  do {
859
750
  if (scope.path.isProgram()) {
860
751
  return scope;
861
752
  }
862
753
  } while (scope = scope.parent);
863
-
864
754
  throw new Error("Couldn't find a Program");
865
755
  }
866
-
867
756
  getFunctionParent() {
868
757
  let scope = this;
869
-
870
758
  do {
871
759
  if (scope.path.isFunctionParent()) {
872
760
  return scope;
873
761
  }
874
762
  } while (scope = scope.parent);
875
-
876
763
  return null;
877
764
  }
878
-
879
765
  getBlockParent() {
880
766
  let scope = this;
881
-
882
767
  do {
883
768
  if (scope.path.isBlockParent()) {
884
769
  return scope;
885
770
  }
886
771
  } while (scope = scope.parent);
887
-
888
772
  throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
889
773
  }
890
-
774
+ getPatternParent() {
775
+ let scope = this;
776
+ do {
777
+ if (!scope.path.isPattern()) {
778
+ return scope.getBlockParent();
779
+ }
780
+ } while (scope = scope.parent.parent);
781
+ throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
782
+ }
891
783
  getAllBindings() {
892
784
  const ids = Object.create(null);
893
785
  let scope = this;
894
-
895
786
  do {
896
787
  for (const key of Object.keys(scope.bindings)) {
897
788
  if (key in ids === false) {
898
789
  ids[key] = scope.bindings[key];
899
790
  }
900
791
  }
901
-
902
792
  scope = scope.parent;
903
793
  } while (scope);
904
-
905
794
  return ids;
906
795
  }
907
-
908
796
  getAllBindingsOfKind(...kinds) {
909
797
  const ids = Object.create(null);
910
-
911
798
  for (const kind of kinds) {
912
799
  let scope = this;
913
-
914
800
  do {
915
801
  for (const name of Object.keys(scope.bindings)) {
916
802
  const binding = scope.bindings[name];
917
803
  if (binding.kind === kind) ids[name] = binding;
918
804
  }
919
-
920
805
  scope = scope.parent;
921
806
  } while (scope);
922
807
  }
923
-
924
808
  return ids;
925
809
  }
926
-
927
810
  bindingIdentifierEquals(name, node) {
928
811
  return this.getBindingIdentifier(name) === node;
929
812
  }
930
-
931
813
  getBinding(name) {
932
814
  let scope = this;
933
815
  let previousPath;
934
-
935
816
  do {
936
817
  const binding = scope.getOwnBinding(name);
937
-
938
818
  if (binding) {
939
819
  var _previousPath;
940
-
941
820
  if ((_previousPath = previousPath) != null && _previousPath.isPattern() && binding.kind !== "param" && binding.kind !== "local") {} else {
942
821
  return binding;
943
822
  }
944
823
  } else if (!binding && name === "arguments" && scope.path.isFunction() && !scope.path.isArrowFunctionExpression()) {
945
824
  break;
946
825
  }
947
-
948
826
  previousPath = scope.path;
949
827
  } while (scope = scope.parent);
950
828
  }
951
-
952
829
  getOwnBinding(name) {
953
830
  return this.bindings[name];
954
831
  }
955
-
956
832
  getBindingIdentifier(name) {
957
- var _this$getBinding;
958
-
959
- return (_this$getBinding = this.getBinding(name)) == null ? void 0 : _this$getBinding.identifier;
833
+ var _this$getBinding2;
834
+ return (_this$getBinding2 = this.getBinding(name)) == null ? void 0 : _this$getBinding2.identifier;
960
835
  }
961
-
962
836
  getOwnBindingIdentifier(name) {
963
837
  const binding = this.bindings[name];
964
838
  return binding == null ? void 0 : binding.identifier;
965
839
  }
966
-
967
840
  hasOwnBinding(name) {
968
841
  return !!this.getOwnBinding(name);
969
842
  }
970
-
971
- hasBinding(name, noGlobals) {
843
+ hasBinding(name, opts) {
844
+ var _opts, _opts2, _opts3;
972
845
  if (!name) return false;
973
846
  if (this.hasOwnBinding(name)) return true;
974
- if (this.parentHasBinding(name, noGlobals)) return true;
975
- if (this.hasUid(name)) return true;
976
- if (!noGlobals && Scope.globals.includes(name)) return true;
977
- if (!noGlobals && Scope.contextVariables.includes(name)) return true;
847
+ {
848
+ if (typeof opts === "boolean") opts = {
849
+ noGlobals: opts
850
+ };
851
+ }
852
+ if (this.parentHasBinding(name, opts)) return true;
853
+ if (!((_opts = opts) != null && _opts.noUids) && this.hasUid(name)) return true;
854
+ if (!((_opts2 = opts) != null && _opts2.noGlobals) && Scope.globals.includes(name)) return true;
855
+ if (!((_opts3 = opts) != null && _opts3.noGlobals) && Scope.contextVariables.includes(name)) return true;
978
856
  return false;
979
857
  }
980
-
981
- parentHasBinding(name, noGlobals) {
858
+ parentHasBinding(name, opts) {
982
859
  var _this$parent;
983
-
984
- return (_this$parent = this.parent) == null ? void 0 : _this$parent.hasBinding(name, noGlobals);
860
+ return (_this$parent = this.parent) == null ? void 0 : _this$parent.hasBinding(name, opts);
985
861
  }
986
-
987
862
  moveBindingTo(name, scope) {
988
863
  const info = this.getBinding(name);
989
-
990
864
  if (info) {
991
865
  info.scope.removeOwnBinding(name);
992
866
  info.scope = scope;
993
867
  scope.bindings[name] = info;
994
868
  }
995
869
  }
996
-
997
870
  removeOwnBinding(name) {
998
871
  delete this.bindings[name];
999
872
  }
1000
-
1001
873
  removeBinding(name) {
1002
- var _this$getBinding2;
1003
-
1004
- (_this$getBinding2 = this.getBinding(name)) == null ? void 0 : _this$getBinding2.scope.removeOwnBinding(name);
874
+ var _this$getBinding3;
875
+ (_this$getBinding3 = this.getBinding(name)) == null ? void 0 : _this$getBinding3.scope.removeOwnBinding(name);
1005
876
  let scope = this;
1006
-
1007
877
  do {
1008
878
  if (scope.uids[name]) {
1009
879
  scope.uids[name] = false;
1010
880
  }
1011
881
  } while (scope = scope.parent);
1012
882
  }
1013
-
1014
883
  }
1015
-
1016
884
  exports.default = Scope;
1017
885
  Scope.globals = Object.keys(_globals.builtin);
1018
- Scope.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];
886
+ Scope.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];
887
+
888
+ //# sourceMappingURL=index.js.map