@babel/traverse 7.8.3 → 7.9.5

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.

Potentially problematic release.


This version of @babel/traverse might be problematic. Click here for more details.

@@ -25,9 +25,9 @@ function shareCommentsWithSiblings() {
25
25
  const hasPrev = Boolean(prev.node);
26
26
  const hasNext = Boolean(next.node);
27
27
 
28
- if (hasPrev && hasNext) {} else if (hasPrev) {
28
+ if (hasPrev && !hasNext) {
29
29
  prev.addComments("trailing", trailing);
30
- } else if (hasNext) {
30
+ } else if (hasNext && !hasPrev) {
31
31
  next.addComments("leading", leading);
32
32
  }
33
33
  }
@@ -19,7 +19,9 @@ function remove() {
19
19
 
20
20
  this.resync();
21
21
 
22
- this._removeFromScope();
22
+ if (!this.opts || !this.opts.noScope) {
23
+ this._removeFromScope();
24
+ }
23
25
 
24
26
  if (this._callRemovalHooks()) {
25
27
  this._markRemoved();
@@ -30,37 +30,143 @@ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj;
30
30
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
31
31
 
32
32
  function gatherNodeParts(node, parts) {
33
- if (t.isModuleDeclaration(node)) {
34
- if (node.source) {
35
- gatherNodeParts(node.source, parts);
36
- } else if (node.specifiers && node.specifiers.length) {
37
- for (const specifier of node.specifiers) {
38
- gatherNodeParts(specifier, parts);
33
+ switch (node == null ? void 0 : node.type) {
34
+ default:
35
+ if (t.isModuleDeclaration(node)) {
36
+ if (node.source) {
37
+ gatherNodeParts(node.source, parts);
38
+ } else if (node.specifiers && node.specifiers.length) {
39
+ for (const e of node.specifiers) gatherNodeParts(e, parts);
40
+ } else if (node.declaration) {
41
+ gatherNodeParts(node.declaration, parts);
42
+ }
43
+ } else if (t.isModuleSpecifier(node)) {
44
+ gatherNodeParts(node.local, parts);
45
+ } else if (t.isLiteral(node)) {
46
+ parts.push(node.value);
47
+ }
48
+
49
+ break;
50
+
51
+ case "MemberExpression":
52
+ case "OptionalMemberExpression":
53
+ case "JSXMemberExpression":
54
+ gatherNodeParts(node.object, parts);
55
+ gatherNodeParts(node.property, parts);
56
+ break;
57
+
58
+ case "Identifier":
59
+ case "JSXIdentifier":
60
+ parts.push(node.name);
61
+ break;
62
+
63
+ case "CallExpression":
64
+ case "OptionalCallExpression":
65
+ case "NewExpression":
66
+ gatherNodeParts(node.callee, parts);
67
+ break;
68
+
69
+ case "ObjectExpression":
70
+ case "ObjectPattern":
71
+ for (const e of node.properties) {
72
+ gatherNodeParts(e, parts);
39
73
  }
40
- } else if (node.declaration) {
41
- gatherNodeParts(node.declaration, parts);
42
- }
43
- } else if (t.isModuleSpecifier(node)) {
44
- gatherNodeParts(node.local, parts);
45
- } else if (t.isMemberExpression(node)) {
46
- gatherNodeParts(node.object, parts);
47
- gatherNodeParts(node.property, parts);
48
- } else if (t.isIdentifier(node)) {
49
- parts.push(node.name);
50
- } else if (t.isLiteral(node)) {
51
- parts.push(node.value);
52
- } else if (t.isCallExpression(node)) {
53
- gatherNodeParts(node.callee, parts);
54
- } else if (t.isObjectExpression(node) || t.isObjectPattern(node)) {
55
- for (const prop of node.properties) {
56
- gatherNodeParts(prop.key || prop.argument, parts);
57
- }
58
- } else if (t.isPrivateName(node)) {
59
- gatherNodeParts(node.id, parts);
60
- } else if (t.isThisExpression(node)) {
61
- parts.push("this");
62
- } else if (t.isSuper(node)) {
63
- parts.push("super");
74
+
75
+ break;
76
+
77
+ case "SpreadElement":
78
+ case "RestElement":
79
+ gatherNodeParts(node.argument, parts);
80
+ break;
81
+
82
+ case "ObjectProperty":
83
+ case "ObjectMethod":
84
+ case "ClassProperty":
85
+ case "ClassMethod":
86
+ case "ClassPrivateProperty":
87
+ case "ClassPrivateMethod":
88
+ gatherNodeParts(node.key, parts);
89
+ break;
90
+
91
+ case "ThisExpression":
92
+ parts.push("this");
93
+ break;
94
+
95
+ case "Super":
96
+ parts.push("super");
97
+ break;
98
+
99
+ case "Import":
100
+ parts.push("import");
101
+ break;
102
+
103
+ case "DoExpression":
104
+ parts.push("do");
105
+ break;
106
+
107
+ case "YieldExpression":
108
+ parts.push("yield");
109
+ gatherNodeParts(node.argument, parts);
110
+ break;
111
+
112
+ case "AwaitExpression":
113
+ parts.push("await");
114
+ gatherNodeParts(node.argument, parts);
115
+ break;
116
+
117
+ case "AssignmentExpression":
118
+ gatherNodeParts(node.left, parts);
119
+ break;
120
+
121
+ case "VariableDeclarator":
122
+ gatherNodeParts(node.id, parts);
123
+ break;
124
+
125
+ case "FunctionExpression":
126
+ case "FunctionDeclaration":
127
+ case "ClassExpression":
128
+ case "ClassDeclaration":
129
+ gatherNodeParts(node.id, parts);
130
+ break;
131
+
132
+ case "PrivateName":
133
+ gatherNodeParts(node.id, parts);
134
+ break;
135
+
136
+ case "ParenthesizedExpression":
137
+ gatherNodeParts(node.expression, parts);
138
+ break;
139
+
140
+ case "UnaryExpression":
141
+ case "UpdateExpression":
142
+ gatherNodeParts(node.argument, parts);
143
+ break;
144
+
145
+ case "MetaProperty":
146
+ gatherNodeParts(node.meta, parts);
147
+ gatherNodeParts(node.property, parts);
148
+ break;
149
+
150
+ case "JSXElement":
151
+ gatherNodeParts(node.openingElement, parts);
152
+ break;
153
+
154
+ case "JSXOpeningElement":
155
+ parts.push(node.name);
156
+ break;
157
+
158
+ case "JSXFragment":
159
+ gatherNodeParts(node.openingFragment, parts);
160
+ break;
161
+
162
+ case "JSXOpeningFragment":
163
+ parts.push("Fragment");
164
+ break;
165
+
166
+ case "JSXNamespacedName":
167
+ gatherNodeParts(node.namespace, parts);
168
+ gatherNodeParts(node.name, parts);
169
+ break;
64
170
  }
65
171
  }
66
172
 
@@ -146,14 +252,14 @@ const collectorVisitor = {
146
252
  BlockScoped(path) {
147
253
  let scope = path.scope;
148
254
  if (scope.path === path) scope = scope.parent;
149
- scope.getBlockParent().registerDeclaration(path);
150
- },
255
+ const parent = scope.getBlockParent();
256
+ parent.registerDeclaration(path);
151
257
 
152
- ClassDeclaration(path) {
153
- const id = path.node.id;
154
- if (!id) return;
155
- const name = id.name;
156
- path.scope.bindings[name] = path.scope.getBinding(name);
258
+ if (path.isClassDeclaration() && path.node.id) {
259
+ const id = path.node.id;
260
+ const name = id.name;
261
+ path.scope.bindings[name] = path.scope.parent.getBinding(name);
262
+ }
157
263
  },
158
264
 
159
265
  Block(path) {
@@ -164,6 +270,28 @@ const collectorVisitor = {
164
270
  path.scope.getBlockParent().registerDeclaration(bodyPath);
165
271
  }
166
272
  }
273
+ },
274
+
275
+ CatchClause(path) {
276
+ path.scope.registerBinding("let", path);
277
+ },
278
+
279
+ Function(path) {
280
+ if (path.isFunctionExpression() && path.has("id") && !path.get("id").node[t.NOT_LOCAL_BINDING]) {
281
+ path.scope.registerBinding("local", path.get("id"), path);
282
+ }
283
+
284
+ const params = path.get("params");
285
+
286
+ for (const param of params) {
287
+ path.scope.registerBinding("param", param);
288
+ }
289
+ },
290
+
291
+ ClassExpression(path) {
292
+ if (path.has("id") && !path.get("id").node[t.NOT_LOCAL_BINDING]) {
293
+ path.scope.registerBinding("local", path);
294
+ }
167
295
  }
168
296
 
169
297
  };
@@ -187,6 +315,7 @@ class Scope {
187
315
  this.block = node;
188
316
  this.path = path;
189
317
  this.labels = new Map();
318
+ this.inited = false;
190
319
  }
191
320
 
192
321
  get parent() {
@@ -240,17 +369,7 @@ class Scope {
240
369
  return `_${id}`;
241
370
  }
242
371
 
243
- generateUidBasedOnNode(parent, defaultName) {
244
- let node = parent;
245
-
246
- if (t.isAssignmentExpression(parent)) {
247
- node = parent.left;
248
- } else if (t.isVariableDeclarator(parent)) {
249
- node = parent.id;
250
- } else if (t.isObjectProperty(node) || t.isObjectMethod(node)) {
251
- node = node.key;
252
- }
253
-
372
+ generateUidBasedOnNode(node, defaultName) {
254
373
  const parts = [];
255
374
  gatherNodeParts(node, parts);
256
375
  let id = parts.join("$");
@@ -258,8 +377,8 @@ class Scope {
258
377
  return this.generateUid(id.slice(0, 20));
259
378
  }
260
379
 
261
- generateUidIdentifierBasedOnNode(parent, defaultName) {
262
- return t.identifier(this.generateUidBasedOnNode(parent, defaultName));
380
+ generateUidIdentifierBasedOnNode(node, defaultName) {
381
+ return t.identifier(this.generateUidBasedOnNode(node, defaultName));
263
382
  }
264
383
 
265
384
  isStatic(node) {
@@ -451,6 +570,8 @@ class Scope {
451
570
  const ids = path.getOuterBindingIdentifiers(true);
452
571
 
453
572
  for (const name of Object.keys(ids)) {
573
+ parent.references[name] = true;
574
+
454
575
  for (const id of ids[name]) {
455
576
  const local = this.getOwnBinding(name);
456
577
 
@@ -459,8 +580,6 @@ class Scope {
459
580
  this.checkBlockScopedCollisions(local, kind, name, id);
460
581
  }
461
582
 
462
- parent.references[name] = true;
463
-
464
583
  if (local) {
465
584
  this.registerConstantViolation(bindingPath);
466
585
  } else {
@@ -500,13 +619,7 @@ class Scope {
500
619
  }
501
620
 
502
621
  hasReference(name) {
503
- let scope = this;
504
-
505
- do {
506
- if (scope.references[name]) return true;
507
- } while (scope = scope.parent);
508
-
509
- return false;
622
+ return !!this.getProgramParent().references[name];
510
623
  }
511
624
 
512
625
  isPure(node, constantsOnly) {
@@ -586,7 +699,10 @@ class Scope {
586
699
  }
587
700
 
588
701
  init() {
589
- if (!this.references) this.crawl();
702
+ if (!this.inited) {
703
+ this.inited = true;
704
+ this.crawl();
705
+ }
590
706
  }
591
707
 
592
708
  crawl() {
@@ -597,26 +713,11 @@ class Scope {
597
713
  this.uids = Object.create(null);
598
714
  this.data = Object.create(null);
599
715
 
600
- if (path.isLoop()) {
601
- for (const key of t.FOR_INIT_KEYS) {
602
- const node = path.get(key);
603
- if (node.isBlockScoped()) this.registerBinding(node.node.kind, node);
604
- }
605
- }
606
-
607
- if (path.isFunctionExpression() && path.has("id")) {
608
- if (!path.get("id").node[t.NOT_LOCAL_BINDING]) {
716
+ if (path.isFunction()) {
717
+ if (path.isFunctionExpression() && path.has("id") && !path.get("id").node[t.NOT_LOCAL_BINDING]) {
609
718
  this.registerBinding("local", path.get("id"), path);
610
719
  }
611
- }
612
-
613
- if (path.isClassExpression() && path.has("id")) {
614
- if (!path.get("id").node[t.NOT_LOCAL_BINDING]) {
615
- this.registerBinding("local", path);
616
- }
617
- }
618
720
 
619
- if (path.isFunction()) {
620
721
  const params = path.get("params");
621
722
 
622
723
  for (const param of params) {
@@ -624,12 +725,8 @@ class Scope {
624
725
  }
625
726
  }
626
727
 
627
- if (path.isCatchClause()) {
628
- this.registerBinding("let", path);
629
- }
630
-
631
- const parent = this.getProgramParent();
632
- if (parent.crawling) return;
728
+ const programParent = this.getProgramParent();
729
+ if (programParent.crawling) return;
633
730
  const state = {
634
731
  references: [],
635
732
  constantViolations: [],
@@ -641,11 +738,9 @@ class Scope {
641
738
 
642
739
  for (const path of state.assignments) {
643
740
  const ids = path.getBindingIdentifiers();
644
- let programParent;
645
741
 
646
742
  for (const name of Object.keys(ids)) {
647
743
  if (path.scope.getBinding(name)) continue;
648
- programParent = programParent || path.scope.getProgramParent();
649
744
  programParent.addGlobal(ids[name]);
650
745
  }
651
746
 
@@ -658,7 +753,7 @@ class Scope {
658
753
  if (binding) {
659
754
  binding.reference(ref);
660
755
  } else {
661
- ref.scope.getProgramParent().addGlobal(ref.node);
756
+ programParent.addGlobal(ref.node);
662
757
  }
663
758
  }
664
759
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@babel/traverse",
3
- "version": "7.8.3",
3
+ "version": "7.9.5",
4
4
  "description": "The Babel Traverse module maintains the overall tree state, and is responsible for replacing, removing, and adding nodes",
5
5
  "author": "Sebastian McKenzie <sebmck@gmail.com>",
6
6
  "homepage": "https://babeljs.io/",
@@ -12,11 +12,11 @@
12
12
  "main": "lib/index.js",
13
13
  "dependencies": {
14
14
  "@babel/code-frame": "^7.8.3",
15
- "@babel/generator": "^7.8.3",
16
- "@babel/helper-function-name": "^7.8.3",
15
+ "@babel/generator": "^7.9.5",
16
+ "@babel/helper-function-name": "^7.9.5",
17
17
  "@babel/helper-split-export-declaration": "^7.8.3",
18
- "@babel/parser": "^7.8.3",
19
- "@babel/types": "^7.8.3",
18
+ "@babel/parser": "^7.9.0",
19
+ "@babel/types": "^7.9.5",
20
20
  "debug": "^4.1.0",
21
21
  "globals": "^11.1.0",
22
22
  "lodash": "^4.17.13"
@@ -24,5 +24,5 @@
24
24
  "devDependencies": {
25
25
  "@babel/helper-plugin-test-runner": "^7.8.3"
26
26
  },
27
- "gitHead": "a7620bd266ae1345975767bbc7abf09034437017"
27
+ "gitHead": "5b97e77e030cf3853a147fdff81844ea4026219d"
28
28
  }