@babel/traverse 7.1.0 → 7.23.2
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.
- package/LICENSE +1 -1
- package/README.md +1 -1
- package/lib/cache.js +27 -7
- package/lib/cache.js.map +1 -0
- package/lib/context.js +22 -59
- package/lib/context.js.map +1 -0
- package/lib/hub.js +2 -6
- package/lib/hub.js.map +1 -0
- package/lib/index.js +42 -77
- package/lib/index.js.map +1 -0
- package/lib/path/ancestry.js +15 -62
- package/lib/path/ancestry.js.map +1 -0
- package/lib/path/comments.js +31 -24
- package/lib/path/comments.js.map +1 -0
- package/lib/path/context.js +68 -91
- package/lib/path/context.js.map +1 -0
- package/lib/path/conversion.js +275 -273
- package/lib/path/conversion.js.map +1 -0
- package/lib/path/evaluation.js +84 -149
- package/lib/path/evaluation.js.map +1 -0
- package/lib/path/family.js +189 -94
- package/lib/path/family.js.map +1 -0
- package/lib/path/index.js +106 -132
- package/lib/path/index.js.map +1 -0
- package/lib/path/inference/index.js +81 -64
- package/lib/path/inference/index.js.map +1 -0
- package/lib/path/inference/inferer-reference.js +22 -52
- package/lib/path/inference/inferer-reference.js.map +1 -0
- package/lib/path/inference/inferers.js +110 -125
- package/lib/path/inference/inferers.js.map +1 -0
- package/lib/path/inference/util.js +30 -0
- package/lib/path/inference/util.js.map +1 -0
- package/lib/path/introspection.js +182 -168
- package/lib/path/introspection.js.map +1 -0
- package/lib/path/lib/hoister.js +37 -54
- package/lib/path/lib/hoister.js.map +1 -0
- package/lib/path/lib/removal-hooks.js +4 -4
- package/lib/path/lib/removal-hooks.js.map +1 -0
- package/lib/path/lib/virtual-types-validator.js +161 -0
- package/lib/path/lib/virtual-types-validator.js.map +1 -0
- package/lib/path/lib/virtual-types.js +21 -189
- package/lib/path/lib/virtual-types.js.map +1 -0
- package/lib/path/modification.js +103 -98
- package/lib/path/modification.js.map +1 -0
- package/lib/path/removal.js +18 -23
- package/lib/path/removal.js.map +1 -0
- package/lib/path/replacement.js +91 -144
- package/lib/path/replacement.js.map +1 -0
- package/lib/scope/binding.js +28 -16
- package/lib/scope/binding.js.map +1 -0
- package/lib/scope/index.js +407 -414
- package/lib/scope/index.js.map +1 -0
- package/lib/scope/lib/renamer.js +45 -70
- package/lib/scope/lib/renamer.js.map +1 -0
- package/lib/traverse-node.js +29 -0
- package/lib/traverse-node.js.map +1 -0
- package/lib/types.js +3 -0
- package/lib/types.js.map +1 -0
- package/lib/visitors.js +77 -113
- package/lib/visitors.js.map +1 -0
- package/package.json +28 -17
package/lib/path/conversion.js
CHANGED
@@ -3,150 +3,169 @@
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
4
4
|
value: true
|
5
5
|
});
|
6
|
-
exports.
|
6
|
+
exports.arrowFunctionToExpression = arrowFunctionToExpression;
|
7
7
|
exports.ensureBlock = ensureBlock;
|
8
|
-
exports.
|
8
|
+
exports.toComputedKey = toComputedKey;
|
9
9
|
exports.unwrapFunctionEnvironment = unwrapFunctionEnvironment;
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
10
|
+
var _t = require("@babel/types");
|
11
|
+
var _helperEnvironmentVisitor = require("@babel/helper-environment-visitor");
|
12
|
+
var _helperFunctionName = require("@babel/helper-function-name");
|
13
|
+
var _visitors = require("../visitors.js");
|
14
|
+
const {
|
15
|
+
arrowFunctionExpression,
|
16
|
+
assignmentExpression,
|
17
|
+
binaryExpression,
|
18
|
+
blockStatement,
|
19
|
+
callExpression,
|
20
|
+
conditionalExpression,
|
21
|
+
expressionStatement,
|
22
|
+
identifier,
|
23
|
+
isIdentifier,
|
24
|
+
jsxIdentifier,
|
25
|
+
logicalExpression,
|
26
|
+
LOGICAL_OPERATORS,
|
27
|
+
memberExpression,
|
28
|
+
metaProperty,
|
29
|
+
numericLiteral,
|
30
|
+
objectExpression,
|
31
|
+
restElement,
|
32
|
+
returnStatement,
|
33
|
+
sequenceExpression,
|
34
|
+
spreadElement,
|
35
|
+
stringLiteral,
|
36
|
+
super: _super,
|
37
|
+
thisExpression,
|
38
|
+
toExpression,
|
39
|
+
unaryExpression
|
40
|
+
} = _t;
|
36
41
|
function toComputedKey() {
|
37
|
-
const node = this.node;
|
38
42
|
let key;
|
39
|
-
|
40
43
|
if (this.isMemberExpression()) {
|
41
|
-
key = node.property;
|
44
|
+
key = this.node.property;
|
42
45
|
} else if (this.isProperty() || this.isMethod()) {
|
43
|
-
key = node.key;
|
46
|
+
key = this.node.key;
|
44
47
|
} else {
|
45
48
|
throw new ReferenceError("todo");
|
46
49
|
}
|
47
|
-
|
48
|
-
|
49
|
-
if (t().isIdentifier(key)) key = t().stringLiteral(key.name);
|
50
|
+
if (!this.node.computed) {
|
51
|
+
if (isIdentifier(key)) key = stringLiteral(key.name);
|
50
52
|
}
|
51
|
-
|
52
53
|
return key;
|
53
54
|
}
|
54
|
-
|
55
55
|
function ensureBlock() {
|
56
56
|
const body = this.get("body");
|
57
57
|
const bodyNode = body.node;
|
58
|
-
|
59
58
|
if (Array.isArray(body)) {
|
60
59
|
throw new Error("Can't convert array path to a block statement");
|
61
60
|
}
|
62
|
-
|
63
61
|
if (!bodyNode) {
|
64
62
|
throw new Error("Can't convert node without a body");
|
65
63
|
}
|
66
|
-
|
67
64
|
if (body.isBlockStatement()) {
|
68
65
|
return bodyNode;
|
69
66
|
}
|
70
|
-
|
71
67
|
const statements = [];
|
72
68
|
let stringPath = "body";
|
73
69
|
let key;
|
74
70
|
let listKey;
|
75
|
-
|
76
71
|
if (body.isStatement()) {
|
77
72
|
listKey = "body";
|
78
73
|
key = 0;
|
79
74
|
statements.push(body.node);
|
80
75
|
} else {
|
81
76
|
stringPath += ".body.0";
|
82
|
-
|
83
77
|
if (this.isFunction()) {
|
84
78
|
key = "argument";
|
85
|
-
statements.push(
|
79
|
+
statements.push(returnStatement(body.node));
|
86
80
|
} else {
|
87
81
|
key = "expression";
|
88
|
-
statements.push(
|
82
|
+
statements.push(expressionStatement(body.node));
|
89
83
|
}
|
90
84
|
}
|
91
|
-
|
92
|
-
this.node.body = t().blockStatement(statements);
|
85
|
+
this.node.body = blockStatement(statements);
|
93
86
|
const parentPath = this.get(stringPath);
|
94
87
|
body.setup(parentPath, listKey ? parentPath.node[listKey] : parentPath.node, listKey, key);
|
95
88
|
return this.node;
|
96
89
|
}
|
97
|
-
|
98
|
-
function
|
99
|
-
|
100
|
-
|
90
|
+
{
|
91
|
+
exports.arrowFunctionToShadowed = function () {
|
92
|
+
if (!this.isArrowFunctionExpression()) return;
|
93
|
+
this.arrowFunctionToExpression();
|
94
|
+
};
|
101
95
|
}
|
102
|
-
|
103
96
|
function unwrapFunctionEnvironment() {
|
104
97
|
if (!this.isArrowFunctionExpression() && !this.isFunctionExpression() && !this.isFunctionDeclaration()) {
|
105
98
|
throw this.buildCodeFrameError("Can only unwrap the environment of a function.");
|
106
99
|
}
|
107
|
-
|
108
100
|
hoistFunctionEnvironment(this);
|
109
101
|
}
|
110
|
-
|
102
|
+
function setType(path, type) {
|
103
|
+
path.node.type = type;
|
104
|
+
}
|
111
105
|
function arrowFunctionToExpression({
|
112
106
|
allowInsertArrow = true,
|
113
|
-
|
107
|
+
allowInsertArrowWithRest = allowInsertArrow,
|
108
|
+
noNewArrows = !(_arguments$ => (_arguments$ = arguments[0]) == null ? void 0 : _arguments$.specCompliant)()
|
114
109
|
} = {}) {
|
115
110
|
if (!this.isArrowFunctionExpression()) {
|
116
111
|
throw this.buildCodeFrameError("Cannot convert non-arrow function to a function expression.");
|
117
112
|
}
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
113
|
+
const {
|
114
|
+
thisBinding,
|
115
|
+
fnPath: fn
|
116
|
+
} = hoistFunctionEnvironment(this, noNewArrows, allowInsertArrow, allowInsertArrowWithRest);
|
117
|
+
fn.ensureBlock();
|
118
|
+
setType(fn, "FunctionExpression");
|
119
|
+
if (!noNewArrows) {
|
120
|
+
const checkBinding = thisBinding ? null : fn.scope.generateUidIdentifier("arrowCheckId");
|
126
121
|
if (checkBinding) {
|
127
|
-
|
122
|
+
fn.parentPath.scope.push({
|
128
123
|
id: checkBinding,
|
129
|
-
init:
|
124
|
+
init: objectExpression([])
|
130
125
|
});
|
131
126
|
}
|
132
|
-
|
133
|
-
|
134
|
-
|
127
|
+
fn.get("body").unshiftContainer("body", expressionStatement(callExpression(this.hub.addHelper("newArrowCheck"), [thisExpression(), checkBinding ? identifier(checkBinding.name) : identifier(thisBinding)])));
|
128
|
+
fn.replaceWith(callExpression(memberExpression((0, _helperFunctionName.default)(this, true) || fn.node, identifier("bind")), [checkBinding ? identifier(checkBinding.name) : thisExpression()]));
|
129
|
+
return fn.get("callee.object");
|
135
130
|
}
|
131
|
+
return fn;
|
136
132
|
}
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
133
|
+
const getSuperCallsVisitor = (0, _visitors.merge)([{
|
134
|
+
CallExpression(child, {
|
135
|
+
allSuperCalls
|
136
|
+
}) {
|
137
|
+
if (!child.get("callee").isSuper()) return;
|
138
|
+
allSuperCalls.push(child);
|
139
|
+
}
|
140
|
+
}, _helperEnvironmentVisitor.default]);
|
141
|
+
function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow = true, allowInsertArrowWithRest = true) {
|
142
|
+
let arrowParent;
|
143
|
+
let thisEnvFn = fnPath.findParent(p => {
|
144
|
+
if (p.isArrowFunctionExpression()) {
|
145
|
+
var _arrowParent;
|
146
|
+
(_arrowParent = arrowParent) != null ? _arrowParent : arrowParent = p;
|
147
|
+
return false;
|
148
|
+
}
|
149
|
+
return p.isFunction() || p.isProgram() || p.isClassProperty({
|
150
|
+
static: false
|
151
|
+
}) || p.isClassPrivateProperty({
|
141
152
|
static: false
|
142
153
|
});
|
143
154
|
});
|
144
|
-
const inConstructor = thisEnvFn
|
145
|
-
|
146
|
-
|
147
|
-
|
155
|
+
const inConstructor = thisEnvFn.isClassMethod({
|
156
|
+
kind: "constructor"
|
157
|
+
});
|
158
|
+
if (thisEnvFn.isClassProperty() || thisEnvFn.isClassPrivateProperty()) {
|
159
|
+
if (arrowParent) {
|
160
|
+
thisEnvFn = arrowParent;
|
161
|
+
} else if (allowInsertArrow) {
|
162
|
+
fnPath.replaceWith(callExpression(arrowFunctionExpression([], toExpression(fnPath.node)), []));
|
163
|
+
thisEnvFn = fnPath.get("callee");
|
164
|
+
fnPath = thisEnvFn.get("body");
|
165
|
+
} else {
|
166
|
+
throw fnPath.buildCodeFrameError("Unable to transform arrow inside class property");
|
167
|
+
}
|
148
168
|
}
|
149
|
-
|
150
169
|
const {
|
151
170
|
thisPaths,
|
152
171
|
argumentsPaths,
|
@@ -154,236 +173,206 @@ function hoistFunctionEnvironment(fnPath, specCompliant = false, allowInsertArro
|
|
154
173
|
superProps,
|
155
174
|
superCalls
|
156
175
|
} = getScopeInformation(fnPath);
|
157
|
-
|
158
176
|
if (inConstructor && superCalls.length > 0) {
|
159
177
|
if (!allowInsertArrow) {
|
160
|
-
throw superCalls[0].buildCodeFrameError("
|
178
|
+
throw superCalls[0].buildCodeFrameError("When using '@babel/plugin-transform-arrow-functions', " + "it's not possible to compile `super()` in an arrow function without compiling classes.\n" + "Please add '@babel/plugin-transform-classes' to your Babel configuration.");
|
179
|
+
}
|
180
|
+
if (!allowInsertArrowWithRest) {
|
181
|
+
throw superCalls[0].buildCodeFrameError("When using '@babel/plugin-transform-parameters', " + "it's not possible to compile `super()` in an arrow function with default or rest parameters without compiling classes.\n" + "Please add '@babel/plugin-transform-classes' to your Babel configuration.");
|
161
182
|
}
|
162
|
-
|
163
183
|
const allSuperCalls = [];
|
164
|
-
thisEnvFn.traverse({
|
165
|
-
|
166
|
-
if (child.isArrowFunctionExpression()) return;
|
167
|
-
child.skip();
|
168
|
-
},
|
169
|
-
|
170
|
-
ClassProperty(child) {
|
171
|
-
if (child.node.static) return;
|
172
|
-
child.skip();
|
173
|
-
},
|
174
|
-
|
175
|
-
CallExpression(child) {
|
176
|
-
if (!child.get("callee").isSuper()) return;
|
177
|
-
allSuperCalls.push(child);
|
178
|
-
}
|
179
|
-
|
184
|
+
thisEnvFn.traverse(getSuperCallsVisitor, {
|
185
|
+
allSuperCalls
|
180
186
|
});
|
181
187
|
const superBinding = getSuperBinding(thisEnvFn);
|
182
188
|
allSuperCalls.forEach(superCall => {
|
183
|
-
const callee =
|
189
|
+
const callee = identifier(superBinding);
|
184
190
|
callee.loc = superCall.node.callee.loc;
|
185
191
|
superCall.get("callee").replaceWith(callee);
|
186
192
|
});
|
187
193
|
}
|
188
|
-
|
189
|
-
let thisBinding;
|
190
|
-
|
191
|
-
if (thisPaths.length > 0 || specCompliant) {
|
192
|
-
thisBinding = getThisBinding(thisEnvFn, inConstructor);
|
193
|
-
|
194
|
-
if (!specCompliant || inConstructor && hasSuperClass(thisEnvFn)) {
|
195
|
-
thisPaths.forEach(thisChild => {
|
196
|
-
const thisRef = thisChild.isJSX() ? t().jsxIdentifier(thisBinding) : t().identifier(thisBinding);
|
197
|
-
thisRef.loc = thisChild.node.loc;
|
198
|
-
thisChild.replaceWith(thisRef);
|
199
|
-
});
|
200
|
-
if (specCompliant) thisBinding = null;
|
201
|
-
}
|
202
|
-
}
|
203
|
-
|
204
194
|
if (argumentsPaths.length > 0) {
|
205
|
-
const argumentsBinding = getBinding(thisEnvFn, "arguments", () =>
|
195
|
+
const argumentsBinding = getBinding(thisEnvFn, "arguments", () => {
|
196
|
+
const args = () => identifier("arguments");
|
197
|
+
if (thisEnvFn.scope.path.isProgram()) {
|
198
|
+
return conditionalExpression(binaryExpression("===", unaryExpression("typeof", args()), stringLiteral("undefined")), thisEnvFn.scope.buildUndefinedNode(), args());
|
199
|
+
} else {
|
200
|
+
return args();
|
201
|
+
}
|
202
|
+
});
|
206
203
|
argumentsPaths.forEach(argumentsChild => {
|
207
|
-
const argsRef =
|
204
|
+
const argsRef = identifier(argumentsBinding);
|
208
205
|
argsRef.loc = argumentsChild.node.loc;
|
209
206
|
argumentsChild.replaceWith(argsRef);
|
210
207
|
});
|
211
208
|
}
|
212
|
-
|
213
209
|
if (newTargetPaths.length > 0) {
|
214
|
-
const newTargetBinding = getBinding(thisEnvFn, "newtarget", () =>
|
210
|
+
const newTargetBinding = getBinding(thisEnvFn, "newtarget", () => metaProperty(identifier("new"), identifier("target")));
|
215
211
|
newTargetPaths.forEach(targetChild => {
|
216
|
-
const targetRef =
|
212
|
+
const targetRef = identifier(newTargetBinding);
|
217
213
|
targetRef.loc = targetChild.node.loc;
|
218
214
|
targetChild.replaceWith(targetRef);
|
219
215
|
});
|
220
216
|
}
|
221
|
-
|
222
217
|
if (superProps.length > 0) {
|
223
218
|
if (!allowInsertArrow) {
|
224
|
-
throw superProps[0].buildCodeFrameError("
|
219
|
+
throw superProps[0].buildCodeFrameError("When using '@babel/plugin-transform-arrow-functions', " + "it's not possible to compile `super.prop` in an arrow function without compiling classes.\n" + "Please add '@babel/plugin-transform-classes' to your Babel configuration.");
|
225
220
|
}
|
226
|
-
|
227
221
|
const flatSuperProps = superProps.reduce((acc, superProp) => acc.concat(standardizeSuperProperty(superProp)), []);
|
228
222
|
flatSuperProps.forEach(superProp => {
|
229
223
|
const key = superProp.node.computed ? "" : superProp.get("property").node.name;
|
230
|
-
|
231
|
-
|
224
|
+
const superParentPath = superProp.parentPath;
|
225
|
+
const isAssignment = superParentPath.isAssignmentExpression({
|
226
|
+
left: superProp.node
|
227
|
+
});
|
228
|
+
const isCall = superParentPath.isCallExpression({
|
232
229
|
callee: superProp.node
|
233
|
-
})
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
230
|
+
});
|
231
|
+
const isTaggedTemplate = superParentPath.isTaggedTemplateExpression({
|
232
|
+
tag: superProp.node
|
233
|
+
});
|
234
|
+
const superBinding = getSuperPropBinding(thisEnvFn, isAssignment, key);
|
235
|
+
const args = [];
|
236
|
+
if (superProp.node.computed) {
|
237
|
+
args.push(superProp.get("property").node);
|
238
|
+
}
|
239
|
+
if (isAssignment) {
|
240
|
+
const value = superParentPath.node.right;
|
241
|
+
args.push(value);
|
242
|
+
}
|
243
|
+
const call = callExpression(identifier(superBinding), args);
|
244
|
+
if (isCall) {
|
245
|
+
superParentPath.unshiftContainer("arguments", thisExpression());
|
246
|
+
superProp.replaceWith(memberExpression(call, identifier("call")));
|
247
|
+
thisPaths.push(superParentPath.get("arguments.0"));
|
248
|
+
} else if (isAssignment) {
|
249
|
+
superParentPath.replaceWith(call);
|
250
|
+
} else if (isTaggedTemplate) {
|
251
|
+
superProp.replaceWith(callExpression(memberExpression(call, identifier("bind"), false), [thisExpression()]));
|
252
|
+
thisPaths.push(superProp.get("arguments.0"));
|
243
253
|
} else {
|
244
|
-
|
245
|
-
left: superProp.node
|
246
|
-
});
|
247
|
-
const superBinding = getSuperPropBinding(thisEnvFn, isAssignment, key);
|
248
|
-
const args = [];
|
249
|
-
|
250
|
-
if (superProp.node.computed) {
|
251
|
-
args.push(superProp.get("property").node);
|
252
|
-
}
|
253
|
-
|
254
|
-
if (isAssignment) {
|
255
|
-
const value = superProp.parentPath.node.right;
|
256
|
-
args.push(value);
|
257
|
-
superProp.parentPath.replaceWith(t().callExpression(t().identifier(superBinding), args));
|
258
|
-
} else {
|
259
|
-
superProp.replaceWith(t().callExpression(t().identifier(superBinding), args));
|
260
|
-
}
|
254
|
+
superProp.replaceWith(call);
|
261
255
|
}
|
262
256
|
});
|
263
257
|
}
|
264
|
-
|
265
|
-
|
258
|
+
let thisBinding;
|
259
|
+
if (thisPaths.length > 0 || !noNewArrows) {
|
260
|
+
thisBinding = getThisBinding(thisEnvFn, inConstructor);
|
261
|
+
if (noNewArrows || inConstructor && hasSuperClass(thisEnvFn)) {
|
262
|
+
thisPaths.forEach(thisChild => {
|
263
|
+
const thisRef = thisChild.isJSX() ? jsxIdentifier(thisBinding) : identifier(thisBinding);
|
264
|
+
thisRef.loc = thisChild.node.loc;
|
265
|
+
thisChild.replaceWith(thisRef);
|
266
|
+
});
|
267
|
+
if (!noNewArrows) thisBinding = null;
|
268
|
+
}
|
269
|
+
}
|
270
|
+
return {
|
271
|
+
thisBinding,
|
272
|
+
fnPath
|
273
|
+
};
|
274
|
+
}
|
275
|
+
function isLogicalOp(op) {
|
276
|
+
return LOGICAL_OPERATORS.includes(op);
|
266
277
|
}
|
267
|
-
|
268
278
|
function standardizeSuperProperty(superProp) {
|
269
279
|
if (superProp.parentPath.isAssignmentExpression() && superProp.parentPath.node.operator !== "=") {
|
270
280
|
const assignmentPath = superProp.parentPath;
|
271
281
|
const op = assignmentPath.node.operator.slice(0, -1);
|
272
282
|
const value = assignmentPath.node.right;
|
273
|
-
|
274
|
-
|
283
|
+
const isLogicalAssignment = isLogicalOp(op);
|
275
284
|
if (superProp.node.computed) {
|
276
285
|
const tmp = superProp.scope.generateDeclaredUidIdentifier("tmp");
|
277
|
-
|
278
|
-
|
286
|
+
const object = superProp.node.object;
|
287
|
+
const property = superProp.node.property;
|
288
|
+
assignmentPath.get("left").replaceWith(memberExpression(object, assignmentExpression("=", tmp, property), true));
|
289
|
+
assignmentPath.get("right").replaceWith(rightExpression(isLogicalAssignment ? "=" : op, memberExpression(object, identifier(tmp.name), true), value));
|
279
290
|
} else {
|
280
|
-
|
281
|
-
|
291
|
+
const object = superProp.node.object;
|
292
|
+
const property = superProp.node.property;
|
293
|
+
assignmentPath.get("left").replaceWith(memberExpression(object, property));
|
294
|
+
assignmentPath.get("right").replaceWith(rightExpression(isLogicalAssignment ? "=" : op, memberExpression(object, identifier(property.name)), value));
|
295
|
+
}
|
296
|
+
if (isLogicalAssignment) {
|
297
|
+
assignmentPath.replaceWith(logicalExpression(op, assignmentPath.node.left, assignmentPath.node.right));
|
298
|
+
} else {
|
299
|
+
assignmentPath.node.operator = "=";
|
282
300
|
}
|
283
|
-
|
284
301
|
return [assignmentPath.get("left"), assignmentPath.get("right").get("left")];
|
285
302
|
} else if (superProp.parentPath.isUpdateExpression()) {
|
286
303
|
const updateExpr = superProp.parentPath;
|
287
304
|
const tmp = superProp.scope.generateDeclaredUidIdentifier("tmp");
|
288
305
|
const computedKey = superProp.node.computed ? superProp.scope.generateDeclaredUidIdentifier("prop") : null;
|
289
|
-
const parts = [
|
290
|
-
|
306
|
+
const parts = [assignmentExpression("=", tmp, memberExpression(superProp.node.object, computedKey ? assignmentExpression("=", computedKey, superProp.node.property) : superProp.node.property, superProp.node.computed)), assignmentExpression("=", memberExpression(superProp.node.object, computedKey ? identifier(computedKey.name) : superProp.node.property, superProp.node.computed), binaryExpression(superProp.parentPath.node.operator[0], identifier(tmp.name), numericLiteral(1)))];
|
291
307
|
if (!superProp.parentPath.node.prefix) {
|
292
|
-
parts.push(
|
308
|
+
parts.push(identifier(tmp.name));
|
293
309
|
}
|
294
|
-
|
295
|
-
updateExpr.replaceWith(t().sequenceExpression(parts));
|
310
|
+
updateExpr.replaceWith(sequenceExpression(parts));
|
296
311
|
const left = updateExpr.get("expressions.0.right");
|
297
312
|
const right = updateExpr.get("expressions.1.left");
|
298
313
|
return [left, right];
|
299
314
|
}
|
300
|
-
|
301
315
|
return [superProp];
|
316
|
+
function rightExpression(op, left, right) {
|
317
|
+
if (op === "=") {
|
318
|
+
return assignmentExpression("=", left, right);
|
319
|
+
} else {
|
320
|
+
return binaryExpression(op, left, right);
|
321
|
+
}
|
322
|
+
}
|
302
323
|
}
|
303
|
-
|
304
324
|
function hasSuperClass(thisEnvFn) {
|
305
325
|
return thisEnvFn.isClassMethod() && !!thisEnvFn.parentPath.parentPath.node.superClass;
|
306
326
|
}
|
307
|
-
|
327
|
+
const assignSuperThisVisitor = (0, _visitors.merge)([{
|
328
|
+
CallExpression(child, {
|
329
|
+
supers,
|
330
|
+
thisBinding
|
331
|
+
}) {
|
332
|
+
if (!child.get("callee").isSuper()) return;
|
333
|
+
if (supers.has(child.node)) return;
|
334
|
+
supers.add(child.node);
|
335
|
+
child.replaceWithMultiple([child.node, assignmentExpression("=", identifier(thisBinding), identifier("this"))]);
|
336
|
+
}
|
337
|
+
}, _helperEnvironmentVisitor.default]);
|
308
338
|
function getThisBinding(thisEnvFn, inConstructor) {
|
309
339
|
return getBinding(thisEnvFn, "this", thisBinding => {
|
310
|
-
if (!inConstructor || !hasSuperClass(thisEnvFn)) return
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
if (child.isArrowFunctionExpression()) return;
|
315
|
-
child.skip();
|
316
|
-
},
|
317
|
-
|
318
|
-
ClassProperty(child) {
|
319
|
-
if (child.node.static) return;
|
320
|
-
child.skip();
|
321
|
-
},
|
322
|
-
|
323
|
-
CallExpression(child) {
|
324
|
-
if (!child.get("callee").isSuper()) return;
|
325
|
-
if (supers.has(child.node)) return;
|
326
|
-
supers.add(child.node);
|
327
|
-
child.replaceWith(t().assignmentExpression("=", t().identifier(thisBinding), child.node));
|
328
|
-
}
|
329
|
-
|
340
|
+
if (!inConstructor || !hasSuperClass(thisEnvFn)) return thisExpression();
|
341
|
+
thisEnvFn.traverse(assignSuperThisVisitor, {
|
342
|
+
supers: new WeakSet(),
|
343
|
+
thisBinding
|
330
344
|
});
|
331
345
|
});
|
332
346
|
}
|
333
|
-
|
334
347
|
function getSuperBinding(thisEnvFn) {
|
335
348
|
return getBinding(thisEnvFn, "supercall", () => {
|
336
349
|
const argsBinding = thisEnvFn.scope.generateUidIdentifier("args");
|
337
|
-
return
|
338
|
-
});
|
339
|
-
}
|
340
|
-
|
341
|
-
function getSuperPropCallBinding(thisEnvFn, propName) {
|
342
|
-
return getBinding(thisEnvFn, `superprop_call:${propName || ""}`, () => {
|
343
|
-
const argsBinding = thisEnvFn.scope.generateUidIdentifier("args");
|
344
|
-
const argsList = [t().restElement(argsBinding)];
|
345
|
-
let fnBody;
|
346
|
-
|
347
|
-
if (propName) {
|
348
|
-
fnBody = t().callExpression(t().memberExpression(t().super(), t().identifier(propName)), [t().spreadElement(t().identifier(argsBinding.name))]);
|
349
|
-
} else {
|
350
|
-
const method = thisEnvFn.scope.generateUidIdentifier("prop");
|
351
|
-
argsList.unshift(method);
|
352
|
-
fnBody = t().callExpression(t().memberExpression(t().super(), t().identifier(method.name), true), [t().spreadElement(t().identifier(argsBinding.name))]);
|
353
|
-
}
|
354
|
-
|
355
|
-
return t().arrowFunctionExpression(argsList, fnBody);
|
350
|
+
return arrowFunctionExpression([restElement(argsBinding)], callExpression(_super(), [spreadElement(identifier(argsBinding.name))]));
|
356
351
|
});
|
357
352
|
}
|
358
|
-
|
359
353
|
function getSuperPropBinding(thisEnvFn, isAssignment, propName) {
|
360
354
|
const op = isAssignment ? "set" : "get";
|
361
355
|
return getBinding(thisEnvFn, `superprop_${op}:${propName || ""}`, () => {
|
362
356
|
const argsList = [];
|
363
357
|
let fnBody;
|
364
|
-
|
365
358
|
if (propName) {
|
366
|
-
fnBody =
|
359
|
+
fnBody = memberExpression(_super(), identifier(propName));
|
367
360
|
} else {
|
368
361
|
const method = thisEnvFn.scope.generateUidIdentifier("prop");
|
369
362
|
argsList.unshift(method);
|
370
|
-
fnBody =
|
363
|
+
fnBody = memberExpression(_super(), identifier(method.name), true);
|
371
364
|
}
|
372
|
-
|
373
365
|
if (isAssignment) {
|
374
366
|
const valueIdent = thisEnvFn.scope.generateUidIdentifier("value");
|
375
367
|
argsList.push(valueIdent);
|
376
|
-
fnBody =
|
368
|
+
fnBody = assignmentExpression("=", fnBody, identifier(valueIdent.name));
|
377
369
|
}
|
378
|
-
|
379
|
-
return t().arrowFunctionExpression(argsList, fnBody);
|
370
|
+
return arrowFunctionExpression(argsList, fnBody);
|
380
371
|
});
|
381
372
|
}
|
382
|
-
|
383
373
|
function getBinding(thisEnvFn, key, init) {
|
384
374
|
const cacheKey = "binding:" + key;
|
385
375
|
let data = thisEnvFn.getData(cacheKey);
|
386
|
-
|
387
376
|
if (!data) {
|
388
377
|
const id = thisEnvFn.scope.generateUidIdentifier(key);
|
389
378
|
data = id.name;
|
@@ -393,68 +382,79 @@ function getBinding(thisEnvFn, key, init) {
|
|
393
382
|
init: init(data)
|
394
383
|
});
|
395
384
|
}
|
396
|
-
|
397
385
|
return data;
|
398
386
|
}
|
399
|
-
|
387
|
+
const getScopeInformationVisitor = (0, _visitors.merge)([{
|
388
|
+
ThisExpression(child, {
|
389
|
+
thisPaths
|
390
|
+
}) {
|
391
|
+
thisPaths.push(child);
|
392
|
+
},
|
393
|
+
JSXIdentifier(child, {
|
394
|
+
thisPaths
|
395
|
+
}) {
|
396
|
+
if (child.node.name !== "this") return;
|
397
|
+
if (!child.parentPath.isJSXMemberExpression({
|
398
|
+
object: child.node
|
399
|
+
}) && !child.parentPath.isJSXOpeningElement({
|
400
|
+
name: child.node
|
401
|
+
})) {
|
402
|
+
return;
|
403
|
+
}
|
404
|
+
thisPaths.push(child);
|
405
|
+
},
|
406
|
+
CallExpression(child, {
|
407
|
+
superCalls
|
408
|
+
}) {
|
409
|
+
if (child.get("callee").isSuper()) superCalls.push(child);
|
410
|
+
},
|
411
|
+
MemberExpression(child, {
|
412
|
+
superProps
|
413
|
+
}) {
|
414
|
+
if (child.get("object").isSuper()) superProps.push(child);
|
415
|
+
},
|
416
|
+
Identifier(child, {
|
417
|
+
argumentsPaths
|
418
|
+
}) {
|
419
|
+
if (!child.isReferencedIdentifier({
|
420
|
+
name: "arguments"
|
421
|
+
})) return;
|
422
|
+
let curr = child.scope;
|
423
|
+
do {
|
424
|
+
if (curr.hasOwnBinding("arguments")) {
|
425
|
+
curr.rename("arguments");
|
426
|
+
return;
|
427
|
+
}
|
428
|
+
if (curr.path.isFunction() && !curr.path.isArrowFunctionExpression()) {
|
429
|
+
break;
|
430
|
+
}
|
431
|
+
} while (curr = curr.parent);
|
432
|
+
argumentsPaths.push(child);
|
433
|
+
},
|
434
|
+
MetaProperty(child, {
|
435
|
+
newTargetPaths
|
436
|
+
}) {
|
437
|
+
if (!child.get("meta").isIdentifier({
|
438
|
+
name: "new"
|
439
|
+
})) return;
|
440
|
+
if (!child.get("property").isIdentifier({
|
441
|
+
name: "target"
|
442
|
+
})) return;
|
443
|
+
newTargetPaths.push(child);
|
444
|
+
}
|
445
|
+
}, _helperEnvironmentVisitor.default]);
|
400
446
|
function getScopeInformation(fnPath) {
|
401
447
|
const thisPaths = [];
|
402
448
|
const argumentsPaths = [];
|
403
449
|
const newTargetPaths = [];
|
404
450
|
const superProps = [];
|
405
451
|
const superCalls = [];
|
406
|
-
fnPath.traverse({
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
Function(child) {
|
413
|
-
if (child.isArrowFunctionExpression()) return;
|
414
|
-
child.skip();
|
415
|
-
},
|
416
|
-
|
417
|
-
ThisExpression(child) {
|
418
|
-
thisPaths.push(child);
|
419
|
-
},
|
420
|
-
|
421
|
-
JSXIdentifier(child) {
|
422
|
-
if (child.node.name !== "this") return;
|
423
|
-
|
424
|
-
if (!child.parentPath.isJSXMemberExpression({
|
425
|
-
object: child.node
|
426
|
-
}) && !child.parentPath.isJSXOpeningElement({
|
427
|
-
name: child.node
|
428
|
-
})) {
|
429
|
-
return;
|
430
|
-
}
|
431
|
-
|
432
|
-
thisPaths.push(child);
|
433
|
-
},
|
434
|
-
|
435
|
-
CallExpression(child) {
|
436
|
-
if (child.get("callee").isSuper()) superCalls.push(child);
|
437
|
-
},
|
438
|
-
|
439
|
-
MemberExpression(child) {
|
440
|
-
if (child.get("object").isSuper()) superProps.push(child);
|
441
|
-
},
|
442
|
-
|
443
|
-
ReferencedIdentifier(child) {
|
444
|
-
if (child.node.name !== "arguments") return;
|
445
|
-
argumentsPaths.push(child);
|
446
|
-
},
|
447
|
-
|
448
|
-
MetaProperty(child) {
|
449
|
-
if (!child.get("meta").isIdentifier({
|
450
|
-
name: "new"
|
451
|
-
})) return;
|
452
|
-
if (!child.get("property").isIdentifier({
|
453
|
-
name: "target"
|
454
|
-
})) return;
|
455
|
-
newTargetPaths.push(child);
|
456
|
-
}
|
457
|
-
|
452
|
+
fnPath.traverse(getScopeInformationVisitor, {
|
453
|
+
thisPaths,
|
454
|
+
argumentsPaths,
|
455
|
+
newTargetPaths,
|
456
|
+
superProps,
|
457
|
+
superCalls
|
458
458
|
});
|
459
459
|
return {
|
460
460
|
thisPaths,
|
@@ -463,4 +463,6 @@ function getScopeInformation(fnPath) {
|
|
463
463
|
superProps,
|
464
464
|
superCalls
|
465
465
|
};
|
466
|
-
}
|
466
|
+
}
|
467
|
+
|
468
|
+
//# sourceMappingURL=conversion.js.map
|