@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 +1 -1
- package/lib/index.js +1 -1
- package/lib/path/evaluation.js +1 -1
- package/lib/path/family.js +145 -67
- package/lib/path/index.js +3 -3
- package/lib/path/inference/index.js +1 -1
- package/lib/path/removal.js +1 -1
- package/lib/path/replacement.js +15 -2
- package/lib/scope/index.js +5 -5
- package/package.json +6 -7
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
|
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
|
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,
|
package/lib/path/evaluation.js
CHANGED
@@ -129,7 +129,7 @@ function _evaluate(path, state) {
|
|
129
129
|
return deopt(binding.path, state);
|
130
130
|
}
|
131
131
|
|
132
|
-
if (binding
|
132
|
+
if (binding != null && binding.hasValue) {
|
133
133
|
return binding.value;
|
134
134
|
} else {
|
135
135
|
if (path.node.name === "undefined") {
|
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() || 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
|
-
|
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/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
|
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)
|
73
|
+
if ((_inferer = inferer) != null && _inferer.validParent) {
|
74
74
|
return this.parentPath.getTypeAnnotation();
|
75
75
|
}
|
76
76
|
} finally {
|
package/lib/path/removal.js
CHANGED
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/lib/scope/index.js
CHANGED
@@ -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 (
|
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 ((
|
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(
|
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.
|
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.
|
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.
|
24
|
-
"@babel/types": "^7.
|
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.
|
29
|
+
"@babel/helper-plugin-test-runner": "7.13.10"
|
31
30
|
}
|
32
31
|
}
|