@babel/traverse 7.6.2 → 7.7.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 +3 -11
- package/lib/index.js +12 -24
- package/lib/path/ancestry.js +3 -11
- package/lib/path/comments.js +4 -12
- package/lib/path/context.js +14 -10
- package/lib/path/conversion.js +70 -107
- package/lib/path/family.js +5 -13
- package/lib/path/index.js +77 -42
- package/lib/path/inference/index.js +19 -27
- package/lib/path/inference/inferer-reference.js +9 -17
- package/lib/path/inference/inferers.js +38 -46
- package/lib/path/introspection.js +11 -27
- package/lib/path/lib/hoister.js +18 -15
- package/lib/path/lib/virtual-types.js +21 -29
- package/lib/path/modification.js +8 -16
- package/lib/path/removal.js +3 -2
- package/lib/path/replacement.js +26 -50
- package/lib/scope/index.js +60 -100
- package/lib/scope/lib/renamer.js +9 -25
- package/lib/visitors.js +7 -23
- package/package.json +8 -8
package/lib/path/lib/hoister.js
CHANGED
@@ -5,23 +5,15 @@ Object.defineProperty(exports, "__esModule", {
|
|
5
5
|
});
|
6
6
|
exports.default = void 0;
|
7
7
|
|
8
|
-
|
9
|
-
const data = _interopRequireWildcard(require("@babel/types"));
|
10
|
-
|
11
|
-
t = function () {
|
12
|
-
return data;
|
13
|
-
};
|
14
|
-
|
15
|
-
return data;
|
16
|
-
}
|
8
|
+
var t = _interopRequireWildcard(require("@babel/types"));
|
17
9
|
|
18
10
|
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
19
11
|
|
20
|
-
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {};
|
12
|
+
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; }
|
21
13
|
|
22
14
|
const referenceVisitor = {
|
23
15
|
ReferencedIdentifier(path, state) {
|
24
|
-
if (path.isJSXIdentifier() && t
|
16
|
+
if (path.isJSXIdentifier() && t.react.isCompatTag(path.node.name) && !path.parentPath.isJSXMemberExpression()) {
|
25
17
|
return;
|
26
18
|
}
|
27
19
|
|
@@ -39,6 +31,15 @@ const referenceVisitor = {
|
|
39
31
|
|
40
32
|
const binding = path.scope.getBinding(path.node.name);
|
41
33
|
if (!binding) return;
|
34
|
+
|
35
|
+
for (const violation of binding.constantViolations) {
|
36
|
+
if (violation.scope !== binding.path.scope) {
|
37
|
+
state.mutableBinding = true;
|
38
|
+
path.stop();
|
39
|
+
return;
|
40
|
+
}
|
41
|
+
}
|
42
|
+
|
42
43
|
if (binding !== state.scope.getBinding(path.node.name)) return;
|
43
44
|
state.bindings[path.node.name] = binding;
|
44
45
|
}
|
@@ -49,6 +50,7 @@ class PathHoister {
|
|
49
50
|
constructor(path, scope) {
|
50
51
|
this.breakOnScopePaths = [];
|
51
52
|
this.bindings = {};
|
53
|
+
this.mutableBinding = false;
|
52
54
|
this.scopes = [];
|
53
55
|
this.scope = scope;
|
54
56
|
this.path = path;
|
@@ -167,21 +169,22 @@ class PathHoister {
|
|
167
169
|
|
168
170
|
run() {
|
169
171
|
this.path.traverse(referenceVisitor, this);
|
172
|
+
if (this.mutableBinding) return;
|
170
173
|
this.getCompatibleScopes();
|
171
174
|
const attachTo = this.getAttachmentPath();
|
172
175
|
if (!attachTo) return;
|
173
176
|
if (attachTo.getFunctionParent() === this.path.getFunctionParent()) return;
|
174
177
|
let uid = attachTo.scope.generateUidIdentifier("ref");
|
175
|
-
const declarator = t
|
178
|
+
const declarator = t.variableDeclarator(uid, this.path.node);
|
176
179
|
const insertFn = this.attachAfter ? "insertAfter" : "insertBefore";
|
177
|
-
const [attached] = attachTo[insertFn]([attachTo.isVariableDeclarator() ? declarator : t
|
180
|
+
const [attached] = attachTo[insertFn]([attachTo.isVariableDeclarator() ? declarator : t.variableDeclaration("var", [declarator])]);
|
178
181
|
const parent = this.path.parentPath;
|
179
182
|
|
180
183
|
if (parent.isJSXElement() && this.path.container === parent.node.children) {
|
181
|
-
uid = t
|
184
|
+
uid = t.JSXExpressionContainer(uid);
|
182
185
|
}
|
183
186
|
|
184
|
-
this.path.replaceWith(t
|
187
|
+
this.path.replaceWith(t.cloneNode(uid));
|
185
188
|
return attachTo.isVariableDeclarator() ? attached.get("init") : attached.get("declarations.0.init");
|
186
189
|
}
|
187
190
|
|
@@ -5,19 +5,11 @@ 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
|
-
|
9
|
-
const data = _interopRequireWildcard(require("@babel/types"));
|
10
|
-
|
11
|
-
t = function () {
|
12
|
-
return data;
|
13
|
-
};
|
14
|
-
|
15
|
-
return data;
|
16
|
-
}
|
8
|
+
var t = _interopRequireWildcard(require("@babel/types"));
|
17
9
|
|
18
10
|
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
19
11
|
|
20
|
-
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {};
|
12
|
+
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; }
|
21
13
|
|
22
14
|
const ReferencedIdentifier = {
|
23
15
|
types: ["Identifier", "JSXIdentifier"],
|
@@ -28,15 +20,15 @@ const ReferencedIdentifier = {
|
|
28
20
|
parent
|
29
21
|
} = path;
|
30
22
|
|
31
|
-
if (!t
|
32
|
-
if (t
|
33
|
-
if (t
|
23
|
+
if (!t.isIdentifier(node, opts) && !t.isJSXMemberExpression(parent, opts)) {
|
24
|
+
if (t.isJSXIdentifier(node, opts)) {
|
25
|
+
if (t.react.isCompatTag(node.name)) return false;
|
34
26
|
} else {
|
35
27
|
return false;
|
36
28
|
}
|
37
29
|
}
|
38
30
|
|
39
|
-
return t
|
31
|
+
return t.isReferenced(node, parent, path.parentPath.parent);
|
40
32
|
}
|
41
33
|
|
42
34
|
};
|
@@ -48,7 +40,7 @@ const ReferencedMemberExpression = {
|
|
48
40
|
node,
|
49
41
|
parent
|
50
42
|
}) {
|
51
|
-
return t
|
43
|
+
return t.isMemberExpression(node) && t.isReferenced(node, parent);
|
52
44
|
}
|
53
45
|
|
54
46
|
};
|
@@ -62,7 +54,7 @@ const BindingIdentifier = {
|
|
62
54
|
parent
|
63
55
|
} = path;
|
64
56
|
const grandparent = path.parentPath.parent;
|
65
|
-
return t
|
57
|
+
return t.isIdentifier(node) && t.isBinding(node, parent, grandparent);
|
66
58
|
}
|
67
59
|
|
68
60
|
};
|
@@ -74,12 +66,12 @@ const Statement = {
|
|
74
66
|
node,
|
75
67
|
parent
|
76
68
|
}) {
|
77
|
-
if (t
|
78
|
-
if (t
|
79
|
-
if (t
|
69
|
+
if (t.isStatement(node)) {
|
70
|
+
if (t.isVariableDeclaration(node)) {
|
71
|
+
if (t.isForXStatement(parent, {
|
80
72
|
left: node
|
81
73
|
})) return false;
|
82
|
-
if (t
|
74
|
+
if (t.isForStatement(parent, {
|
83
75
|
init: node
|
84
76
|
})) return false;
|
85
77
|
}
|
@@ -99,7 +91,7 @@ const Expression = {
|
|
99
91
|
if (path.isIdentifier()) {
|
100
92
|
return path.isReferencedIdentifier();
|
101
93
|
} else {
|
102
|
-
return t
|
94
|
+
return t.isExpression(path.node);
|
103
95
|
}
|
104
96
|
}
|
105
97
|
|
@@ -109,21 +101,21 @@ const Scope = {
|
|
109
101
|
types: ["Scopable"],
|
110
102
|
|
111
103
|
checkPath(path) {
|
112
|
-
return t
|
104
|
+
return t.isScope(path.node, path.parent);
|
113
105
|
}
|
114
106
|
|
115
107
|
};
|
116
108
|
exports.Scope = Scope;
|
117
109
|
const Referenced = {
|
118
110
|
checkPath(path) {
|
119
|
-
return t
|
111
|
+
return t.isReferenced(path.node, path.parent);
|
120
112
|
}
|
121
113
|
|
122
114
|
};
|
123
115
|
exports.Referenced = Referenced;
|
124
116
|
const BlockScoped = {
|
125
117
|
checkPath(path) {
|
126
|
-
return t
|
118
|
+
return t.isBlockScoped(path.node);
|
127
119
|
}
|
128
120
|
|
129
121
|
};
|
@@ -132,7 +124,7 @@ const Var = {
|
|
132
124
|
types: ["VariableDeclaration"],
|
133
125
|
|
134
126
|
checkPath(path) {
|
135
|
-
return t
|
127
|
+
return t.isVar(path.node);
|
136
128
|
}
|
137
129
|
|
138
130
|
};
|
@@ -164,13 +156,13 @@ const Flow = {
|
|
164
156
|
checkPath({
|
165
157
|
node
|
166
158
|
}) {
|
167
|
-
if (t
|
159
|
+
if (t.isFlow(node)) {
|
168
160
|
return true;
|
169
|
-
} else if (t
|
161
|
+
} else if (t.isImportDeclaration(node)) {
|
170
162
|
return node.importKind === "type" || node.importKind === "typeof";
|
171
|
-
} else if (t
|
163
|
+
} else if (t.isExportDeclaration(node)) {
|
172
164
|
return node.exportKind === "type";
|
173
|
-
} else if (t
|
165
|
+
} else if (t.isImportSpecifier(node)) {
|
174
166
|
return node.importKind === "type" || node.importKind === "typeof";
|
175
167
|
} else {
|
176
168
|
return false;
|
package/lib/path/modification.js
CHANGED
@@ -20,19 +20,11 @@ var _hoister = _interopRequireDefault(require("./lib/hoister"));
|
|
20
20
|
|
21
21
|
var _index = _interopRequireDefault(require("./index"));
|
22
22
|
|
23
|
-
|
24
|
-
const data = _interopRequireWildcard(require("@babel/types"));
|
25
|
-
|
26
|
-
t = function () {
|
27
|
-
return data;
|
28
|
-
};
|
29
|
-
|
30
|
-
return data;
|
31
|
-
}
|
23
|
+
var t = _interopRequireWildcard(require("@babel/types"));
|
32
24
|
|
33
25
|
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
34
26
|
|
35
|
-
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {};
|
27
|
+
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; }
|
36
28
|
|
37
29
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
38
30
|
|
@@ -53,7 +45,7 @@ function insertBefore(nodes) {
|
|
53
45
|
return this._containerInsertBefore(nodes);
|
54
46
|
} else if (this.isStatementOrBlock()) {
|
55
47
|
const shouldInsertCurrentNode = this.node && (!this.isExpressionStatement() || this.node.expression != null);
|
56
|
-
this.replaceWith(t
|
48
|
+
this.replaceWith(t.blockStatement(shouldInsertCurrentNode ? [this.node] : []));
|
57
49
|
return this.unshiftContainer("body", nodes);
|
58
50
|
} else {
|
59
51
|
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?");
|
@@ -107,9 +99,9 @@ function insertAfter(nodes) {
|
|
107
99
|
|
108
100
|
if (parentPath.isExpressionStatement() || parentPath.isLabeledStatement() || parentPath.isExportNamedDeclaration() || parentPath.isExportDefaultDeclaration() && this.isDeclaration()) {
|
109
101
|
return parentPath.insertAfter(nodes.map(node => {
|
110
|
-
return t
|
102
|
+
return t.isExpression(node) ? t.expressionStatement(node) : node;
|
111
103
|
}));
|
112
|
-
} else if (this.isNodeType("Expression") && !this.isJSXElement() || parentPath.isForStatement() && this.key === "init") {
|
104
|
+
} else if (this.isNodeType("Expression") && !this.isJSXElement() && !parentPath.isJSXElement() || parentPath.isForStatement() && this.key === "init") {
|
113
105
|
if (this.node) {
|
114
106
|
let {
|
115
107
|
scope
|
@@ -123,8 +115,8 @@ function insertAfter(nodes) {
|
|
123
115
|
}
|
124
116
|
|
125
117
|
const temp = scope.generateDeclaredUidIdentifier();
|
126
|
-
nodes.unshift(t
|
127
|
-
nodes.push(t
|
118
|
+
nodes.unshift(t.expressionStatement(t.assignmentExpression("=", t.cloneNode(temp), this.node)));
|
119
|
+
nodes.push(t.expressionStatement(t.cloneNode(temp)));
|
128
120
|
}
|
129
121
|
|
130
122
|
return this.replaceExpressionWithStatements(nodes);
|
@@ -132,7 +124,7 @@ function insertAfter(nodes) {
|
|
132
124
|
return this._containerInsertAfter(nodes);
|
133
125
|
} else if (this.isStatementOrBlock()) {
|
134
126
|
const shouldInsertCurrentNode = this.node && (!this.isExpressionStatement() || this.node.expression != null);
|
135
|
-
this.replaceWith(t
|
127
|
+
this.replaceWith(t.blockStatement(shouldInsertCurrentNode ? [this.node] : []));
|
136
128
|
return this.pushContainer("body", nodes);
|
137
129
|
} else {
|
138
130
|
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
@@ -12,6 +12,8 @@ exports._assertUnremoved = _assertUnremoved;
|
|
12
12
|
|
13
13
|
var _removalHooks = require("./lib/removal-hooks");
|
14
14
|
|
15
|
+
var _index = require("./index");
|
16
|
+
|
15
17
|
function remove() {
|
16
18
|
this._assertUnremoved();
|
17
19
|
|
@@ -53,8 +55,7 @@ function _remove() {
|
|
53
55
|
}
|
54
56
|
|
55
57
|
function _markRemoved() {
|
56
|
-
this.
|
57
|
-
this.removed = true;
|
58
|
+
this._traverseFlags |= _index.SHOULD_SKIP | _index.REMOVED;
|
58
59
|
this.node = null;
|
59
60
|
}
|
60
61
|
|
package/lib/path/replacement.js
CHANGED
@@ -10,43 +10,19 @@ exports._replaceWith = _replaceWith;
|
|
10
10
|
exports.replaceExpressionWithStatements = replaceExpressionWithStatements;
|
11
11
|
exports.replaceInline = replaceInline;
|
12
12
|
|
13
|
-
|
14
|
-
const data = require("@babel/code-frame");
|
15
|
-
|
16
|
-
_codeFrame = function () {
|
17
|
-
return data;
|
18
|
-
};
|
19
|
-
|
20
|
-
return data;
|
21
|
-
}
|
13
|
+
var _codeFrame = require("@babel/code-frame");
|
22
14
|
|
23
15
|
var _index = _interopRequireDefault(require("../index"));
|
24
16
|
|
25
17
|
var _index2 = _interopRequireDefault(require("./index"));
|
26
18
|
|
27
|
-
|
28
|
-
const data = require("@babel/parser");
|
19
|
+
var _parser = require("@babel/parser");
|
29
20
|
|
30
|
-
|
31
|
-
return data;
|
32
|
-
};
|
33
|
-
|
34
|
-
return data;
|
35
|
-
}
|
36
|
-
|
37
|
-
function t() {
|
38
|
-
const data = _interopRequireWildcard(require("@babel/types"));
|
39
|
-
|
40
|
-
t = function () {
|
41
|
-
return data;
|
42
|
-
};
|
43
|
-
|
44
|
-
return data;
|
45
|
-
}
|
21
|
+
var t = _interopRequireWildcard(require("@babel/types"));
|
46
22
|
|
47
23
|
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
48
24
|
|
49
|
-
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {};
|
25
|
+
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; }
|
50
26
|
|
51
27
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
52
28
|
|
@@ -69,7 +45,7 @@ const hoistVariablesVisitor = {
|
|
69
45
|
|
70
46
|
for (const declar of path.node.declarations) {
|
71
47
|
if (declar.init) {
|
72
|
-
exprs.push(t
|
48
|
+
exprs.push(t.expressionStatement(t.assignmentExpression("=", declar.id, declar.init)));
|
73
49
|
}
|
74
50
|
}
|
75
51
|
|
@@ -81,8 +57,8 @@ const hoistVariablesVisitor = {
|
|
81
57
|
function replaceWithMultiple(nodes) {
|
82
58
|
this.resync();
|
83
59
|
nodes = this._verifyNodeList(nodes);
|
84
|
-
t
|
85
|
-
t
|
60
|
+
t.inheritLeadingComments(nodes[0], this.node);
|
61
|
+
t.inheritTrailingComments(nodes[nodes.length - 1], this.node);
|
86
62
|
this.node = this.container[this.key] = null;
|
87
63
|
const paths = this.insertAfter(nodes);
|
88
64
|
|
@@ -100,12 +76,12 @@ function replaceWithSourceString(replacement) {
|
|
100
76
|
|
101
77
|
try {
|
102
78
|
replacement = `(${replacement})`;
|
103
|
-
replacement = (0, _parser
|
79
|
+
replacement = (0, _parser.parse)(replacement);
|
104
80
|
} catch (err) {
|
105
81
|
const loc = err.loc;
|
106
82
|
|
107
83
|
if (loc) {
|
108
|
-
err.message += " - make sure this is an expression.\n" + (0, _codeFrame
|
84
|
+
err.message += " - make sure this is an expression.\n" + (0, _codeFrame.codeFrameColumns)(replacement, {
|
109
85
|
start: {
|
110
86
|
line: loc.line,
|
111
87
|
column: loc.column + 1
|
@@ -143,7 +119,7 @@ function replaceWith(replacement) {
|
|
143
119
|
return [this];
|
144
120
|
}
|
145
121
|
|
146
|
-
if (this.isProgram() && !t
|
122
|
+
if (this.isProgram() && !t.isProgram(replacement)) {
|
147
123
|
throw new Error("You can only replace a Program root node with another Program node");
|
148
124
|
}
|
149
125
|
|
@@ -157,14 +133,14 @@ function replaceWith(replacement) {
|
|
157
133
|
|
158
134
|
let nodePath = "";
|
159
135
|
|
160
|
-
if (this.isNodeType("Statement") && t
|
136
|
+
if (this.isNodeType("Statement") && t.isExpression(replacement)) {
|
161
137
|
if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement) && !this.parentPath.isExportDefaultDeclaration()) {
|
162
|
-
replacement = t
|
138
|
+
replacement = t.expressionStatement(replacement);
|
163
139
|
nodePath = "expression";
|
164
140
|
}
|
165
141
|
}
|
166
142
|
|
167
|
-
if (this.isNodeType("Expression") && t
|
143
|
+
if (this.isNodeType("Expression") && t.isStatement(replacement)) {
|
168
144
|
if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement)) {
|
169
145
|
return this.replaceExpressionWithStatements([replacement]);
|
170
146
|
}
|
@@ -173,8 +149,8 @@ function replaceWith(replacement) {
|
|
173
149
|
const oldNode = this.node;
|
174
150
|
|
175
151
|
if (oldNode) {
|
176
|
-
t
|
177
|
-
t
|
152
|
+
t.inheritsComments(replacement, oldNode);
|
153
|
+
t.removeComments(oldNode);
|
178
154
|
}
|
179
155
|
|
180
156
|
this._replaceWith(replacement);
|
@@ -191,9 +167,9 @@ function _replaceWith(node) {
|
|
191
167
|
}
|
192
168
|
|
193
169
|
if (this.inList) {
|
194
|
-
t
|
170
|
+
t.validate(this.parent, this.key, [node]);
|
195
171
|
} else {
|
196
|
-
t
|
172
|
+
t.validate(this.parent, this.key, node);
|
197
173
|
}
|
198
174
|
|
199
175
|
this.debug(`Replace with ${node && node.type}`);
|
@@ -202,7 +178,7 @@ function _replaceWith(node) {
|
|
202
178
|
|
203
179
|
function replaceExpressionWithStatements(nodes) {
|
204
180
|
this.resync();
|
205
|
-
const toSequenceExpression = t
|
181
|
+
const toSequenceExpression = t.toSequenceExpression(nodes, this.scope);
|
206
182
|
|
207
183
|
if (toSequenceExpression) {
|
208
184
|
return this.replaceWith(toSequenceExpression)[0].get("expressions");
|
@@ -210,8 +186,8 @@ function replaceExpressionWithStatements(nodes) {
|
|
210
186
|
|
211
187
|
const functionParent = this.getFunctionParent();
|
212
188
|
const isParentAsync = functionParent && functionParent.is("async");
|
213
|
-
const container = t
|
214
|
-
this.replaceWith(t
|
189
|
+
const container = t.arrowFunctionExpression([], t.blockStatement(nodes));
|
190
|
+
this.replaceWith(t.callExpression(container, []));
|
215
191
|
this.traverse(hoistVariablesVisitor);
|
216
192
|
const completionRecords = this.get("callee").getCompletionRecords();
|
217
193
|
|
@@ -225,24 +201,24 @@ function replaceExpressionWithStatements(nodes) {
|
|
225
201
|
if (!uid) {
|
226
202
|
const callee = this.get("callee");
|
227
203
|
uid = callee.scope.generateDeclaredUidIdentifier("ret");
|
228
|
-
callee.get("body").pushContainer("body", t
|
204
|
+
callee.get("body").pushContainer("body", t.returnStatement(t.cloneNode(uid)));
|
229
205
|
loop.setData("expressionReplacementReturnUid", uid);
|
230
206
|
} else {
|
231
|
-
uid = t
|
207
|
+
uid = t.identifier(uid.name);
|
232
208
|
}
|
233
209
|
|
234
|
-
path.get("expression").replaceWith(t
|
210
|
+
path.get("expression").replaceWith(t.assignmentExpression("=", t.cloneNode(uid), path.node.expression));
|
235
211
|
} else {
|
236
|
-
path.replaceWith(t
|
212
|
+
path.replaceWith(t.returnStatement(path.node.expression));
|
237
213
|
}
|
238
214
|
}
|
239
215
|
|
240
216
|
const callee = this.get("callee");
|
241
217
|
callee.arrowFunctionToExpression();
|
242
218
|
|
243
|
-
if (isParentAsync && _index.default.hasType(this.get("callee.body").node, "AwaitExpression", t
|
219
|
+
if (isParentAsync && _index.default.hasType(this.get("callee.body").node, "AwaitExpression", t.FUNCTION_TYPES)) {
|
244
220
|
callee.set("async", true);
|
245
|
-
this.replaceWith(t
|
221
|
+
this.replaceWith(t.awaitExpression(this.node));
|
246
222
|
}
|
247
223
|
|
248
224
|
return callee.get("body.body");
|