@babel/traverse 7.0.0-beta.45 → 7.0.0-beta.49
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/.npmignore +3 -0
- package/README.md +3 -3
- package/lib/cache.js +2 -2
- package/lib/context.js +35 -75
- package/lib/hub.js +6 -3
- package/lib/index.js +12 -24
- package/lib/path/ancestry.js +26 -40
- package/lib/path/comments.js +9 -9
- package/lib/path/context.js +17 -45
- package/lib/path/conversion.js +122 -128
- package/lib/path/evaluation.js +97 -136
- package/lib/path/family.js +27 -40
- package/lib/path/index.js +61 -81
- package/lib/path/inference/index.js +12 -16
- package/lib/path/inference/inferer-reference.js +37 -41
- package/lib/path/inference/inferers.js +18 -16
- package/lib/path/introspection.js +55 -99
- package/lib/path/lib/hoister.js +48 -56
- package/lib/path/lib/removal-hooks.js +2 -2
- package/lib/path/lib/virtual-types.js +76 -46
- package/lib/path/modification.js +40 -56
- package/lib/path/removal.js +3 -10
- package/lib/path/replacement.js +34 -52
- package/lib/scope/binding.js +20 -22
- package/lib/scope/index.js +280 -402
- package/lib/scope/lib/renamer.js +37 -35
- package/lib/visitors.js +62 -120
- package/package.json +9 -9
@@ -6,9 +6,9 @@ Object.defineProperty(exports, "__esModule", {
|
|
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
8
|
function t() {
|
9
|
-
|
9
|
+
const data = _interopRequireWildcard(require("@babel/types"));
|
10
10
|
|
11
|
-
t = function
|
11
|
+
t = function () {
|
12
12
|
return data;
|
13
13
|
};
|
14
14
|
|
@@ -17,12 +17,13 @@ function t() {
|
|
17
17
|
|
18
18
|
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
19
19
|
|
20
|
-
|
20
|
+
const ReferencedIdentifier = {
|
21
21
|
types: ["Identifier", "JSXIdentifier"],
|
22
|
-
checkPath: function checkPath(_ref, opts) {
|
23
|
-
var node = _ref.node,
|
24
|
-
parent = _ref.parent;
|
25
22
|
|
23
|
+
checkPath({
|
24
|
+
node,
|
25
|
+
parent
|
26
|
+
}, opts) {
|
26
27
|
if (!t().isIdentifier(node, opts) && !t().isJSXMemberExpression(parent, opts)) {
|
27
28
|
if (t().isJSXIdentifier(node, opts)) {
|
28
29
|
if (t().react.isCompatTag(node.name)) return false;
|
@@ -33,32 +34,40 @@ var ReferencedIdentifier = {
|
|
33
34
|
|
34
35
|
return t().isReferenced(node, parent);
|
35
36
|
}
|
37
|
+
|
36
38
|
};
|
37
39
|
exports.ReferencedIdentifier = ReferencedIdentifier;
|
38
|
-
|
40
|
+
const ReferencedMemberExpression = {
|
39
41
|
types: ["MemberExpression"],
|
40
|
-
|
41
|
-
|
42
|
-
|
42
|
+
|
43
|
+
checkPath({
|
44
|
+
node,
|
45
|
+
parent
|
46
|
+
}) {
|
43
47
|
return t().isMemberExpression(node) && t().isReferenced(node, parent);
|
44
48
|
}
|
49
|
+
|
45
50
|
};
|
46
51
|
exports.ReferencedMemberExpression = ReferencedMemberExpression;
|
47
|
-
|
52
|
+
const BindingIdentifier = {
|
48
53
|
types: ["Identifier"],
|
49
|
-
|
50
|
-
|
51
|
-
|
54
|
+
|
55
|
+
checkPath({
|
56
|
+
node,
|
57
|
+
parent
|
58
|
+
}) {
|
52
59
|
return t().isIdentifier(node) && t().isBinding(node, parent);
|
53
60
|
}
|
61
|
+
|
54
62
|
};
|
55
63
|
exports.BindingIdentifier = BindingIdentifier;
|
56
|
-
|
64
|
+
const Statement = {
|
57
65
|
types: ["Statement"],
|
58
|
-
checkPath: function checkPath(_ref4) {
|
59
|
-
var node = _ref4.node,
|
60
|
-
parent = _ref4.parent;
|
61
66
|
|
67
|
+
checkPath({
|
68
|
+
node,
|
69
|
+
parent
|
70
|
+
}) {
|
62
71
|
if (t().isStatement(node)) {
|
63
72
|
if (t().isVariableDeclaration(node)) {
|
64
73
|
if (t().isForXStatement(parent, {
|
@@ -74,68 +83,81 @@ var Statement = {
|
|
74
83
|
return false;
|
75
84
|
}
|
76
85
|
}
|
86
|
+
|
77
87
|
};
|
78
88
|
exports.Statement = Statement;
|
79
|
-
|
89
|
+
const Expression = {
|
80
90
|
types: ["Expression"],
|
81
|
-
|
91
|
+
|
92
|
+
checkPath(path) {
|
82
93
|
if (path.isIdentifier()) {
|
83
94
|
return path.isReferencedIdentifier();
|
84
95
|
} else {
|
85
96
|
return t().isExpression(path.node);
|
86
97
|
}
|
87
98
|
}
|
99
|
+
|
88
100
|
};
|
89
101
|
exports.Expression = Expression;
|
90
|
-
|
102
|
+
const Scope = {
|
91
103
|
types: ["Scopable"],
|
92
|
-
|
104
|
+
|
105
|
+
checkPath(path) {
|
93
106
|
return t().isScope(path.node, path.parent);
|
94
107
|
}
|
108
|
+
|
95
109
|
};
|
96
110
|
exports.Scope = Scope;
|
97
|
-
|
98
|
-
checkPath
|
111
|
+
const Referenced = {
|
112
|
+
checkPath(path) {
|
99
113
|
return t().isReferenced(path.node, path.parent);
|
100
114
|
}
|
115
|
+
|
101
116
|
};
|
102
117
|
exports.Referenced = Referenced;
|
103
|
-
|
104
|
-
checkPath
|
118
|
+
const BlockScoped = {
|
119
|
+
checkPath(path) {
|
105
120
|
return t().isBlockScoped(path.node);
|
106
121
|
}
|
122
|
+
|
107
123
|
};
|
108
124
|
exports.BlockScoped = BlockScoped;
|
109
|
-
|
125
|
+
const Var = {
|
110
126
|
types: ["VariableDeclaration"],
|
111
|
-
|
127
|
+
|
128
|
+
checkPath(path) {
|
112
129
|
return t().isVar(path.node);
|
113
130
|
}
|
131
|
+
|
114
132
|
};
|
115
133
|
exports.Var = Var;
|
116
|
-
|
117
|
-
checkPath
|
134
|
+
const User = {
|
135
|
+
checkPath(path) {
|
118
136
|
return path.node && !!path.node.loc;
|
119
137
|
}
|
138
|
+
|
120
139
|
};
|
121
140
|
exports.User = User;
|
122
|
-
|
123
|
-
checkPath
|
141
|
+
const Generated = {
|
142
|
+
checkPath(path) {
|
124
143
|
return !path.isUser();
|
125
144
|
}
|
145
|
+
|
126
146
|
};
|
127
147
|
exports.Generated = Generated;
|
128
|
-
|
129
|
-
checkPath
|
148
|
+
const Pure = {
|
149
|
+
checkPath(path, opts) {
|
130
150
|
return path.scope.isPure(path.node, opts);
|
131
151
|
}
|
152
|
+
|
132
153
|
};
|
133
154
|
exports.Pure = Pure;
|
134
|
-
|
155
|
+
const Flow = {
|
135
156
|
types: ["Flow", "ImportDeclaration", "ExportDeclaration", "ImportSpecifier"],
|
136
|
-
checkPath: function checkPath(_ref5) {
|
137
|
-
var node = _ref5.node;
|
138
157
|
|
158
|
+
checkPath({
|
159
|
+
node
|
160
|
+
}) {
|
139
161
|
if (t().isFlow(node)) {
|
140
162
|
return true;
|
141
163
|
} else if (t().isImportDeclaration(node)) {
|
@@ -148,35 +170,43 @@ var Flow = {
|
|
148
170
|
return false;
|
149
171
|
}
|
150
172
|
}
|
173
|
+
|
151
174
|
};
|
152
175
|
exports.Flow = Flow;
|
153
|
-
|
176
|
+
const RestProperty = {
|
154
177
|
types: ["RestElement"],
|
155
|
-
|
178
|
+
|
179
|
+
checkPath(path) {
|
156
180
|
return path.parentPath && path.parentPath.isObjectPattern();
|
157
181
|
}
|
182
|
+
|
158
183
|
};
|
159
184
|
exports.RestProperty = RestProperty;
|
160
|
-
|
185
|
+
const SpreadProperty = {
|
161
186
|
types: ["RestElement"],
|
162
|
-
|
187
|
+
|
188
|
+
checkPath(path) {
|
163
189
|
return path.parentPath && path.parentPath.isObjectExpression();
|
164
190
|
}
|
191
|
+
|
165
192
|
};
|
166
193
|
exports.SpreadProperty = SpreadProperty;
|
167
|
-
|
194
|
+
const ExistentialTypeParam = {
|
168
195
|
types: ["ExistsTypeAnnotation"]
|
169
196
|
};
|
170
197
|
exports.ExistentialTypeParam = ExistentialTypeParam;
|
171
|
-
|
198
|
+
const NumericLiteralTypeAnnotation = {
|
172
199
|
types: ["NumberLiteralTypeAnnotation"]
|
173
200
|
};
|
174
201
|
exports.NumericLiteralTypeAnnotation = NumericLiteralTypeAnnotation;
|
175
|
-
|
202
|
+
const ForAwaitStatement = {
|
176
203
|
types: ["ForOfStatement"],
|
177
|
-
|
178
|
-
|
204
|
+
|
205
|
+
checkPath({
|
206
|
+
node
|
207
|
+
}) {
|
179
208
|
return node.await === true;
|
180
209
|
}
|
210
|
+
|
181
211
|
};
|
182
212
|
exports.ForAwaitStatement = ForAwaitStatement;
|
package/lib/path/modification.js
CHANGED
@@ -21,9 +21,9 @@ var _hoister = _interopRequireDefault(require("./lib/hoister"));
|
|
21
21
|
var _index = _interopRequireDefault(require("./index"));
|
22
22
|
|
23
23
|
function t() {
|
24
|
-
|
24
|
+
const data = _interopRequireWildcard(require("@babel/types"));
|
25
25
|
|
26
|
-
t = function
|
26
|
+
t = function () {
|
27
27
|
return data;
|
28
28
|
};
|
29
29
|
|
@@ -38,7 +38,9 @@ function insertBefore(nodes) {
|
|
38
38
|
this._assertUnremoved();
|
39
39
|
|
40
40
|
nodes = this._verifyNodeList(nodes);
|
41
|
-
|
41
|
+
const {
|
42
|
+
parentPath
|
43
|
+
} = this;
|
42
44
|
|
43
45
|
if (parentPath.isExpressionStatement() || parentPath.isLabeledStatement() || parentPath.isExportNamedDeclaration() || parentPath.isExportDefaultDeclaration() && this.isDeclaration()) {
|
44
46
|
return parentPath.insertBefore(nodes);
|
@@ -48,7 +50,7 @@ function insertBefore(nodes) {
|
|
48
50
|
} else if (Array.isArray(this.container)) {
|
49
51
|
return this._containerInsertBefore(nodes);
|
50
52
|
} else if (this.isStatementOrBlock()) {
|
51
|
-
|
53
|
+
const shouldInsertCurrentNode = this.node && (!this.isExpressionStatement() || this.node.expression != null);
|
52
54
|
this.replaceWith(t().blockStatement(shouldInsertCurrentNode ? [this.node] : []));
|
53
55
|
return this.unshiftContainer("body", nodes);
|
54
56
|
} else {
|
@@ -57,16 +59,13 @@ function insertBefore(nodes) {
|
|
57
59
|
}
|
58
60
|
|
59
61
|
function _containerInsert(from, nodes) {
|
60
|
-
var _container;
|
61
|
-
|
62
62
|
this.updateSiblingKeys(from, nodes.length);
|
63
|
-
|
64
|
-
|
65
|
-
(_container = this.container).splice.apply(_container, [from, 0].concat(nodes));
|
63
|
+
const paths = [];
|
64
|
+
this.container.splice(from, 0, ...nodes);
|
66
65
|
|
67
|
-
for (
|
68
|
-
|
69
|
-
|
66
|
+
for (let i = 0; i < nodes.length; i++) {
|
67
|
+
const to = from + i;
|
68
|
+
const path = this.getSibling(to);
|
70
69
|
paths.push(path);
|
71
70
|
|
72
71
|
if (this.context && this.context.queue) {
|
@@ -74,29 +73,14 @@ function _containerInsert(from, nodes) {
|
|
74
73
|
}
|
75
74
|
}
|
76
75
|
|
77
|
-
|
78
|
-
|
79
|
-
for (var _i = 0; _i < paths.length; _i++) {
|
80
|
-
var _path = paths[_i];
|
81
|
-
|
82
|
-
_path.setScope();
|
83
|
-
|
84
|
-
_path.debug("Inserted.");
|
76
|
+
const contexts = this._getQueueContexts();
|
85
77
|
|
86
|
-
|
87
|
-
|
78
|
+
for (const path of paths) {
|
79
|
+
path.setScope();
|
80
|
+
path.debug("Inserted.");
|
88
81
|
|
89
|
-
|
90
|
-
|
91
|
-
_ref = _iterator[_i2++];
|
92
|
-
} else {
|
93
|
-
_i2 = _iterator.next();
|
94
|
-
if (_i2.done) break;
|
95
|
-
_ref = _i2.value;
|
96
|
-
}
|
97
|
-
|
98
|
-
var context = _ref;
|
99
|
-
context.maybeQueue(_path, true);
|
82
|
+
for (const context of contexts) {
|
83
|
+
context.maybeQueue(path, true);
|
100
84
|
}
|
101
85
|
}
|
102
86
|
|
@@ -115,13 +99,17 @@ function insertAfter(nodes) {
|
|
115
99
|
this._assertUnremoved();
|
116
100
|
|
117
101
|
nodes = this._verifyNodeList(nodes);
|
118
|
-
|
102
|
+
const {
|
103
|
+
parentPath
|
104
|
+
} = this;
|
119
105
|
|
120
106
|
if (parentPath.isExpressionStatement() || parentPath.isLabeledStatement() || parentPath.isExportNamedDeclaration() || parentPath.isExportDefaultDeclaration() && this.isDeclaration()) {
|
121
107
|
return parentPath.insertAfter(nodes);
|
122
108
|
} else if (this.isNodeType("Expression") || parentPath.isForStatement() && this.key === "init") {
|
123
109
|
if (this.node) {
|
124
|
-
|
110
|
+
let {
|
111
|
+
scope
|
112
|
+
} = this;
|
125
113
|
|
126
114
|
if (parentPath.isMethod({
|
127
115
|
computed: true,
|
@@ -130,7 +118,7 @@ function insertAfter(nodes) {
|
|
130
118
|
scope = scope.parent;
|
131
119
|
}
|
132
120
|
|
133
|
-
|
121
|
+
const temp = scope.generateDeclaredUidIdentifier();
|
134
122
|
nodes.unshift(t().expressionStatement(t().assignmentExpression("=", t().cloneNode(temp), this.node)));
|
135
123
|
nodes.push(t().expressionStatement(t().cloneNode(temp)));
|
136
124
|
}
|
@@ -139,7 +127,7 @@ function insertAfter(nodes) {
|
|
139
127
|
} else if (Array.isArray(this.container)) {
|
140
128
|
return this._containerInsertAfter(nodes);
|
141
129
|
} else if (this.isStatementOrBlock()) {
|
142
|
-
|
130
|
+
const shouldInsertCurrentNode = this.node && (!this.isExpressionStatement() || this.node.expression != null);
|
143
131
|
this.replaceWith(t().blockStatement(shouldInsertCurrentNode ? [this.node] : []));
|
144
132
|
return this.pushContainer("body", nodes);
|
145
133
|
} else {
|
@@ -150,10 +138,10 @@ function insertAfter(nodes) {
|
|
150
138
|
function updateSiblingKeys(fromIndex, incrementBy) {
|
151
139
|
if (!this.parent) return;
|
152
140
|
|
153
|
-
|
141
|
+
const paths = _cache.path.get(this.parent);
|
154
142
|
|
155
|
-
for (
|
156
|
-
|
143
|
+
for (let i = 0; i < paths.length; i++) {
|
144
|
+
const path = paths[i];
|
157
145
|
|
158
146
|
if (path.key >= fromIndex) {
|
159
147
|
path.key += incrementBy;
|
@@ -170,9 +158,9 @@ function _verifyNodeList(nodes) {
|
|
170
158
|
nodes = [nodes];
|
171
159
|
}
|
172
160
|
|
173
|
-
for (
|
174
|
-
|
175
|
-
|
161
|
+
for (let i = 0; i < nodes.length; i++) {
|
162
|
+
const node = nodes[i];
|
163
|
+
let msg;
|
176
164
|
|
177
165
|
if (!node) {
|
178
166
|
msg = "has falsy node";
|
@@ -185,8 +173,8 @@ function _verifyNodeList(nodes) {
|
|
185
173
|
}
|
186
174
|
|
187
175
|
if (msg) {
|
188
|
-
|
189
|
-
throw new Error(
|
176
|
+
const type = Array.isArray(node) ? "array" : typeof node;
|
177
|
+
throw new Error(`Node list ${msg} with the index of ${i} and type of ${type}`);
|
190
178
|
}
|
191
179
|
}
|
192
180
|
|
@@ -198,11 +186,11 @@ function unshiftContainer(listKey, nodes) {
|
|
198
186
|
|
199
187
|
nodes = this._verifyNodeList(nodes);
|
200
188
|
|
201
|
-
|
189
|
+
const path = _index.default.get({
|
202
190
|
parentPath: this,
|
203
191
|
parent: this.node,
|
204
192
|
container: this.node[listKey],
|
205
|
-
listKey
|
193
|
+
listKey,
|
206
194
|
key: 0
|
207
195
|
});
|
208
196
|
|
@@ -213,24 +201,20 @@ function pushContainer(listKey, nodes) {
|
|
213
201
|
this._assertUnremoved();
|
214
202
|
|
215
203
|
nodes = this._verifyNodeList(nodes);
|
216
|
-
|
204
|
+
const container = this.node[listKey];
|
217
205
|
|
218
|
-
|
206
|
+
const path = _index.default.get({
|
219
207
|
parentPath: this,
|
220
208
|
parent: this.node,
|
221
209
|
container: container,
|
222
|
-
listKey
|
210
|
+
listKey,
|
223
211
|
key: container.length
|
224
212
|
});
|
225
213
|
|
226
214
|
return path.replaceWithMultiple(nodes);
|
227
215
|
}
|
228
216
|
|
229
|
-
function hoist(scope) {
|
230
|
-
|
231
|
-
scope = this.scope;
|
232
|
-
}
|
233
|
-
|
234
|
-
var hoister = new _hoister.default(this, scope);
|
217
|
+
function hoist(scope = this.scope) {
|
218
|
+
const hoister = new _hoister.default(this, scope);
|
235
219
|
return hoister.run();
|
236
220
|
}
|
package/lib/path/removal.js
CHANGED
@@ -33,19 +33,12 @@ function remove() {
|
|
33
33
|
}
|
34
34
|
|
35
35
|
function _removeFromScope() {
|
36
|
-
|
37
|
-
|
38
|
-
var bindings = this.getBindingIdentifiers();
|
39
|
-
Object.keys(bindings).forEach(function (name) {
|
40
|
-
return _this.scope.removeBinding(name);
|
41
|
-
});
|
36
|
+
const bindings = this.getBindingIdentifiers();
|
37
|
+
Object.keys(bindings).forEach(name => this.scope.removeBinding(name));
|
42
38
|
}
|
43
39
|
|
44
40
|
function _callRemovalHooks() {
|
45
|
-
|
46
|
-
|
47
|
-
for (var _i = 0; _i < _arr.length; _i++) {
|
48
|
-
var fn = _arr[_i];
|
41
|
+
for (const fn of _removalHooks.hooks) {
|
49
42
|
if (fn(this, this.parentPath)) return true;
|
50
43
|
}
|
51
44
|
}
|
package/lib/path/replacement.js
CHANGED
@@ -11,9 +11,9 @@ exports.replaceExpressionWithStatements = replaceExpressionWithStatements;
|
|
11
11
|
exports.replaceInline = replaceInline;
|
12
12
|
|
13
13
|
function _codeFrame() {
|
14
|
-
|
14
|
+
const data = require("@babel/code-frame");
|
15
15
|
|
16
|
-
_codeFrame = function
|
16
|
+
_codeFrame = function () {
|
17
17
|
return data;
|
18
18
|
};
|
19
19
|
|
@@ -24,10 +24,10 @@ var _index = _interopRequireDefault(require("../index"));
|
|
24
24
|
|
25
25
|
var _index2 = _interopRequireDefault(require("./index"));
|
26
26
|
|
27
|
-
function
|
28
|
-
|
27
|
+
function _parser() {
|
28
|
+
const data = require("@babel/parser");
|
29
29
|
|
30
|
-
|
30
|
+
_parser = function () {
|
31
31
|
return data;
|
32
32
|
};
|
33
33
|
|
@@ -35,9 +35,9 @@ function _babylon() {
|
|
35
35
|
}
|
36
36
|
|
37
37
|
function t() {
|
38
|
-
|
38
|
+
const data = _interopRequireWildcard(require("@babel/types"));
|
39
39
|
|
40
|
-
t = function
|
40
|
+
t = function () {
|
41
41
|
return data;
|
42
42
|
};
|
43
43
|
|
@@ -48,26 +48,24 @@ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj;
|
|
48
48
|
|
49
49
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
50
50
|
|
51
|
-
|
52
|
-
Function
|
51
|
+
const hoistVariablesVisitor = {
|
52
|
+
Function(path) {
|
53
53
|
path.skip();
|
54
54
|
},
|
55
|
-
|
55
|
+
|
56
|
+
VariableDeclaration(path) {
|
56
57
|
if (path.node.kind !== "var") return;
|
57
|
-
|
58
|
+
const bindings = path.getBindingIdentifiers();
|
58
59
|
|
59
|
-
for (
|
60
|
+
for (const key in bindings) {
|
60
61
|
path.scope.push({
|
61
62
|
id: bindings[key]
|
62
63
|
});
|
63
64
|
}
|
64
65
|
|
65
|
-
|
66
|
-
var _arr = path.node.declarations;
|
67
|
-
|
68
|
-
for (var _i = 0; _i < _arr.length; _i++) {
|
69
|
-
var declar = _arr[_i];
|
66
|
+
const exprs = [];
|
70
67
|
|
68
|
+
for (const declar of path.node.declarations) {
|
71
69
|
if (declar.init) {
|
72
70
|
exprs.push(t().expressionStatement(t().assignmentExpression("=", declar.id, declar.init)));
|
73
71
|
}
|
@@ -75,6 +73,7 @@ var hoistVariablesVisitor = {
|
|
75
73
|
|
76
74
|
path.replaceWithMultiple(exprs);
|
77
75
|
}
|
76
|
+
|
78
77
|
};
|
79
78
|
|
80
79
|
function replaceWithMultiple(nodes) {
|
@@ -83,7 +82,7 @@ function replaceWithMultiple(nodes) {
|
|
83
82
|
t().inheritLeadingComments(nodes[0], this.node);
|
84
83
|
t().inheritTrailingComments(nodes[nodes.length - 1], this.node);
|
85
84
|
this.node = this.container[this.key] = null;
|
86
|
-
|
85
|
+
const paths = this.insertAfter(nodes);
|
87
86
|
|
88
87
|
if (this.node) {
|
89
88
|
this.requeue();
|
@@ -98,10 +97,10 @@ function replaceWithSourceString(replacement) {
|
|
98
97
|
this.resync();
|
99
98
|
|
100
99
|
try {
|
101
|
-
replacement =
|
102
|
-
replacement = (0,
|
100
|
+
replacement = `(${replacement})`;
|
101
|
+
replacement = (0, _parser().parse)(replacement);
|
103
102
|
} catch (err) {
|
104
|
-
|
103
|
+
const loc = err.loc;
|
105
104
|
|
106
105
|
if (loc) {
|
107
106
|
err.message += " - make sure this is an expression.\n" + (0, _codeFrame().codeFrameColumns)(replacement, {
|
@@ -154,7 +153,7 @@ function replaceWith(replacement) {
|
|
154
153
|
throw new Error("Don't use `path.replaceWith()` with a source string, use `path.replaceWithSourceString()`");
|
155
154
|
}
|
156
155
|
|
157
|
-
|
156
|
+
let nodePath = "";
|
158
157
|
|
159
158
|
if (this.isNodeType("Statement") && t().isExpression(replacement)) {
|
160
159
|
if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement) && !this.parentPath.isExportDefaultDeclaration()) {
|
@@ -169,7 +168,7 @@ function replaceWith(replacement) {
|
|
169
168
|
}
|
170
169
|
}
|
171
170
|
|
172
|
-
|
171
|
+
const oldNode = this.node;
|
173
172
|
|
174
173
|
if (oldNode) {
|
175
174
|
t().inheritsComments(replacement, oldNode);
|
@@ -195,51 +194,34 @@ function _replaceWith(node) {
|
|
195
194
|
t().validate(this.parent, this.key, node);
|
196
195
|
}
|
197
196
|
|
198
|
-
this.debug(
|
197
|
+
this.debug(`Replace with ${node && node.type}`);
|
199
198
|
this.node = this.container[this.key] = node;
|
200
199
|
}
|
201
200
|
|
202
201
|
function replaceExpressionWithStatements(nodes) {
|
203
202
|
this.resync();
|
204
|
-
|
203
|
+
const toSequenceExpression = t().toSequenceExpression(nodes, this.scope);
|
205
204
|
|
206
205
|
if (toSequenceExpression) {
|
207
206
|
return this.replaceWith(toSequenceExpression)[0].get("expressions");
|
208
207
|
}
|
209
208
|
|
210
|
-
|
209
|
+
const container = t().arrowFunctionExpression([], t().blockStatement(nodes));
|
211
210
|
this.replaceWith(t().callExpression(container, []));
|
212
211
|
this.traverse(hoistVariablesVisitor);
|
213
|
-
|
214
|
-
|
215
|
-
for (var _iterator = completionRecords, _isArray = Array.isArray(_iterator), _i2 = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
216
|
-
var _ref;
|
217
|
-
|
218
|
-
if (_isArray) {
|
219
|
-
if (_i2 >= _iterator.length) break;
|
220
|
-
_ref = _iterator[_i2++];
|
221
|
-
} else {
|
222
|
-
_i2 = _iterator.next();
|
223
|
-
if (_i2.done) break;
|
224
|
-
_ref = _i2.value;
|
225
|
-
}
|
212
|
+
const completionRecords = this.get("callee").getCompletionRecords();
|
226
213
|
|
227
|
-
|
214
|
+
for (const path of completionRecords) {
|
228
215
|
if (!path.isExpressionStatement()) continue;
|
229
|
-
|
230
|
-
return path.isLoop();
|
231
|
-
});
|
216
|
+
const loop = path.findParent(path => path.isLoop());
|
232
217
|
|
233
218
|
if (loop) {
|
234
|
-
|
219
|
+
let uid = loop.getData("expressionReplacementReturnUid");
|
235
220
|
|
236
221
|
if (!uid) {
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
_callee.get("body").pushContainer("body", t().returnStatement(t().cloneNode(uid)));
|
242
|
-
|
222
|
+
const callee = this.get("callee");
|
223
|
+
uid = callee.scope.generateDeclaredUidIdentifier("ret");
|
224
|
+
callee.get("body").pushContainer("body", t().returnStatement(t().cloneNode(uid)));
|
243
225
|
loop.setData("expressionReplacementReturnUid", uid);
|
244
226
|
} else {
|
245
227
|
uid = t().identifier(uid.name);
|
@@ -251,7 +233,7 @@ function replaceExpressionWithStatements(nodes) {
|
|
251
233
|
}
|
252
234
|
}
|
253
235
|
|
254
|
-
|
236
|
+
const callee = this.get("callee");
|
255
237
|
callee.arrowFunctionToExpression();
|
256
238
|
return callee.get("body.body");
|
257
239
|
}
|
@@ -263,7 +245,7 @@ function replaceInline(nodes) {
|
|
263
245
|
if (Array.isArray(this.container)) {
|
264
246
|
nodes = this._verifyNodeList(nodes);
|
265
247
|
|
266
|
-
|
248
|
+
const paths = this._containerInsertAfter(nodes);
|
267
249
|
|
268
250
|
this.remove();
|
269
251
|
return paths;
|