@babel/traverse 7.14.7 → 7.15.4
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 +12 -6
- package/lib/path/ancestry.js +6 -2
- package/lib/path/comments.js +8 -3
- package/lib/path/conversion.js +77 -31
- package/lib/path/family.js +59 -29
- package/lib/path/index.js +8 -2
- package/lib/path/inference/index.js +36 -18
- package/lib/path/inference/inferer-reference.js +28 -17
- package/lib/path/inference/inferers.js +69 -45
- package/lib/path/introspection.js +23 -11
- package/lib/path/lib/hoister.js +17 -7
- package/lib/path/lib/virtual-types.js +45 -21
- package/lib/path/modification.js +19 -8
- package/lib/path/replacement.js +47 -24
- package/lib/scope/index.js +103 -51
- package/lib/scope/lib/renamer.js +16 -8
- package/lib/types.js +0 -2
- package/lib/visitors.js +10 -4
- package/package.json +7 -7
- package/scripts/generators/validators.js +9 -0
@@ -5,8 +5,32 @@ Object.defineProperty(exports, "__esModule", {
|
|
5
5
|
});
|
6
6
|
exports.ForAwaitStatement = exports.NumericLiteralTypeAnnotation = exports.ExistentialTypeParam = exports.SpreadProperty = exports.RestProperty = exports.Flow = exports.Pure = exports.Generated = exports.User = exports.Var = exports.BlockScoped = exports.Referenced = exports.Scope = exports.Expression = exports.Statement = exports.BindingIdentifier = exports.ReferencedMemberExpression = exports.ReferencedIdentifier = void 0;
|
7
7
|
|
8
|
-
var
|
9
|
-
|
8
|
+
var _t = require("@babel/types");
|
9
|
+
|
10
|
+
const {
|
11
|
+
isBinding,
|
12
|
+
isBlockScoped,
|
13
|
+
isExportDeclaration,
|
14
|
+
isExpression,
|
15
|
+
isFlow,
|
16
|
+
isForStatement,
|
17
|
+
isForXStatement,
|
18
|
+
isIdentifier,
|
19
|
+
isImportDeclaration,
|
20
|
+
isImportSpecifier,
|
21
|
+
isJSXIdentifier,
|
22
|
+
isJSXMemberExpression,
|
23
|
+
isMemberExpression,
|
24
|
+
isReferenced,
|
25
|
+
isScope,
|
26
|
+
isStatement,
|
27
|
+
isVar,
|
28
|
+
isVariableDeclaration,
|
29
|
+
react
|
30
|
+
} = _t;
|
31
|
+
const {
|
32
|
+
isCompatTag
|
33
|
+
} = react;
|
10
34
|
const ReferencedIdentifier = {
|
11
35
|
types: ["Identifier", "JSXIdentifier"],
|
12
36
|
|
@@ -16,15 +40,15 @@ const ReferencedIdentifier = {
|
|
16
40
|
parent
|
17
41
|
} = path;
|
18
42
|
|
19
|
-
if (!
|
20
|
-
if (
|
21
|
-
if (
|
43
|
+
if (!isIdentifier(node, opts) && !isJSXMemberExpression(parent, opts)) {
|
44
|
+
if (isJSXIdentifier(node, opts)) {
|
45
|
+
if (isCompatTag(node.name)) return false;
|
22
46
|
} else {
|
23
47
|
return false;
|
24
48
|
}
|
25
49
|
}
|
26
50
|
|
27
|
-
return
|
51
|
+
return isReferenced(node, parent, path.parentPath.parent);
|
28
52
|
}
|
29
53
|
|
30
54
|
};
|
@@ -36,7 +60,7 @@ const ReferencedMemberExpression = {
|
|
36
60
|
node,
|
37
61
|
parent
|
38
62
|
}) {
|
39
|
-
return
|
63
|
+
return isMemberExpression(node) && isReferenced(node, parent);
|
40
64
|
}
|
41
65
|
|
42
66
|
};
|
@@ -50,7 +74,7 @@ const BindingIdentifier = {
|
|
50
74
|
parent
|
51
75
|
} = path;
|
52
76
|
const grandparent = path.parentPath.parent;
|
53
|
-
return
|
77
|
+
return isIdentifier(node) && isBinding(node, parent, grandparent);
|
54
78
|
}
|
55
79
|
|
56
80
|
};
|
@@ -62,12 +86,12 @@ const Statement = {
|
|
62
86
|
node,
|
63
87
|
parent
|
64
88
|
}) {
|
65
|
-
if (
|
66
|
-
if (
|
67
|
-
if (
|
89
|
+
if (isStatement(node)) {
|
90
|
+
if (isVariableDeclaration(node)) {
|
91
|
+
if (isForXStatement(parent, {
|
68
92
|
left: node
|
69
93
|
})) return false;
|
70
|
-
if (
|
94
|
+
if (isForStatement(parent, {
|
71
95
|
init: node
|
72
96
|
})) return false;
|
73
97
|
}
|
@@ -87,7 +111,7 @@ const Expression = {
|
|
87
111
|
if (path.isIdentifier()) {
|
88
112
|
return path.isReferencedIdentifier();
|
89
113
|
} else {
|
90
|
-
return
|
114
|
+
return isExpression(path.node);
|
91
115
|
}
|
92
116
|
}
|
93
117
|
|
@@ -97,21 +121,21 @@ const Scope = {
|
|
97
121
|
types: ["Scopable", "Pattern"],
|
98
122
|
|
99
123
|
checkPath(path) {
|
100
|
-
return
|
124
|
+
return isScope(path.node, path.parent);
|
101
125
|
}
|
102
126
|
|
103
127
|
};
|
104
128
|
exports.Scope = Scope;
|
105
129
|
const Referenced = {
|
106
130
|
checkPath(path) {
|
107
|
-
return
|
131
|
+
return isReferenced(path.node, path.parent);
|
108
132
|
}
|
109
133
|
|
110
134
|
};
|
111
135
|
exports.Referenced = Referenced;
|
112
136
|
const BlockScoped = {
|
113
137
|
checkPath(path) {
|
114
|
-
return
|
138
|
+
return isBlockScoped(path.node);
|
115
139
|
}
|
116
140
|
|
117
141
|
};
|
@@ -120,7 +144,7 @@ const Var = {
|
|
120
144
|
types: ["VariableDeclaration"],
|
121
145
|
|
122
146
|
checkPath(path) {
|
123
|
-
return
|
147
|
+
return isVar(path.node);
|
124
148
|
}
|
125
149
|
|
126
150
|
};
|
@@ -152,13 +176,13 @@ const Flow = {
|
|
152
176
|
checkPath({
|
153
177
|
node
|
154
178
|
}) {
|
155
|
-
if (
|
179
|
+
if (isFlow(node)) {
|
156
180
|
return true;
|
157
|
-
} else if (
|
181
|
+
} else if (isImportDeclaration(node)) {
|
158
182
|
return node.importKind === "type" || node.importKind === "typeof";
|
159
|
-
} else if (
|
183
|
+
} else if (isExportDeclaration(node)) {
|
160
184
|
return node.exportKind === "type";
|
161
|
-
} else if (
|
185
|
+
} else if (isImportSpecifier(node)) {
|
162
186
|
return node.importKind === "type" || node.importKind === "typeof";
|
163
187
|
} else {
|
164
188
|
return false;
|
package/lib/path/modification.js
CHANGED
@@ -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/replacement.js
CHANGED
@@ -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");
|