@babel/traverse 7.13.0 → 7.14.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.

Potentially problematic release.


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

package/lib/context.js CHANGED
@@ -32,7 +32,7 @@ class TraversalContext {
32
32
  if (opts.enter || opts.exit) return true;
33
33
  if (opts[node.type]) return true;
34
34
  const keys = t.VISITOR_KEYS[node.type];
35
- if (!(keys == null ? void 0 : keys.length)) return false;
35
+ if (!(keys != null && keys.length)) return false;
36
36
 
37
37
  for (const key of keys) {
38
38
  if (node[key]) return true;
package/lib/index.js CHANGED
@@ -101,7 +101,7 @@ function hasDenylistedType(path, state) {
101
101
  }
102
102
 
103
103
  traverse.hasType = function (tree, type, denylistTypes) {
104
- if (denylistTypes == null ? void 0 : denylistTypes.includes(tree.type)) return false;
104
+ if (denylistTypes != null && denylistTypes.includes(tree.type)) return false;
105
105
  if (tree.type === type) return true;
106
106
  const state = {
107
107
  has: false,
@@ -129,7 +129,7 @@ function _evaluate(path, state) {
129
129
  return deopt(binding.path, state);
130
130
  }
131
131
 
132
- if (binding == null ? void 0 : binding.hasValue) {
132
+ if (binding != null && binding.hasValue) {
133
133
  return binding.value;
134
134
  } else {
135
135
  if (path.node.name === "undefined") {
@@ -28,6 +28,23 @@ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj;
28
28
 
29
29
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
30
30
 
31
+ const NORMAL_COMPLETION = 0;
32
+ const BREAK_COMPLETION = 1;
33
+
34
+ function NormalCompletion(path) {
35
+ return {
36
+ type: NORMAL_COMPLETION,
37
+ path
38
+ };
39
+ }
40
+
41
+ function BreakCompletion(path) {
42
+ return {
43
+ type: BREAK_COMPLETION,
44
+ path
45
+ };
46
+ }
47
+
31
48
  function getOpposite() {
32
49
  if (this.key === "left") {
33
50
  return this.getSibling("right");
@@ -38,102 +55,163 @@ function getOpposite() {
38
55
  return null;
39
56
  }
40
57
 
41
- function addCompletionRecords(path, paths) {
42
- if (path) return paths.concat(path.getCompletionRecords());
43
- return paths;
58
+ function addCompletionRecords(path, records, context) {
59
+ if (path) return records.concat(_getCompletionRecords(path, context));
60
+ return records;
44
61
  }
45
62
 
46
- function findBreak(statements) {
47
- let breakStatement;
63
+ function completionRecordForSwitch(cases, records, context) {
64
+ let lastNormalCompletions = [];
48
65
 
49
- if (!Array.isArray(statements)) {
50
- statements = [statements];
51
- }
66
+ for (let i = 0; i < cases.length; i++) {
67
+ const casePath = cases[i];
68
+
69
+ const caseCompletions = _getCompletionRecords(casePath, context);
52
70
 
53
- for (const statement of statements) {
54
- if (statement.isDoExpression() || statement.isProgram() || statement.isBlockStatement() || statement.isCatchClause() || statement.isLabeledStatement()) {
55
- breakStatement = findBreak(statement.get("body"));
56
- } else if (statement.isIfStatement()) {
57
- var _findBreak;
71
+ const normalCompletions = [];
72
+ const breakCompletions = [];
58
73
 
59
- breakStatement = (_findBreak = findBreak(statement.get("consequent"))) != null ? _findBreak : findBreak(statement.get("alternate"));
60
- } else if (statement.isTryStatement()) {
61
- var _findBreak2;
74
+ for (const c of caseCompletions) {
75
+ if (c.type === NORMAL_COMPLETION) {
76
+ normalCompletions.push(c);
77
+ }
62
78
 
63
- breakStatement = (_findBreak2 = findBreak(statement.get("block"))) != null ? _findBreak2 : findBreak(statement.get("handler"));
64
- } else if (statement.isBreakStatement()) {
65
- breakStatement = statement;
79
+ if (c.type === BREAK_COMPLETION) {
80
+ breakCompletions.push(c);
81
+ }
66
82
  }
67
83
 
68
- if (breakStatement) {
69
- return breakStatement;
84
+ if (normalCompletions.length) {
85
+ lastNormalCompletions = normalCompletions;
70
86
  }
87
+
88
+ records = records.concat(breakCompletions);
71
89
  }
72
90
 
73
- return null;
91
+ records = records.concat(lastNormalCompletions);
92
+ return records;
74
93
  }
75
94
 
76
- function completionRecordForSwitch(cases, paths) {
77
- let isLastCaseWithConsequent = true;
78
-
79
- for (let i = cases.length - 1; i >= 0; i--) {
80
- const switchCase = cases[i];
81
- const consequent = switchCase.get("consequent");
82
- let breakStatement = findBreak(consequent);
95
+ function normalCompletionToBreak(completions) {
96
+ completions.forEach(c => {
97
+ c.type = BREAK_COMPLETION;
98
+ });
99
+ }
83
100
 
84
- if (breakStatement) {
85
- while (breakStatement.key === 0 && breakStatement.parentPath.isBlockStatement()) {
86
- breakStatement = breakStatement.parentPath;
101
+ function replaceBreakStatementInBreakCompletion(completions, reachable) {
102
+ completions.forEach(c => {
103
+ if (c.path.isBreakStatement({
104
+ label: null
105
+ })) {
106
+ if (reachable) {
107
+ c.path.replaceWith(t.unaryExpression("void", t.numericLiteral(0)));
108
+ } else {
109
+ c.path.remove();
87
110
  }
111
+ }
112
+ });
113
+ }
88
114
 
89
- const prevSibling = breakStatement.getPrevSibling();
115
+ function getStatementListCompletion(paths, context) {
116
+ let completions = [];
90
117
 
91
- if (breakStatement.key > 0 && (prevSibling.isExpressionStatement() || prevSibling.isBlockStatement())) {
92
- paths = addCompletionRecords(prevSibling, paths);
93
- breakStatement.remove();
94
- } else {
95
- breakStatement.replaceWith(breakStatement.scope.buildUndefinedNode());
96
- paths = addCompletionRecords(breakStatement, paths);
118
+ if (context.canHaveBreak) {
119
+ let lastNormalCompletions = [];
120
+
121
+ for (let i = 0; i < paths.length; i++) {
122
+ const path = paths[i];
123
+ const newContext = Object.assign({}, context, {
124
+ inCaseClause: false
125
+ });
126
+
127
+ if (path.isBlockStatement() && (context.inCaseClause || context.shouldPopulateBreak)) {
128
+ newContext.shouldPopulateBreak = true;
129
+ } else {
130
+ newContext.shouldPopulateBreak = false;
97
131
  }
98
- } else if (isLastCaseWithConsequent) {
99
- const statementFinder = statement => !statement.isBlockStatement() || statement.get("body").some(statementFinder);
100
132
 
101
- const hasConsequent = consequent.some(statementFinder);
133
+ const statementCompletions = _getCompletionRecords(path, newContext);
134
+
135
+ if (statementCompletions.length > 0 && statementCompletions.every(c => c.type === BREAK_COMPLETION)) {
136
+ if (lastNormalCompletions.length > 0 && statementCompletions.every(c => c.path.isBreakStatement({
137
+ label: null
138
+ }))) {
139
+ normalCompletionToBreak(lastNormalCompletions);
140
+ completions = completions.concat(lastNormalCompletions);
141
+
142
+ if (lastNormalCompletions.some(c => c.path.isDeclaration())) {
143
+ completions = completions.concat(statementCompletions);
144
+ replaceBreakStatementInBreakCompletion(statementCompletions, true);
145
+ }
102
146
 
103
- if (hasConsequent) {
104
- paths = addCompletionRecords(consequent[consequent.length - 1], paths);
105
- isLastCaseWithConsequent = false;
147
+ replaceBreakStatementInBreakCompletion(statementCompletions, false);
148
+ } else {
149
+ completions = completions.concat(statementCompletions);
150
+
151
+ if (!context.shouldPopulateBreak) {
152
+ replaceBreakStatementInBreakCompletion(statementCompletions, true);
153
+ }
154
+ }
155
+
156
+ break;
157
+ }
158
+
159
+ if (i === paths.length - 1) {
160
+ completions = completions.concat(statementCompletions);
161
+ } else {
162
+ completions = completions.concat(statementCompletions.filter(c => c.type === BREAK_COMPLETION));
163
+ lastNormalCompletions = statementCompletions.filter(c => c.type === NORMAL_COMPLETION);
106
164
  }
107
165
  }
166
+ } else if (paths.length) {
167
+ completions = completions.concat(_getCompletionRecords(paths[paths.length - 1], context));
108
168
  }
109
169
 
110
- return paths;
170
+ return completions;
111
171
  }
112
172
 
113
- function getCompletionRecords() {
114
- let paths = [];
115
-
116
- if (this.isIfStatement()) {
117
- paths = addCompletionRecords(this.get("consequent"), paths);
118
- paths = addCompletionRecords(this.get("alternate"), paths);
119
- } else if (this.isDoExpression() || this.isFor() || this.isWhile()) {
120
- paths = addCompletionRecords(this.get("body"), paths);
121
- } else if (this.isProgram() || this.isBlockStatement()) {
122
- paths = addCompletionRecords(this.get("body").pop(), paths);
123
- } else if (this.isFunction()) {
124
- return this.get("body").getCompletionRecords();
125
- } else if (this.isTryStatement()) {
126
- paths = addCompletionRecords(this.get("block"), paths);
127
- paths = addCompletionRecords(this.get("handler"), paths);
128
- } else if (this.isCatchClause()) {
129
- paths = addCompletionRecords(this.get("body"), paths);
130
- } else if (this.isSwitchStatement()) {
131
- paths = completionRecordForSwitch(this.get("cases"), paths);
173
+ function _getCompletionRecords(path, context) {
174
+ let records = [];
175
+
176
+ if (path.isIfStatement()) {
177
+ records = addCompletionRecords(path.get("consequent"), records, context);
178
+ records = addCompletionRecords(path.get("alternate"), records, context);
179
+ } else if (path.isDoExpression() || path.isFor() || path.isWhile() || path.isLabeledStatement()) {
180
+ records = addCompletionRecords(path.get("body"), records, context);
181
+ } else if (path.isProgram() || path.isBlockStatement()) {
182
+ records = records.concat(getStatementListCompletion(path.get("body"), context));
183
+ } else if (path.isFunction()) {
184
+ return _getCompletionRecords(path.get("body"), context);
185
+ } else if (path.isTryStatement()) {
186
+ records = addCompletionRecords(path.get("block"), records, context);
187
+ records = addCompletionRecords(path.get("handler"), records, context);
188
+ } else if (path.isCatchClause()) {
189
+ records = addCompletionRecords(path.get("body"), records, context);
190
+ } else if (path.isSwitchStatement()) {
191
+ records = completionRecordForSwitch(path.get("cases"), records, context);
192
+ } else if (path.isSwitchCase()) {
193
+ records = records.concat(getStatementListCompletion(path.get("consequent"), {
194
+ canHaveBreak: true,
195
+ shouldPopulateBreak: false,
196
+ inCaseClause: true
197
+ }));
198
+ } else if (path.isBreakStatement()) {
199
+ records.push(BreakCompletion(path));
132
200
  } else {
133
- paths.push(this);
201
+ records.push(NormalCompletion(path));
134
202
  }
135
203
 
136
- return paths;
204
+ return records;
205
+ }
206
+
207
+ function getCompletionRecords() {
208
+ const records = _getCompletionRecords(this, {
209
+ canHaveBreak: false,
210
+ shouldPopulateBreak: false,
211
+ inCaseClause: false
212
+ });
213
+
214
+ return records.map(r => r.path);
137
215
  }
138
216
 
139
217
  function getSibling(key) {
package/lib/path/index.js CHANGED
@@ -7,8 +7,6 @@ exports.default = exports.SHOULD_SKIP = exports.SHOULD_STOP = exports.REMOVED =
7
7
 
8
8
  var virtualTypes = _interopRequireWildcard(require("./lib/virtual-types"));
9
9
 
10
- var _debug = _interopRequireDefault(require("debug"));
11
-
12
10
  var _index = _interopRequireDefault(require("../index"));
13
11
 
14
12
  var _scope = _interopRequireDefault(require("../scope"));
@@ -47,7 +45,9 @@ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return
47
45
 
48
46
  function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
49
47
 
50
- const debug = (0, _debug.default)("babel");
48
+ const buildDebug = require("debug");
49
+
50
+ const debug = buildDebug("babel");
51
51
  const REMOVED = 1 << 0;
52
52
  exports.REMOVED = REMOVED;
53
53
  const SHOULD_STOP = 1 << 1;
@@ -70,7 +70,7 @@ function _getTypeAnnotation() {
70
70
 
71
71
  inferer = inferers[this.parentPath.type];
72
72
 
73
- if ((_inferer = inferer) == null ? void 0 : _inferer.validParent) {
73
+ if ((_inferer = inferer) != null && _inferer.validParent) {
74
74
  return this.parentPath.getTypeAnnotation();
75
75
  }
76
76
  } finally {
@@ -27,7 +27,7 @@ function remove() {
27
27
 
28
28
  this.resync();
29
29
 
30
- if (!((_this$opts = this.opts) == null ? void 0 : _this$opts.noScope)) {
30
+ if (!((_this$opts = this.opts) != null && _this$opts.noScope)) {
31
31
  this._removeFromScope();
32
32
  }
33
33
 
@@ -194,6 +194,7 @@ function replaceExpressionWithStatements(nodes) {
194
194
 
195
195
  const functionParent = this.getFunctionParent();
196
196
  const isParentAsync = functionParent == null ? void 0 : functionParent.is("async");
197
+ const isParentGenerator = functionParent == null ? void 0 : functionParent.is("generator");
197
198
  const container = t.arrowFunctionExpression([], t.blockStatement(nodes));
198
199
  this.replaceWith(t.callExpression(container, []));
199
200
  this.traverse(hoistVariablesVisitor);
@@ -224,9 +225,21 @@ function replaceExpressionWithStatements(nodes) {
224
225
  const callee = this.get("callee");
225
226
  callee.arrowFunctionToExpression();
226
227
 
227
- if (isParentAsync && _index.default.hasType(this.get("callee.body").node, "AwaitExpression", t.FUNCTION_TYPES)) {
228
+ const needToAwaitFunction = isParentAsync && _index.default.hasType(this.get("callee.body").node, "AwaitExpression", t.FUNCTION_TYPES);
229
+
230
+ const needToYieldFunction = isParentGenerator && _index.default.hasType(this.get("callee.body").node, "YieldExpression", t.FUNCTION_TYPES);
231
+
232
+ if (needToAwaitFunction) {
228
233
  callee.set("async", true);
229
- this.replaceWith(t.awaitExpression(this.node));
234
+
235
+ if (!needToYieldFunction) {
236
+ this.replaceWith(t.awaitExpression(this.node));
237
+ }
238
+ }
239
+
240
+ if (needToYieldFunction) {
241
+ callee.set("generator", true);
242
+ this.replaceWith(t.yieldExpression(this.node, true));
230
243
  }
231
244
 
232
245
  return callee.get("body.body");
@@ -11,8 +11,6 @@ var _index = _interopRequireDefault(require("../index"));
11
11
 
12
12
  var _binding = _interopRequireDefault(require("./binding"));
13
13
 
14
- var _globals = _interopRequireDefault(require("globals"));
15
-
16
14
  var t = _interopRequireWildcard(require("@babel/types"));
17
15
 
18
16
  var _cache = require("../cache");
@@ -23,6 +21,8 @@ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj;
23
21
 
24
22
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
23
 
24
+ const globals = require("globals");
25
+
26
26
  function gatherNodeParts(node, parts) {
27
27
  switch (node == null ? void 0 : node.type) {
28
28
  default:
@@ -470,7 +470,7 @@ class Scope {
470
470
  if (t.isIdentifier(node)) {
471
471
  const binding = this.getBinding(node.name);
472
472
 
473
- if ((binding == null ? void 0 : binding.constant) && binding.path.isGenericType("Array")) {
473
+ if (binding != null && binding.constant && binding.path.isGenericType("Array")) {
474
474
  return node;
475
475
  }
476
476
  }
@@ -891,7 +891,7 @@ class Scope {
891
891
  if (binding) {
892
892
  var _previousPath;
893
893
 
894
- if (((_previousPath = previousPath) == null ? void 0 : _previousPath.isPattern()) && binding.kind !== "param") {} else {
894
+ if ((_previousPath = previousPath) != null && _previousPath.isPattern() && binding.kind !== "param") {} else {
895
895
  return binding;
896
896
  }
897
897
  }
@@ -965,5 +965,5 @@ class Scope {
965
965
  }
966
966
 
967
967
  exports.default = Scope;
968
- Scope.globals = Object.keys(_globals.default.builtin);
968
+ Scope.globals = Object.keys(globals.builtin);
969
969
  Scope.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@babel/traverse",
3
- "version": "7.13.0",
3
+ "version": "7.14.0",
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://babel.dev/docs/en/next/babel-traverse",
@@ -17,16 +17,15 @@
17
17
  "main": "lib/index.js",
18
18
  "dependencies": {
19
19
  "@babel/code-frame": "^7.12.13",
20
- "@babel/generator": "^7.13.0",
20
+ "@babel/generator": "^7.14.0",
21
21
  "@babel/helper-function-name": "^7.12.13",
22
22
  "@babel/helper-split-export-declaration": "^7.12.13",
23
- "@babel/parser": "^7.13.0",
24
- "@babel/types": "^7.13.0",
23
+ "@babel/parser": "^7.14.0",
24
+ "@babel/types": "^7.14.0",
25
25
  "debug": "^4.1.0",
26
- "globals": "^11.1.0",
27
- "lodash": "^4.17.19"
26
+ "globals": "^11.1.0"
28
27
  },
29
28
  "devDependencies": {
30
- "@babel/helper-plugin-test-runner": "7.12.13"
29
+ "@babel/helper-plugin-test-runner": "7.13.10"
31
30
  }
32
31
  }