@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.
- package/lib/path/family.js +145 -67
- package/lib/path/replacement.js +15 -2
- package/package.json +3 -3
package/lib/path/family.js
CHANGED
@@ -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,
|
42
|
-
if (path) return
|
43
|
-
return
|
58
|
+
function addCompletionRecords(path, records, context) {
|
59
|
+
if (path) return records.concat(_getCompletionRecords(path, context));
|
60
|
+
return records;
|
44
61
|
}
|
45
62
|
|
46
|
-
function
|
47
|
-
let
|
63
|
+
function completionRecordForSwitch(cases, records, context) {
|
64
|
+
let lastNormalCompletions = [];
|
48
65
|
|
49
|
-
|
50
|
-
|
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
|
-
|
54
|
-
|
55
|
-
breakStatement = findBreak(statement.get("body"));
|
56
|
-
} else if (statement.isIfStatement()) {
|
57
|
-
var _findBreak;
|
71
|
+
const normalCompletions = [];
|
72
|
+
const breakCompletions = [];
|
58
73
|
|
59
|
-
|
60
|
-
|
61
|
-
|
74
|
+
for (const c of caseCompletions) {
|
75
|
+
if (c.type === NORMAL_COMPLETION) {
|
76
|
+
normalCompletions.push(c);
|
77
|
+
}
|
62
78
|
|
63
|
-
|
64
|
-
|
65
|
-
|
79
|
+
if (c.type === BREAK_COMPLETION) {
|
80
|
+
breakCompletions.push(c);
|
81
|
+
}
|
66
82
|
}
|
67
83
|
|
68
|
-
if (
|
69
|
-
|
84
|
+
if (normalCompletions.length) {
|
85
|
+
lastNormalCompletions = normalCompletions;
|
70
86
|
}
|
87
|
+
|
88
|
+
records = records.concat(breakCompletions);
|
71
89
|
}
|
72
90
|
|
73
|
-
|
91
|
+
records = records.concat(lastNormalCompletions);
|
92
|
+
return records;
|
74
93
|
}
|
75
94
|
|
76
|
-
function
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
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
|
-
|
85
|
-
|
86
|
-
|
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
|
-
|
115
|
+
function getStatementListCompletion(paths, context) {
|
116
|
+
let completions = [];
|
90
117
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
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
|
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
|
-
|
104
|
-
|
105
|
-
|
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
|
170
|
+
return completions;
|
111
171
|
}
|
112
172
|
|
113
|
-
function
|
114
|
-
let
|
115
|
-
|
116
|
-
if (
|
117
|
-
|
118
|
-
|
119
|
-
} else if (
|
120
|
-
|
121
|
-
} else if (
|
122
|
-
|
123
|
-
} else if (
|
124
|
-
return
|
125
|
-
} else if (
|
126
|
-
|
127
|
-
|
128
|
-
} else if (
|
129
|
-
|
130
|
-
} else if (
|
131
|
-
|
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
|
-
|
201
|
+
records.push(NormalCompletion(path));
|
134
202
|
}
|
135
203
|
|
136
|
-
return
|
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/replacement.js
CHANGED
@@ -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
|
-
|
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
|
-
|
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.
|
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.
|
24
|
-
"@babel/types": "^7.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
|
},
|