@babel/traverse 7.13.13 → 7.13.15

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.

@@ -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()) {
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) {
@@ -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");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@babel/traverse",
3
- "version": "7.13.13",
3
+ "version": "7.13.15",
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",
@@ -20,8 +20,8 @@
20
20
  "@babel/generator": "^7.13.9",
21
21
  "@babel/helper-function-name": "^7.12.13",
22
22
  "@babel/helper-split-export-declaration": "^7.12.13",
23
- "@babel/parser": "^7.13.13",
24
- "@babel/types": "^7.13.13",
23
+ "@babel/parser": "^7.13.15",
24
+ "@babel/types": "^7.13.14",
25
25
  "debug": "^4.1.0",
26
26
  "globals": "^11.1.0"
27
27
  },