1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 |
|
5 | var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
|
6 |
|
7 | var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
8 |
|
9 | var _getIterator2 = require("babel-runtime/core-js/get-iterator");
|
10 |
|
11 | var _getIterator3 = _interopRequireDefault(_getIterator2);
|
12 |
|
13 | exports.default = function (_ref) {
|
14 | var t = _ref.types;
|
15 |
|
16 |
|
17 | function variableDeclarationHasPattern(node) {
|
18 | for (var _iterator = node.declarations, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
|
19 | var _ref2;
|
20 |
|
21 | if (_isArray) {
|
22 | if (_i >= _iterator.length) break;
|
23 | _ref2 = _iterator[_i++];
|
24 | } else {
|
25 | _i = _iterator.next();
|
26 | if (_i.done) break;
|
27 | _ref2 = _i.value;
|
28 | }
|
29 |
|
30 | var declar = _ref2;
|
31 |
|
32 | if (t.isPattern(declar.id)) {
|
33 | return true;
|
34 | }
|
35 | }
|
36 | return false;
|
37 | }
|
38 |
|
39 | function hasRest(pattern) {
|
40 | for (var _iterator2 = pattern.elements, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
|
41 | var _ref3;
|
42 |
|
43 | if (_isArray2) {
|
44 | if (_i2 >= _iterator2.length) break;
|
45 | _ref3 = _iterator2[_i2++];
|
46 | } else {
|
47 | _i2 = _iterator2.next();
|
48 | if (_i2.done) break;
|
49 | _ref3 = _i2.value;
|
50 | }
|
51 |
|
52 | var elem = _ref3;
|
53 |
|
54 | if (t.isRestElement(elem)) {
|
55 | return true;
|
56 | }
|
57 | }
|
58 | return false;
|
59 | }
|
60 |
|
61 | var arrayUnpackVisitor = {
|
62 | ReferencedIdentifier: function ReferencedIdentifier(path, state) {
|
63 | if (state.bindings[path.node.name]) {
|
64 | state.deopt = true;
|
65 | path.stop();
|
66 | }
|
67 | }
|
68 | };
|
69 |
|
70 | var DestructuringTransformer = function () {
|
71 | function DestructuringTransformer(opts) {
|
72 | (0, _classCallCheck3.default)(this, DestructuringTransformer);
|
73 |
|
74 | this.blockHoist = opts.blockHoist;
|
75 | this.operator = opts.operator;
|
76 | this.arrays = {};
|
77 | this.nodes = opts.nodes || [];
|
78 | this.scope = opts.scope;
|
79 | this.file = opts.file;
|
80 | this.kind = opts.kind;
|
81 | }
|
82 |
|
83 | DestructuringTransformer.prototype.buildVariableAssignment = function buildVariableAssignment(id, init) {
|
84 | var op = this.operator;
|
85 | if (t.isMemberExpression(id)) op = "=";
|
86 |
|
87 | var node = void 0;
|
88 |
|
89 | if (op) {
|
90 | node = t.expressionStatement(t.assignmentExpression(op, id, init));
|
91 | } else {
|
92 | node = t.variableDeclaration(this.kind, [t.variableDeclarator(id, init)]);
|
93 | }
|
94 |
|
95 | node._blockHoist = this.blockHoist;
|
96 |
|
97 | return node;
|
98 | };
|
99 |
|
100 | DestructuringTransformer.prototype.buildVariableDeclaration = function buildVariableDeclaration(id, init) {
|
101 | var declar = t.variableDeclaration("var", [t.variableDeclarator(id, init)]);
|
102 | declar._blockHoist = this.blockHoist;
|
103 | return declar;
|
104 | };
|
105 |
|
106 | DestructuringTransformer.prototype.push = function push(id, init) {
|
107 | if (t.isObjectPattern(id)) {
|
108 | this.pushObjectPattern(id, init);
|
109 | } else if (t.isArrayPattern(id)) {
|
110 | this.pushArrayPattern(id, init);
|
111 | } else if (t.isAssignmentPattern(id)) {
|
112 | this.pushAssignmentPattern(id, init);
|
113 | } else {
|
114 | this.nodes.push(this.buildVariableAssignment(id, init));
|
115 | }
|
116 | };
|
117 |
|
118 | DestructuringTransformer.prototype.toArray = function toArray(node, count) {
|
119 | if (this.file.opts.loose || t.isIdentifier(node) && this.arrays[node.name]) {
|
120 | return node;
|
121 | } else {
|
122 | return this.scope.toArray(node, count);
|
123 | }
|
124 | };
|
125 |
|
126 | DestructuringTransformer.prototype.pushAssignmentPattern = function pushAssignmentPattern(pattern, valueRef) {
|
127 |
|
128 | var tempValueRef = this.scope.generateUidIdentifierBasedOnNode(valueRef);
|
129 |
|
130 | var declar = t.variableDeclaration("var", [t.variableDeclarator(tempValueRef, valueRef)]);
|
131 | declar._blockHoist = this.blockHoist;
|
132 | this.nodes.push(declar);
|
133 |
|
134 | var tempConditional = t.conditionalExpression(t.binaryExpression("===", tempValueRef, t.identifier("undefined")), pattern.right, tempValueRef);
|
135 |
|
136 | var left = pattern.left;
|
137 | if (t.isPattern(left)) {
|
138 | var tempValueDefault = t.expressionStatement(t.assignmentExpression("=", tempValueRef, tempConditional));
|
139 | tempValueDefault._blockHoist = this.blockHoist;
|
140 |
|
141 | this.nodes.push(tempValueDefault);
|
142 | this.push(left, tempValueRef);
|
143 | } else {
|
144 | this.nodes.push(this.buildVariableAssignment(left, tempConditional));
|
145 | }
|
146 | };
|
147 |
|
148 | DestructuringTransformer.prototype.pushObjectRest = function pushObjectRest(pattern, objRef, spreadProp, spreadPropIndex) {
|
149 |
|
150 | var keys = [];
|
151 |
|
152 | for (var i = 0; i < pattern.properties.length; i++) {
|
153 | var prop = pattern.properties[i];
|
154 |
|
155 | if (i >= spreadPropIndex) break;
|
156 |
|
157 | if (t.isRestProperty(prop)) continue;
|
158 |
|
159 | var key = prop.key;
|
160 | if (t.isIdentifier(key) && !prop.computed) key = t.stringLiteral(prop.key.name);
|
161 | keys.push(key);
|
162 | }
|
163 |
|
164 | keys = t.arrayExpression(keys);
|
165 |
|
166 | var value = t.callExpression(this.file.addHelper("objectWithoutProperties"), [objRef, keys]);
|
167 | this.nodes.push(this.buildVariableAssignment(spreadProp.argument, value));
|
168 | };
|
169 |
|
170 | DestructuringTransformer.prototype.pushObjectProperty = function pushObjectProperty(prop, propRef) {
|
171 | if (t.isLiteral(prop.key)) prop.computed = true;
|
172 |
|
173 | var pattern = prop.value;
|
174 | var objRef = t.memberExpression(propRef, prop.key, prop.computed);
|
175 |
|
176 | if (t.isPattern(pattern)) {
|
177 | this.push(pattern, objRef);
|
178 | } else {
|
179 | this.nodes.push(this.buildVariableAssignment(pattern, objRef));
|
180 | }
|
181 | };
|
182 |
|
183 | DestructuringTransformer.prototype.pushObjectPattern = function pushObjectPattern(pattern, objRef) {
|
184 |
|
185 | if (!pattern.properties.length) {
|
186 | this.nodes.push(t.expressionStatement(t.callExpression(this.file.addHelper("objectDestructuringEmpty"), [objRef])));
|
187 | }
|
188 |
|
189 | if (pattern.properties.length > 1 && !this.scope.isStatic(objRef)) {
|
190 | var temp = this.scope.generateUidIdentifierBasedOnNode(objRef);
|
191 | this.nodes.push(this.buildVariableDeclaration(temp, objRef));
|
192 | objRef = temp;
|
193 | }
|
194 |
|
195 | for (var i = 0; i < pattern.properties.length; i++) {
|
196 | var prop = pattern.properties[i];
|
197 | if (t.isRestProperty(prop)) {
|
198 | this.pushObjectRest(pattern, objRef, prop, i);
|
199 | } else {
|
200 | this.pushObjectProperty(prop, objRef);
|
201 | }
|
202 | }
|
203 | };
|
204 |
|
205 | DestructuringTransformer.prototype.canUnpackArrayPattern = function canUnpackArrayPattern(pattern, arr) {
|
206 | if (!t.isArrayExpression(arr)) return false;
|
207 |
|
208 | if (pattern.elements.length > arr.elements.length) return;
|
209 | if (pattern.elements.length < arr.elements.length && !hasRest(pattern)) return false;
|
210 |
|
211 | for (var _iterator3 = pattern.elements, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : (0, _getIterator3.default)(_iterator3);;) {
|
212 | var _ref4;
|
213 |
|
214 | if (_isArray3) {
|
215 | if (_i3 >= _iterator3.length) break;
|
216 | _ref4 = _iterator3[_i3++];
|
217 | } else {
|
218 | _i3 = _iterator3.next();
|
219 | if (_i3.done) break;
|
220 | _ref4 = _i3.value;
|
221 | }
|
222 |
|
223 | var elem = _ref4;
|
224 |
|
225 | if (!elem) return false;
|
226 |
|
227 | if (t.isMemberExpression(elem)) return false;
|
228 | }
|
229 |
|
230 | for (var _iterator4 = arr.elements, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : (0, _getIterator3.default)(_iterator4);;) {
|
231 | var _ref5;
|
232 |
|
233 | if (_isArray4) {
|
234 | if (_i4 >= _iterator4.length) break;
|
235 | _ref5 = _iterator4[_i4++];
|
236 | } else {
|
237 | _i4 = _iterator4.next();
|
238 | if (_i4.done) break;
|
239 | _ref5 = _i4.value;
|
240 | }
|
241 |
|
242 | var _elem = _ref5;
|
243 |
|
244 | if (t.isSpreadElement(_elem)) return false;
|
245 |
|
246 | if (t.isCallExpression(_elem)) return false;
|
247 |
|
248 | if (t.isMemberExpression(_elem)) return false;
|
249 | }
|
250 |
|
251 | var bindings = t.getBindingIdentifiers(pattern);
|
252 | var state = { deopt: false, bindings: bindings };
|
253 | this.scope.traverse(arr, arrayUnpackVisitor, state);
|
254 | return !state.deopt;
|
255 | };
|
256 |
|
257 | DestructuringTransformer.prototype.pushUnpackedArrayPattern = function pushUnpackedArrayPattern(pattern, arr) {
|
258 | for (var i = 0; i < pattern.elements.length; i++) {
|
259 | var elem = pattern.elements[i];
|
260 | if (t.isRestElement(elem)) {
|
261 | this.push(elem.argument, t.arrayExpression(arr.elements.slice(i)));
|
262 | } else {
|
263 | this.push(elem, arr.elements[i]);
|
264 | }
|
265 | }
|
266 | };
|
267 |
|
268 | DestructuringTransformer.prototype.pushArrayPattern = function pushArrayPattern(pattern, arrayRef) {
|
269 | if (!pattern.elements) return;
|
270 |
|
271 | if (this.canUnpackArrayPattern(pattern, arrayRef)) {
|
272 | return this.pushUnpackedArrayPattern(pattern, arrayRef);
|
273 | }
|
274 |
|
275 | var count = !hasRest(pattern) && pattern.elements.length;
|
276 |
|
277 | var toArray = this.toArray(arrayRef, count);
|
278 |
|
279 | if (t.isIdentifier(toArray)) {
|
280 | arrayRef = toArray;
|
281 | } else {
|
282 | arrayRef = this.scope.generateUidIdentifierBasedOnNode(arrayRef);
|
283 | this.arrays[arrayRef.name] = true;
|
284 | this.nodes.push(this.buildVariableDeclaration(arrayRef, toArray));
|
285 | }
|
286 |
|
287 | for (var i = 0; i < pattern.elements.length; i++) {
|
288 | var elem = pattern.elements[i];
|
289 |
|
290 | if (!elem) continue;
|
291 |
|
292 | var elemRef = void 0;
|
293 |
|
294 | if (t.isRestElement(elem)) {
|
295 | elemRef = this.toArray(arrayRef);
|
296 | elemRef = t.callExpression(t.memberExpression(elemRef, t.identifier("slice")), [t.numericLiteral(i)]);
|
297 |
|
298 | elem = elem.argument;
|
299 | } else {
|
300 | elemRef = t.memberExpression(arrayRef, t.numericLiteral(i), true);
|
301 | }
|
302 |
|
303 | this.push(elem, elemRef);
|
304 | }
|
305 | };
|
306 |
|
307 | DestructuringTransformer.prototype.init = function init(pattern, ref) {
|
308 |
|
309 | if (!t.isArrayExpression(ref) && !t.isMemberExpression(ref)) {
|
310 | var memo = this.scope.maybeGenerateMemoised(ref, true);
|
311 | if (memo) {
|
312 | this.nodes.push(this.buildVariableDeclaration(memo, ref));
|
313 | ref = memo;
|
314 | }
|
315 | }
|
316 |
|
317 | this.push(pattern, ref);
|
318 |
|
319 | return this.nodes;
|
320 | };
|
321 |
|
322 | return DestructuringTransformer;
|
323 | }();
|
324 |
|
325 | return {
|
326 | visitor: {
|
327 | ExportNamedDeclaration: function ExportNamedDeclaration(path) {
|
328 | var declaration = path.get("declaration");
|
329 | if (!declaration.isVariableDeclaration()) return;
|
330 | if (!variableDeclarationHasPattern(declaration.node)) return;
|
331 |
|
332 | var specifiers = [];
|
333 |
|
334 | for (var name in path.getOuterBindingIdentifiers(path)) {
|
335 | var id = t.identifier(name);
|
336 | specifiers.push(t.exportSpecifier(id, id));
|
337 | }
|
338 |
|
339 | path.replaceWith(declaration.node);
|
340 | path.insertAfter(t.exportNamedDeclaration(null, specifiers));
|
341 | },
|
342 | ForXStatement: function ForXStatement(path, file) {
|
343 | var node = path.node,
|
344 | scope = path.scope;
|
345 |
|
346 | var left = node.left;
|
347 |
|
348 | if (t.isPattern(left)) {
|
349 |
|
350 | var temp = scope.generateUidIdentifier("ref");
|
351 |
|
352 | node.left = t.variableDeclaration("var", [t.variableDeclarator(temp)]);
|
353 |
|
354 | path.ensureBlock();
|
355 |
|
356 | node.body.body.unshift(t.variableDeclaration("var", [t.variableDeclarator(left, temp)]));
|
357 |
|
358 | return;
|
359 | }
|
360 |
|
361 | if (!t.isVariableDeclaration(left)) return;
|
362 |
|
363 | var pattern = left.declarations[0].id;
|
364 | if (!t.isPattern(pattern)) return;
|
365 |
|
366 | var key = scope.generateUidIdentifier("ref");
|
367 | node.left = t.variableDeclaration(left.kind, [t.variableDeclarator(key, null)]);
|
368 |
|
369 | var nodes = [];
|
370 |
|
371 | var destructuring = new DestructuringTransformer({
|
372 | kind: left.kind,
|
373 | file: file,
|
374 | scope: scope,
|
375 | nodes: nodes
|
376 | });
|
377 |
|
378 | destructuring.init(pattern, key);
|
379 |
|
380 | path.ensureBlock();
|
381 |
|
382 | var block = node.body;
|
383 | block.body = nodes.concat(block.body);
|
384 | },
|
385 | CatchClause: function CatchClause(_ref6, file) {
|
386 | var node = _ref6.node,
|
387 | scope = _ref6.scope;
|
388 |
|
389 | var pattern = node.param;
|
390 | if (!t.isPattern(pattern)) return;
|
391 |
|
392 | var ref = scope.generateUidIdentifier("ref");
|
393 | node.param = ref;
|
394 |
|
395 | var nodes = [];
|
396 |
|
397 | var destructuring = new DestructuringTransformer({
|
398 | kind: "let",
|
399 | file: file,
|
400 | scope: scope,
|
401 | nodes: nodes
|
402 | });
|
403 | destructuring.init(pattern, ref);
|
404 |
|
405 | node.body.body = nodes.concat(node.body.body);
|
406 | },
|
407 | AssignmentExpression: function AssignmentExpression(path, file) {
|
408 | var node = path.node,
|
409 | scope = path.scope;
|
410 |
|
411 | if (!t.isPattern(node.left)) return;
|
412 |
|
413 | var nodes = [];
|
414 |
|
415 | var destructuring = new DestructuringTransformer({
|
416 | operator: node.operator,
|
417 | file: file,
|
418 | scope: scope,
|
419 | nodes: nodes
|
420 | });
|
421 |
|
422 | var ref = void 0;
|
423 | if (path.isCompletionRecord() || !path.parentPath.isExpressionStatement()) {
|
424 | ref = scope.generateUidIdentifierBasedOnNode(node.right, "ref");
|
425 |
|
426 | nodes.push(t.variableDeclaration("var", [t.variableDeclarator(ref, node.right)]));
|
427 |
|
428 | if (t.isArrayExpression(node.right)) {
|
429 | destructuring.arrays[ref.name] = true;
|
430 | }
|
431 | }
|
432 |
|
433 | destructuring.init(node.left, ref || node.right);
|
434 |
|
435 | if (ref) {
|
436 | nodes.push(t.expressionStatement(ref));
|
437 | }
|
438 |
|
439 | path.replaceWithMultiple(nodes);
|
440 | },
|
441 | VariableDeclaration: function VariableDeclaration(path, file) {
|
442 | var node = path.node,
|
443 | scope = path.scope,
|
444 | parent = path.parent;
|
445 |
|
446 | if (t.isForXStatement(parent)) return;
|
447 | if (!parent || !path.container) return;
|
448 | if (!variableDeclarationHasPattern(node)) return;
|
449 |
|
450 | var nodes = [];
|
451 | var declar = void 0;
|
452 |
|
453 | for (var i = 0; i < node.declarations.length; i++) {
|
454 | declar = node.declarations[i];
|
455 |
|
456 | var patternId = declar.init;
|
457 | var pattern = declar.id;
|
458 |
|
459 | var destructuring = new DestructuringTransformer({
|
460 | blockHoist: node._blockHoist,
|
461 | nodes: nodes,
|
462 | scope: scope,
|
463 | kind: node.kind,
|
464 | file: file
|
465 | });
|
466 |
|
467 | if (t.isPattern(pattern)) {
|
468 | destructuring.init(pattern, patternId);
|
469 |
|
470 | if (+i !== node.declarations.length - 1) {
|
471 | t.inherits(nodes[nodes.length - 1], declar);
|
472 | }
|
473 | } else {
|
474 | nodes.push(t.inherits(destructuring.buildVariableAssignment(declar.id, declar.init), declar));
|
475 | }
|
476 | }
|
477 |
|
478 | var nodesOut = [];
|
479 | for (var _iterator5 = nodes, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : (0, _getIterator3.default)(_iterator5);;) {
|
480 | var _ref7;
|
481 |
|
482 | if (_isArray5) {
|
483 | if (_i5 >= _iterator5.length) break;
|
484 | _ref7 = _iterator5[_i5++];
|
485 | } else {
|
486 | _i5 = _iterator5.next();
|
487 | if (_i5.done) break;
|
488 | _ref7 = _i5.value;
|
489 | }
|
490 |
|
491 | var _node = _ref7;
|
492 |
|
493 | var tail = nodesOut[nodesOut.length - 1];
|
494 | if (tail && t.isVariableDeclaration(tail) && t.isVariableDeclaration(_node) && tail.kind === _node.kind) {
|
495 | var _tail$declarations;
|
496 |
|
497 | (_tail$declarations = tail.declarations).push.apply(_tail$declarations, _node.declarations);
|
498 | } else {
|
499 | nodesOut.push(_node);
|
500 | }
|
501 | }
|
502 |
|
503 | for (var _iterator6 = nodesOut, _isArray6 = Array.isArray(_iterator6), _i6 = 0, _iterator6 = _isArray6 ? _iterator6 : (0, _getIterator3.default)(_iterator6);;) {
|
504 | var _ref8;
|
505 |
|
506 | if (_isArray6) {
|
507 | if (_i6 >= _iterator6.length) break;
|
508 | _ref8 = _iterator6[_i6++];
|
509 | } else {
|
510 | _i6 = _iterator6.next();
|
511 | if (_i6.done) break;
|
512 | _ref8 = _i6.value;
|
513 | }
|
514 |
|
515 | var nodeOut = _ref8;
|
516 |
|
517 | if (!nodeOut.declarations) continue;
|
518 | for (var _iterator7 = nodeOut.declarations, _isArray7 = Array.isArray(_iterator7), _i7 = 0, _iterator7 = _isArray7 ? _iterator7 : (0, _getIterator3.default)(_iterator7);;) {
|
519 | var _ref9;
|
520 |
|
521 | if (_isArray7) {
|
522 | if (_i7 >= _iterator7.length) break;
|
523 | _ref9 = _iterator7[_i7++];
|
524 | } else {
|
525 | _i7 = _iterator7.next();
|
526 | if (_i7.done) break;
|
527 | _ref9 = _i7.value;
|
528 | }
|
529 |
|
530 | var declaration = _ref9;
|
531 | var name = declaration.id.name;
|
532 |
|
533 | if (scope.bindings[name]) {
|
534 | scope.bindings[name].kind = nodeOut.kind;
|
535 | }
|
536 | }
|
537 | }
|
538 |
|
539 | if (nodesOut.length === 1) {
|
540 | path.replaceWith(nodesOut[0]);
|
541 | } else {
|
542 | path.replaceWithMultiple(nodesOut);
|
543 | }
|
544 | }
|
545 | }
|
546 | };
|
547 | };
|
548 |
|
549 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
550 |
|
551 | module.exports = exports["default"]; |
\ | No newline at end of file |