@babel/traverse 7.15.0 → 7.16.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.
- package/lib/context.js +6 -2
- package/lib/index.js +18 -12
- package/lib/path/ancestry.js +11 -7
- package/lib/path/comments.js +9 -4
- package/lib/path/context.js +26 -14
- package/lib/path/conversion.js +206 -142
- package/lib/path/evaluation.js +1 -1
- package/lib/path/family.js +61 -38
- package/lib/path/index.js +9 -3
- package/lib/path/inference/index.js +39 -21
- package/lib/path/inference/inferer-reference.js +28 -17
- package/lib/path/inference/inferers.js +89 -65
- package/lib/path/introspection.js +37 -25
- package/lib/path/lib/hoister.js +17 -7
- package/lib/path/lib/virtual-types.js +47 -23
- package/lib/path/modification.js +25 -14
- package/lib/path/removal.js +4 -4
- package/lib/path/replacement.js +50 -27
- package/lib/scope/index.js +104 -55
- package/lib/scope/lib/renamer.js +16 -8
- package/lib/types.js +0 -2
- package/lib/visitors.js +11 -5
- package/package.json +10 -9
package/lib/path/modification.js
CHANGED
@@ -3,16 +3,16 @@
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
4
4
|
value: true
|
5
5
|
});
|
6
|
-
exports.insertBefore = insertBefore;
|
7
6
|
exports._containerInsert = _containerInsert;
|
8
|
-
exports._containerInsertBefore = _containerInsertBefore;
|
9
7
|
exports._containerInsertAfter = _containerInsertAfter;
|
10
|
-
exports.
|
11
|
-
exports.updateSiblingKeys = updateSiblingKeys;
|
8
|
+
exports._containerInsertBefore = _containerInsertBefore;
|
12
9
|
exports._verifyNodeList = _verifyNodeList;
|
13
|
-
exports.unshiftContainer = unshiftContainer;
|
14
|
-
exports.pushContainer = pushContainer;
|
15
10
|
exports.hoist = hoist;
|
11
|
+
exports.insertAfter = insertAfter;
|
12
|
+
exports.insertBefore = insertBefore;
|
13
|
+
exports.pushContainer = pushContainer;
|
14
|
+
exports.unshiftContainer = unshiftContainer;
|
15
|
+
exports.updateSiblingKeys = updateSiblingKeys;
|
16
16
|
|
17
17
|
var _cache = require("../cache");
|
18
18
|
|
@@ -20,7 +20,18 @@ var _hoister = require("./lib/hoister");
|
|
20
20
|
|
21
21
|
var _index = require("./index");
|
22
22
|
|
23
|
-
var
|
23
|
+
var _t = require("@babel/types");
|
24
|
+
|
25
|
+
const {
|
26
|
+
arrowFunctionExpression,
|
27
|
+
assertExpression,
|
28
|
+
assignmentExpression,
|
29
|
+
blockStatement,
|
30
|
+
callExpression,
|
31
|
+
cloneNode,
|
32
|
+
expressionStatement,
|
33
|
+
isExpression
|
34
|
+
} = _t;
|
24
35
|
|
25
36
|
function insertBefore(nodes_) {
|
26
37
|
this._assertUnremoved();
|
@@ -41,7 +52,7 @@ function insertBefore(nodes_) {
|
|
41
52
|
} else if (this.isStatementOrBlock()) {
|
42
53
|
const node = this.node;
|
43
54
|
const shouldInsertCurrentNode = node && (!this.isExpressionStatement() || node.expression != null);
|
44
|
-
this.replaceWith(
|
55
|
+
this.replaceWith(blockStatement(shouldInsertCurrentNode ? [node] : []));
|
45
56
|
return this.unshiftContainer("body", nodes);
|
46
57
|
} else {
|
47
58
|
throw new Error("We don't know what to do with this node type. " + "We were previously a Statement but we can't fit in here?");
|
@@ -96,7 +107,7 @@ function insertAfter(nodes_) {
|
|
96
107
|
|
97
108
|
if (parentPath.isExpressionStatement() || parentPath.isLabeledStatement() || parentPath.isExportNamedDeclaration() || parentPath.isExportDefaultDeclaration() && this.isDeclaration()) {
|
98
109
|
return parentPath.insertAfter(nodes.map(node => {
|
99
|
-
return
|
110
|
+
return isExpression(node) ? expressionStatement(node) : node;
|
100
111
|
}));
|
101
112
|
} else if (this.isNodeType("Expression") && !this.isJSXElement() && !parentPath.isJSXElement() || parentPath.isForStatement() && this.key === "init") {
|
102
113
|
if (this.node) {
|
@@ -106,8 +117,8 @@ function insertAfter(nodes_) {
|
|
106
117
|
} = this;
|
107
118
|
|
108
119
|
if (scope.path.isPattern()) {
|
109
|
-
|
110
|
-
this.replaceWith(
|
120
|
+
assertExpression(node);
|
121
|
+
this.replaceWith(callExpression(arrowFunctionExpression([], node), []));
|
111
122
|
this.get("callee.body").insertAfter(nodes);
|
112
123
|
return [this];
|
113
124
|
}
|
@@ -120,8 +131,8 @@ function insertAfter(nodes_) {
|
|
120
131
|
}
|
121
132
|
|
122
133
|
const temp = scope.generateDeclaredUidIdentifier();
|
123
|
-
nodes.unshift(
|
124
|
-
nodes.push(
|
134
|
+
nodes.unshift(expressionStatement(assignmentExpression("=", cloneNode(temp), node)));
|
135
|
+
nodes.push(expressionStatement(cloneNode(temp)));
|
125
136
|
}
|
126
137
|
|
127
138
|
return this.replaceExpressionWithStatements(nodes);
|
@@ -130,7 +141,7 @@ function insertAfter(nodes_) {
|
|
130
141
|
} else if (this.isStatementOrBlock()) {
|
131
142
|
const node = this.node;
|
132
143
|
const shouldInsertCurrentNode = node && (!this.isExpressionStatement() || node.expression != null);
|
133
|
-
this.replaceWith(
|
144
|
+
this.replaceWith(blockStatement(shouldInsertCurrentNode ? [node] : []));
|
134
145
|
return this.pushContainer("body", nodes);
|
135
146
|
} else {
|
136
147
|
throw new Error("We don't know what to do with this node type. " + "We were previously a Statement but we can't fit in here?");
|
package/lib/path/removal.js
CHANGED
@@ -3,12 +3,12 @@
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
4
4
|
value: true
|
5
5
|
});
|
6
|
-
exports.
|
7
|
-
exports._removeFromScope = _removeFromScope;
|
6
|
+
exports._assertUnremoved = _assertUnremoved;
|
8
7
|
exports._callRemovalHooks = _callRemovalHooks;
|
9
|
-
exports._remove = _remove;
|
10
8
|
exports._markRemoved = _markRemoved;
|
11
|
-
exports.
|
9
|
+
exports._remove = _remove;
|
10
|
+
exports._removeFromScope = _removeFromScope;
|
11
|
+
exports.remove = remove;
|
12
12
|
|
13
13
|
var _removalHooks = require("./lib/removal-hooks");
|
14
14
|
|
package/lib/path/replacement.js
CHANGED
@@ -3,12 +3,12 @@
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
4
4
|
value: true
|
5
5
|
});
|
6
|
-
exports.replaceWithMultiple = replaceWithMultiple;
|
7
|
-
exports.replaceWithSourceString = replaceWithSourceString;
|
8
|
-
exports.replaceWith = replaceWith;
|
9
6
|
exports._replaceWith = _replaceWith;
|
10
7
|
exports.replaceExpressionWithStatements = replaceExpressionWithStatements;
|
11
8
|
exports.replaceInline = replaceInline;
|
9
|
+
exports.replaceWith = replaceWith;
|
10
|
+
exports.replaceWithMultiple = replaceWithMultiple;
|
11
|
+
exports.replaceWithSourceString = replaceWithSourceString;
|
12
12
|
|
13
13
|
var _codeFrame = require("@babel/code-frame");
|
14
14
|
|
@@ -20,17 +20,40 @@ var _cache = require("../cache");
|
|
20
20
|
|
21
21
|
var _parser = require("@babel/parser");
|
22
22
|
|
23
|
-
var
|
23
|
+
var _t = require("@babel/types");
|
24
24
|
|
25
25
|
var _helperHoistVariables = require("@babel/helper-hoist-variables");
|
26
26
|
|
27
|
+
const {
|
28
|
+
FUNCTION_TYPES,
|
29
|
+
arrowFunctionExpression,
|
30
|
+
assignmentExpression,
|
31
|
+
awaitExpression,
|
32
|
+
blockStatement,
|
33
|
+
callExpression,
|
34
|
+
cloneNode,
|
35
|
+
expressionStatement,
|
36
|
+
identifier,
|
37
|
+
inheritLeadingComments,
|
38
|
+
inheritTrailingComments,
|
39
|
+
inheritsComments,
|
40
|
+
isExpression,
|
41
|
+
isProgram,
|
42
|
+
isStatement,
|
43
|
+
removeComments,
|
44
|
+
returnStatement,
|
45
|
+
toSequenceExpression,
|
46
|
+
validate,
|
47
|
+
yieldExpression
|
48
|
+
} = _t;
|
49
|
+
|
27
50
|
function replaceWithMultiple(nodes) {
|
28
51
|
var _pathCache$get;
|
29
52
|
|
30
53
|
this.resync();
|
31
54
|
nodes = this._verifyNodeList(nodes);
|
32
|
-
|
33
|
-
|
55
|
+
inheritLeadingComments(nodes[0], this.node);
|
56
|
+
inheritTrailingComments(nodes[nodes.length - 1], this.node);
|
34
57
|
(_pathCache$get = _cache.path.get(this.parent)) == null ? void 0 : _pathCache$get.delete(this.node);
|
35
58
|
this.node = this.container[this.key] = null;
|
36
59
|
const paths = this.insertAfter(nodes);
|
@@ -92,7 +115,7 @@ function replaceWith(replacement) {
|
|
92
115
|
return [this];
|
93
116
|
}
|
94
117
|
|
95
|
-
if (this.isProgram() && !
|
118
|
+
if (this.isProgram() && !isProgram(replacement)) {
|
96
119
|
throw new Error("You can only replace a Program root node with another Program node");
|
97
120
|
}
|
98
121
|
|
@@ -106,14 +129,14 @@ function replaceWith(replacement) {
|
|
106
129
|
|
107
130
|
let nodePath = "";
|
108
131
|
|
109
|
-
if (this.isNodeType("Statement") &&
|
132
|
+
if (this.isNodeType("Statement") && isExpression(replacement)) {
|
110
133
|
if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement) && !this.parentPath.isExportDefaultDeclaration()) {
|
111
|
-
replacement =
|
134
|
+
replacement = expressionStatement(replacement);
|
112
135
|
nodePath = "expression";
|
113
136
|
}
|
114
137
|
}
|
115
138
|
|
116
|
-
if (this.isNodeType("Expression") &&
|
139
|
+
if (this.isNodeType("Expression") && isStatement(replacement)) {
|
117
140
|
if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement)) {
|
118
141
|
return this.replaceExpressionWithStatements([replacement]);
|
119
142
|
}
|
@@ -122,8 +145,8 @@ function replaceWith(replacement) {
|
|
122
145
|
const oldNode = this.node;
|
123
146
|
|
124
147
|
if (oldNode) {
|
125
|
-
|
126
|
-
|
148
|
+
inheritsComments(replacement, oldNode);
|
149
|
+
removeComments(oldNode);
|
127
150
|
}
|
128
151
|
|
129
152
|
this._replaceWith(replacement);
|
@@ -142,9 +165,9 @@ function _replaceWith(node) {
|
|
142
165
|
}
|
143
166
|
|
144
167
|
if (this.inList) {
|
145
|
-
|
168
|
+
validate(this.parent, this.key, [node]);
|
146
169
|
} else {
|
147
|
-
|
170
|
+
validate(this.parent, this.key, node);
|
148
171
|
}
|
149
172
|
|
150
173
|
this.debug(`Replace with ${node == null ? void 0 : node.type}`);
|
@@ -154,17 +177,17 @@ function _replaceWith(node) {
|
|
154
177
|
|
155
178
|
function replaceExpressionWithStatements(nodes) {
|
156
179
|
this.resync();
|
157
|
-
const
|
180
|
+
const nodesAsSequenceExpression = toSequenceExpression(nodes, this.scope);
|
158
181
|
|
159
|
-
if (
|
160
|
-
return this.replaceWith(
|
182
|
+
if (nodesAsSequenceExpression) {
|
183
|
+
return this.replaceWith(nodesAsSequenceExpression)[0].get("expressions");
|
161
184
|
}
|
162
185
|
|
163
186
|
const functionParent = this.getFunctionParent();
|
164
187
|
const isParentAsync = functionParent == null ? void 0 : functionParent.is("async");
|
165
188
|
const isParentGenerator = functionParent == null ? void 0 : functionParent.is("generator");
|
166
|
-
const container =
|
167
|
-
this.replaceWith(
|
189
|
+
const container = arrowFunctionExpression([], blockStatement(nodes));
|
190
|
+
this.replaceWith(callExpression(container, []));
|
168
191
|
const callee = this.get("callee");
|
169
192
|
(0, _helperHoistVariables.default)(callee.get("body"), id => {
|
170
193
|
this.scope.push({
|
@@ -182,36 +205,36 @@ function replaceExpressionWithStatements(nodes) {
|
|
182
205
|
|
183
206
|
if (!uid) {
|
184
207
|
uid = callee.scope.generateDeclaredUidIdentifier("ret");
|
185
|
-
callee.get("body").pushContainer("body",
|
208
|
+
callee.get("body").pushContainer("body", returnStatement(cloneNode(uid)));
|
186
209
|
loop.setData("expressionReplacementReturnUid", uid);
|
187
210
|
} else {
|
188
|
-
uid =
|
211
|
+
uid = identifier(uid.name);
|
189
212
|
}
|
190
213
|
|
191
|
-
path.get("expression").replaceWith(
|
214
|
+
path.get("expression").replaceWith(assignmentExpression("=", cloneNode(uid), path.node.expression));
|
192
215
|
} else {
|
193
|
-
path.replaceWith(
|
216
|
+
path.replaceWith(returnStatement(path.node.expression));
|
194
217
|
}
|
195
218
|
}
|
196
219
|
|
197
220
|
callee.arrowFunctionToExpression();
|
198
221
|
const newCallee = callee;
|
199
222
|
|
200
|
-
const needToAwaitFunction = isParentAsync && _index.default.hasType(this.get("callee.body").node, "AwaitExpression",
|
223
|
+
const needToAwaitFunction = isParentAsync && _index.default.hasType(this.get("callee.body").node, "AwaitExpression", FUNCTION_TYPES);
|
201
224
|
|
202
|
-
const needToYieldFunction = isParentGenerator && _index.default.hasType(this.get("callee.body").node, "YieldExpression",
|
225
|
+
const needToYieldFunction = isParentGenerator && _index.default.hasType(this.get("callee.body").node, "YieldExpression", FUNCTION_TYPES);
|
203
226
|
|
204
227
|
if (needToAwaitFunction) {
|
205
228
|
newCallee.set("async", true);
|
206
229
|
|
207
230
|
if (!needToYieldFunction) {
|
208
|
-
this.replaceWith(
|
231
|
+
this.replaceWith(awaitExpression(this.node));
|
209
232
|
}
|
210
233
|
}
|
211
234
|
|
212
235
|
if (needToYieldFunction) {
|
213
236
|
newCallee.set("generator", true);
|
214
|
-
this.replaceWith(
|
237
|
+
this.replaceWith(yieldExpression(this.node, true));
|
215
238
|
}
|
216
239
|
|
217
240
|
return newCallee.get("body.body");
|
package/lib/scope/index.js
CHANGED
@@ -13,24 +13,63 @@ var _binding = require("./binding");
|
|
13
13
|
|
14
14
|
var _globals = require("globals");
|
15
15
|
|
16
|
-
var
|
16
|
+
var _t = require("@babel/types");
|
17
17
|
|
18
18
|
var _cache = require("../cache");
|
19
19
|
|
20
|
+
const {
|
21
|
+
NOT_LOCAL_BINDING,
|
22
|
+
callExpression,
|
23
|
+
cloneNode,
|
24
|
+
getBindingIdentifiers,
|
25
|
+
identifier,
|
26
|
+
isArrayExpression,
|
27
|
+
isBinary,
|
28
|
+
isClass,
|
29
|
+
isClassBody,
|
30
|
+
isClassDeclaration,
|
31
|
+
isExportAllDeclaration,
|
32
|
+
isExportDefaultDeclaration,
|
33
|
+
isExportNamedDeclaration,
|
34
|
+
isFunctionDeclaration,
|
35
|
+
isIdentifier,
|
36
|
+
isImportDeclaration,
|
37
|
+
isLiteral,
|
38
|
+
isMethod,
|
39
|
+
isModuleDeclaration,
|
40
|
+
isModuleSpecifier,
|
41
|
+
isObjectExpression,
|
42
|
+
isProperty,
|
43
|
+
isPureish,
|
44
|
+
isSuper,
|
45
|
+
isTaggedTemplateExpression,
|
46
|
+
isTemplateLiteral,
|
47
|
+
isThisExpression,
|
48
|
+
isUnaryExpression,
|
49
|
+
isVariableDeclaration,
|
50
|
+
matchesPattern,
|
51
|
+
memberExpression,
|
52
|
+
numericLiteral,
|
53
|
+
toIdentifier,
|
54
|
+
unaryExpression,
|
55
|
+
variableDeclaration,
|
56
|
+
variableDeclarator
|
57
|
+
} = _t;
|
58
|
+
|
20
59
|
function gatherNodeParts(node, parts) {
|
21
60
|
switch (node == null ? void 0 : node.type) {
|
22
61
|
default:
|
23
|
-
if (
|
24
|
-
if ((
|
62
|
+
if (isModuleDeclaration(node)) {
|
63
|
+
if ((isExportAllDeclaration(node) || isExportNamedDeclaration(node) || isImportDeclaration(node)) && node.source) {
|
25
64
|
gatherNodeParts(node.source, parts);
|
26
|
-
} else if ((
|
65
|
+
} else if ((isExportNamedDeclaration(node) || isImportDeclaration(node)) && node.specifiers && node.specifiers.length) {
|
27
66
|
for (const e of node.specifiers) gatherNodeParts(e, parts);
|
28
|
-
} else if ((
|
67
|
+
} else if ((isExportDefaultDeclaration(node) || isExportNamedDeclaration(node)) && node.declaration) {
|
29
68
|
gatherNodeParts(node.declaration, parts);
|
30
69
|
}
|
31
|
-
} else if (
|
70
|
+
} else if (isModuleSpecifier(node)) {
|
32
71
|
gatherNodeParts(node.local, parts);
|
33
|
-
} else if (
|
72
|
+
} else if (isLiteral(node)) {
|
34
73
|
parts.push(node.value);
|
35
74
|
}
|
36
75
|
|
@@ -159,14 +198,15 @@ function gatherNodeParts(node, parts) {
|
|
159
198
|
}
|
160
199
|
|
161
200
|
const collectorVisitor = {
|
162
|
-
|
163
|
-
|
164
|
-
const declar = path.get(key);
|
201
|
+
ForStatement(path) {
|
202
|
+
const declar = path.get("init");
|
165
203
|
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
}
|
204
|
+
if (declar.isVar()) {
|
205
|
+
const {
|
206
|
+
scope
|
207
|
+
} = path;
|
208
|
+
const parentScope = scope.getFunctionParent() || scope.getProgramParent();
|
209
|
+
parentScope.registerBinding("var", declar);
|
170
210
|
}
|
171
211
|
},
|
172
212
|
|
@@ -192,6 +232,12 @@ const collectorVisitor = {
|
|
192
232
|
|
193
233
|
if (left.isPattern() || left.isIdentifier()) {
|
194
234
|
state.constantViolations.push(path);
|
235
|
+
} else if (left.isVar()) {
|
236
|
+
const {
|
237
|
+
scope
|
238
|
+
} = path;
|
239
|
+
const parentScope = scope.getFunctionParent() || scope.getProgramParent();
|
240
|
+
parentScope.registerBinding("var", left);
|
195
241
|
}
|
196
242
|
},
|
197
243
|
|
@@ -201,19 +247,19 @@ const collectorVisitor = {
|
|
201
247
|
node,
|
202
248
|
scope
|
203
249
|
} = path;
|
204
|
-
if (
|
250
|
+
if (isExportAllDeclaration(node)) return;
|
205
251
|
const declar = node.declaration;
|
206
252
|
|
207
|
-
if (
|
253
|
+
if (isClassDeclaration(declar) || isFunctionDeclaration(declar)) {
|
208
254
|
const id = declar.id;
|
209
255
|
if (!id) return;
|
210
256
|
const binding = scope.getBinding(id.name);
|
211
|
-
|
212
|
-
} else if (
|
257
|
+
binding == null ? void 0 : binding.reference(path);
|
258
|
+
} else if (isVariableDeclaration(declar)) {
|
213
259
|
for (const decl of declar.declarations) {
|
214
|
-
for (const name of Object.keys(
|
260
|
+
for (const name of Object.keys(getBindingIdentifiers(decl))) {
|
215
261
|
const binding = scope.getBinding(name);
|
216
|
-
|
262
|
+
binding == null ? void 0 : binding.reference(path);
|
217
263
|
}
|
218
264
|
}
|
219
265
|
}
|
@@ -257,19 +303,19 @@ const collectorVisitor = {
|
|
257
303
|
},
|
258
304
|
|
259
305
|
Function(path) {
|
260
|
-
if (path.isFunctionExpression() && path.has("id") && !path.get("id").node[t.NOT_LOCAL_BINDING]) {
|
261
|
-
path.scope.registerBinding("local", path.get("id"), path);
|
262
|
-
}
|
263
|
-
|
264
306
|
const params = path.get("params");
|
265
307
|
|
266
308
|
for (const param of params) {
|
267
309
|
path.scope.registerBinding("param", param);
|
268
310
|
}
|
311
|
+
|
312
|
+
if (path.isFunctionExpression() && path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
|
313
|
+
path.scope.registerBinding("local", path.get("id"), path);
|
314
|
+
}
|
269
315
|
},
|
270
316
|
|
271
317
|
ClassExpression(path) {
|
272
|
-
if (path.has("id") && !path.get("id").node[
|
318
|
+
if (path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
|
273
319
|
path.scope.registerBinding("local", path);
|
274
320
|
}
|
275
321
|
}
|
@@ -342,15 +388,15 @@ class Scope {
|
|
342
388
|
this.push({
|
343
389
|
id
|
344
390
|
});
|
345
|
-
return
|
391
|
+
return cloneNode(id);
|
346
392
|
}
|
347
393
|
|
348
394
|
generateUidIdentifier(name) {
|
349
|
-
return
|
395
|
+
return identifier(this.generateUid(name));
|
350
396
|
}
|
351
397
|
|
352
398
|
generateUid(name = "temp") {
|
353
|
-
name =
|
399
|
+
name = toIdentifier(name).replace(/^_+/, "").replace(/[0-9]+$/g, "");
|
354
400
|
let uid;
|
355
401
|
let i = 1;
|
356
402
|
|
@@ -380,15 +426,15 @@ class Scope {
|
|
380
426
|
}
|
381
427
|
|
382
428
|
generateUidIdentifierBasedOnNode(node, defaultName) {
|
383
|
-
return
|
429
|
+
return identifier(this.generateUidBasedOnNode(node, defaultName));
|
384
430
|
}
|
385
431
|
|
386
432
|
isStatic(node) {
|
387
|
-
if (
|
433
|
+
if (isThisExpression(node) || isSuper(node)) {
|
388
434
|
return true;
|
389
435
|
}
|
390
436
|
|
391
|
-
if (
|
437
|
+
if (isIdentifier(node)) {
|
392
438
|
const binding = this.getBinding(node.name);
|
393
439
|
|
394
440
|
if (binding) {
|
@@ -411,7 +457,7 @@ class Scope {
|
|
411
457
|
this.push({
|
412
458
|
id
|
413
459
|
});
|
414
|
-
return
|
460
|
+
return cloneNode(id);
|
415
461
|
}
|
416
462
|
|
417
463
|
return id;
|
@@ -467,7 +513,7 @@ class Scope {
|
|
467
513
|
}
|
468
514
|
|
469
515
|
toArray(node, i, arrayLikeIsIterable) {
|
470
|
-
if (
|
516
|
+
if (isIdentifier(node)) {
|
471
517
|
const binding = this.getBinding(node.name);
|
472
518
|
|
473
519
|
if (binding != null && binding.constant && binding.path.isGenericType("Array")) {
|
@@ -475,14 +521,14 @@ class Scope {
|
|
475
521
|
}
|
476
522
|
}
|
477
523
|
|
478
|
-
if (
|
524
|
+
if (isArrayExpression(node)) {
|
479
525
|
return node;
|
480
526
|
}
|
481
527
|
|
482
|
-
if (
|
528
|
+
if (isIdentifier(node, {
|
483
529
|
name: "arguments"
|
484
530
|
})) {
|
485
|
-
return
|
531
|
+
return callExpression(memberExpression(memberExpression(memberExpression(identifier("Array"), identifier("prototype")), identifier("slice")), identifier("call")), [node]);
|
486
532
|
}
|
487
533
|
|
488
534
|
let helperName;
|
@@ -491,7 +537,7 @@ class Scope {
|
|
491
537
|
if (i === true) {
|
492
538
|
helperName = "toConsumableArray";
|
493
539
|
} else if (i) {
|
494
|
-
args.push(
|
540
|
+
args.push(numericLiteral(i));
|
495
541
|
helperName = "slicedToArray";
|
496
542
|
} else {
|
497
543
|
helperName = "toArray";
|
@@ -502,7 +548,7 @@ class Scope {
|
|
502
548
|
helperName = "maybeArrayLike";
|
503
549
|
}
|
504
550
|
|
505
|
-
return
|
551
|
+
return callExpression(this.hub.addHelper(helperName), args);
|
506
552
|
}
|
507
553
|
|
508
554
|
hasLabel(name) {
|
@@ -529,6 +575,7 @@ class Scope {
|
|
529
575
|
this.registerBinding(path.node.kind, declar);
|
530
576
|
}
|
531
577
|
} else if (path.isClassDeclaration()) {
|
578
|
+
if (path.node.declare) return;
|
532
579
|
this.registerBinding("let", path);
|
533
580
|
} else if (path.isImportDeclaration()) {
|
534
581
|
const specifiers = path.get("specifiers");
|
@@ -548,7 +595,7 @@ class Scope {
|
|
548
595
|
}
|
549
596
|
|
550
597
|
buildUndefinedNode() {
|
551
|
-
return
|
598
|
+
return unaryExpression("void", numericLiteral(0), true);
|
552
599
|
}
|
553
600
|
|
554
601
|
registerConstantViolation(path) {
|
@@ -630,56 +677,56 @@ class Scope {
|
|
630
677
|
}
|
631
678
|
|
632
679
|
isPure(node, constantsOnly) {
|
633
|
-
if (
|
680
|
+
if (isIdentifier(node)) {
|
634
681
|
const binding = this.getBinding(node.name);
|
635
682
|
if (!binding) return false;
|
636
683
|
if (constantsOnly) return binding.constant;
|
637
684
|
return true;
|
638
|
-
} else if (
|
685
|
+
} else if (isClass(node)) {
|
639
686
|
if (node.superClass && !this.isPure(node.superClass, constantsOnly)) {
|
640
687
|
return false;
|
641
688
|
}
|
642
689
|
|
643
690
|
return this.isPure(node.body, constantsOnly);
|
644
|
-
} else if (
|
691
|
+
} else if (isClassBody(node)) {
|
645
692
|
for (const method of node.body) {
|
646
693
|
if (!this.isPure(method, constantsOnly)) return false;
|
647
694
|
}
|
648
695
|
|
649
696
|
return true;
|
650
|
-
} else if (
|
697
|
+
} else if (isBinary(node)) {
|
651
698
|
return this.isPure(node.left, constantsOnly) && this.isPure(node.right, constantsOnly);
|
652
|
-
} else if (
|
699
|
+
} else if (isArrayExpression(node)) {
|
653
700
|
for (const elem of node.elements) {
|
654
701
|
if (!this.isPure(elem, constantsOnly)) return false;
|
655
702
|
}
|
656
703
|
|
657
704
|
return true;
|
658
|
-
} else if (
|
705
|
+
} else if (isObjectExpression(node)) {
|
659
706
|
for (const prop of node.properties) {
|
660
707
|
if (!this.isPure(prop, constantsOnly)) return false;
|
661
708
|
}
|
662
709
|
|
663
710
|
return true;
|
664
|
-
} else if (
|
711
|
+
} else if (isMethod(node)) {
|
665
712
|
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
|
666
713
|
if (node.kind === "get" || node.kind === "set") return false;
|
667
714
|
return true;
|
668
|
-
} else if (
|
715
|
+
} else if (isProperty(node)) {
|
669
716
|
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
|
670
717
|
return this.isPure(node.value, constantsOnly);
|
671
|
-
} else if (
|
718
|
+
} else if (isUnaryExpression(node)) {
|
672
719
|
return this.isPure(node.argument, constantsOnly);
|
673
|
-
} else if (
|
674
|
-
return
|
675
|
-
} else if (
|
720
|
+
} else if (isTaggedTemplateExpression(node)) {
|
721
|
+
return matchesPattern(node.tag, "String.raw") && !this.hasBinding("String", true) && this.isPure(node.quasi, constantsOnly);
|
722
|
+
} else if (isTemplateLiteral(node)) {
|
676
723
|
for (const expression of node.expressions) {
|
677
724
|
if (!this.isPure(expression, constantsOnly)) return false;
|
678
725
|
}
|
679
726
|
|
680
727
|
return true;
|
681
728
|
} else {
|
682
|
-
return
|
729
|
+
return isPureish(node);
|
683
730
|
}
|
684
731
|
}
|
685
732
|
|
@@ -794,13 +841,13 @@ class Scope {
|
|
794
841
|
let declarPath = !unique && path.getData(dataKey);
|
795
842
|
|
796
843
|
if (!declarPath) {
|
797
|
-
const declar =
|
844
|
+
const declar = variableDeclaration(kind, []);
|
798
845
|
declar._blockHoist = blockHoist;
|
799
846
|
[declarPath] = path.unshiftContainer("body", [declar]);
|
800
847
|
if (!unique) path.setData(dataKey, declarPath);
|
801
848
|
}
|
802
849
|
|
803
|
-
const declarator =
|
850
|
+
const declarator = variableDeclarator(opts.id, opts.init);
|
804
851
|
declarPath.node.declarations.push(declarator);
|
805
852
|
this.registerBinding(kind, declarPath.get("declarations").pop());
|
806
853
|
}
|
@@ -891,9 +938,11 @@ class Scope {
|
|
891
938
|
if (binding) {
|
892
939
|
var _previousPath;
|
893
940
|
|
894
|
-
if ((_previousPath = previousPath) != null && _previousPath.isPattern() && binding.kind !== "param") {} else {
|
941
|
+
if ((_previousPath = previousPath) != null && _previousPath.isPattern() && binding.kind !== "param" && binding.kind !== "local") {} else {
|
895
942
|
return binding;
|
896
943
|
}
|
944
|
+
} else if (!binding && name === "arguments" && scope.path.isFunction() && !scope.path.isArrowFunctionExpression()) {
|
945
|
+
break;
|
897
946
|
}
|
898
947
|
|
899
948
|
previousPath = scope.path;
|