@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.
Files changed (61) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +1 -1
  3. package/lib/cache.js +27 -7
  4. package/lib/cache.js.map +1 -0
  5. package/lib/context.js +22 -59
  6. package/lib/context.js.map +1 -0
  7. package/lib/hub.js +2 -6
  8. package/lib/hub.js.map +1 -0
  9. package/lib/index.js +42 -77
  10. package/lib/index.js.map +1 -0
  11. package/lib/path/ancestry.js +15 -62
  12. package/lib/path/ancestry.js.map +1 -0
  13. package/lib/path/comments.js +31 -24
  14. package/lib/path/comments.js.map +1 -0
  15. package/lib/path/context.js +68 -91
  16. package/lib/path/context.js.map +1 -0
  17. package/lib/path/conversion.js +275 -273
  18. package/lib/path/conversion.js.map +1 -0
  19. package/lib/path/evaluation.js +84 -149
  20. package/lib/path/evaluation.js.map +1 -0
  21. package/lib/path/family.js +189 -94
  22. package/lib/path/family.js.map +1 -0
  23. package/lib/path/index.js +106 -132
  24. package/lib/path/index.js.map +1 -0
  25. package/lib/path/inference/index.js +81 -64
  26. package/lib/path/inference/index.js.map +1 -0
  27. package/lib/path/inference/inferer-reference.js +22 -52
  28. package/lib/path/inference/inferer-reference.js.map +1 -0
  29. package/lib/path/inference/inferers.js +110 -125
  30. package/lib/path/inference/inferers.js.map +1 -0
  31. package/lib/path/inference/util.js +30 -0
  32. package/lib/path/inference/util.js.map +1 -0
  33. package/lib/path/introspection.js +182 -168
  34. package/lib/path/introspection.js.map +1 -0
  35. package/lib/path/lib/hoister.js +37 -54
  36. package/lib/path/lib/hoister.js.map +1 -0
  37. package/lib/path/lib/removal-hooks.js +4 -4
  38. package/lib/path/lib/removal-hooks.js.map +1 -0
  39. package/lib/path/lib/virtual-types-validator.js +161 -0
  40. package/lib/path/lib/virtual-types-validator.js.map +1 -0
  41. package/lib/path/lib/virtual-types.js +21 -189
  42. package/lib/path/lib/virtual-types.js.map +1 -0
  43. package/lib/path/modification.js +103 -98
  44. package/lib/path/modification.js.map +1 -0
  45. package/lib/path/removal.js +18 -23
  46. package/lib/path/removal.js.map +1 -0
  47. package/lib/path/replacement.js +91 -144
  48. package/lib/path/replacement.js.map +1 -0
  49. package/lib/scope/binding.js +28 -16
  50. package/lib/scope/binding.js.map +1 -0
  51. package/lib/scope/index.js +407 -414
  52. package/lib/scope/index.js.map +1 -0
  53. package/lib/scope/lib/renamer.js +45 -70
  54. package/lib/scope/lib/renamer.js.map +1 -0
  55. package/lib/traverse-node.js +29 -0
  56. package/lib/traverse-node.js.map +1 -0
  57. package/lib/types.js +3 -0
  58. package/lib/types.js.map +1 -0
  59. package/lib/visitors.js +77 -113
  60. package/lib/visitors.js.map +1 -0
  61. package/package.json +28 -17
@@ -3,150 +3,169 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.toComputedKey = toComputedKey;
6
+ exports.arrowFunctionToExpression = arrowFunctionToExpression;
7
7
  exports.ensureBlock = ensureBlock;
8
- exports.arrowFunctionToShadowed = arrowFunctionToShadowed;
8
+ exports.toComputedKey = toComputedKey;
9
9
  exports.unwrapFunctionEnvironment = unwrapFunctionEnvironment;
10
- exports.arrowFunctionToExpression = arrowFunctionToExpression;
11
-
12
- function t() {
13
- const data = _interopRequireWildcard(require("@babel/types"));
14
-
15
- t = function () {
16
- return data;
17
- };
18
-
19
- return data;
20
- }
21
-
22
- function _helperFunctionName() {
23
- const data = _interopRequireDefault(require("@babel/helper-function-name"));
24
-
25
- _helperFunctionName = function () {
26
- return data;
27
- };
28
-
29
- return data;
30
- }
31
-
32
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
33
-
34
- 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; } }
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
- if (!node.computed) {
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(t().returnStatement(body.node));
79
+ statements.push(returnStatement(body.node));
86
80
  } else {
87
81
  key = "expression";
88
- statements.push(t().expressionStatement(body.node));
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 arrowFunctionToShadowed() {
99
- if (!this.isArrowFunctionExpression()) return;
100
- this.arrowFunctionToExpression();
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
- specCompliant = false
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
- const thisBinding = hoistFunctionEnvironment(this, specCompliant, allowInsertArrow);
120
- this.ensureBlock();
121
- this.node.type = "FunctionExpression";
122
-
123
- if (specCompliant) {
124
- const checkBinding = thisBinding ? null : this.parentPath.scope.generateUidIdentifier("arrowCheckId");
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
- this.parentPath.scope.push({
122
+ fn.parentPath.scope.push({
128
123
  id: checkBinding,
129
- init: t().objectExpression([])
124
+ init: objectExpression([])
130
125
  });
131
126
  }
132
-
133
- this.get("body").unshiftContainer("body", t().expressionStatement(t().callExpression(this.hub.addHelper("newArrowCheck"), [t().thisExpression(), checkBinding ? t().identifier(checkBinding.name) : t().identifier(thisBinding)])));
134
- this.replaceWith(t().callExpression(t().memberExpression((0, _helperFunctionName().default)(this, true) || this.node, t().identifier("bind")), [checkBinding ? t().identifier(checkBinding.name) : t().thisExpression()]));
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
- function hoistFunctionEnvironment(fnPath, specCompliant = false, allowInsertArrow = true) {
139
- const thisEnvFn = fnPath.findParent(p => {
140
- return p.isFunction() && !p.isArrowFunctionExpression() || p.isProgram() || p.isClassProperty({
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 && thisEnvFn.node.kind === "constructor";
145
-
146
- if (thisEnvFn.isClassProperty()) {
147
- throw fnPath.buildCodeFrameError("Unable to transform arrow inside class property");
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("Unable to handle nested super() usage in arrow");
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
- Function(child) {
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 = t().identifier(superBinding);
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", () => t().identifier("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 = t().identifier(argumentsBinding);
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", () => t().metaProperty(t().identifier("new"), t().identifier("target")));
210
+ const newTargetBinding = getBinding(thisEnvFn, "newtarget", () => metaProperty(identifier("new"), identifier("target")));
215
211
  newTargetPaths.forEach(targetChild => {
216
- const targetRef = t().identifier(newTargetBinding);
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("Unable to handle nested super.prop usage");
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
- if (superProp.parentPath.isCallExpression({
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
- const superBinding = getSuperPropCallBinding(thisEnvFn, key);
235
-
236
- if (superProp.node.computed) {
237
- const prop = superProp.get("property").node;
238
- superProp.replaceWith(t().identifier(superBinding));
239
- superProp.parentPath.node.arguments.unshift(prop);
240
- } else {
241
- superProp.replaceWith(t().identifier(superBinding));
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
- const isAssignment = superProp.parentPath.isAssignmentExpression({
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
- return thisBinding;
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
- assignmentPath.node.operator = "=";
274
-
283
+ const isLogicalAssignment = isLogicalOp(op);
275
284
  if (superProp.node.computed) {
276
285
  const tmp = superProp.scope.generateDeclaredUidIdentifier("tmp");
277
- assignmentPath.get("left").replaceWith(t().memberExpression(superProp.node.object, t().assignmentExpression("=", tmp, superProp.node.property), true));
278
- assignmentPath.get("right").replaceWith(t().binaryExpression(op, t().memberExpression(superProp.node.object, t().identifier(tmp.name), true), value));
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
- assignmentPath.get("left").replaceWith(t().memberExpression(superProp.node.object, superProp.node.property));
281
- assignmentPath.get("right").replaceWith(t().binaryExpression(op, t().memberExpression(superProp.node.object, t().identifier(superProp.node.property.name)), value));
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 = [t().assignmentExpression("=", tmp, t().memberExpression(superProp.node.object, computedKey ? t().assignmentExpression("=", computedKey, superProp.node.property) : superProp.node.property, superProp.node.computed)), t().assignmentExpression("=", t().memberExpression(superProp.node.object, computedKey ? t().identifier(computedKey.name) : superProp.node.property, superProp.node.computed), t().binaryExpression("+", t().identifier(tmp.name), t().numericLiteral(1)))];
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(t().identifier(tmp.name));
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 t().thisExpression();
311
- const supers = new WeakSet();
312
- thisEnvFn.traverse({
313
- Function(child) {
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 t().arrowFunctionExpression([t().restElement(argsBinding)], t().callExpression(t().super(), [t().spreadElement(t().identifier(argsBinding.name))]));
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 = t().memberExpression(t().super(), t().identifier(propName));
359
+ fnBody = memberExpression(_super(), identifier(propName));
367
360
  } else {
368
361
  const method = thisEnvFn.scope.generateUidIdentifier("prop");
369
362
  argsList.unshift(method);
370
- fnBody = t().memberExpression(t().super(), t().identifier(method.name), true);
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 = t().assignmentExpression("=", fnBody, t().identifier(valueIdent.name));
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
- ClassProperty(child) {
408
- if (child.node.static) return;
409
- child.skip();
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