source: trip-planner-front/node_modules/@babel/plugin-transform-destructuring/lib/index.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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