source: trip-planner-front/node_modules/@babel/plugin-proposal-object-rest-spread/lib/index.js@ 84d0fbb

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

initial commit

  • Property mode set to 100644
File size: 17.1 KB
RevLine 
[6a3a178]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 _pluginSyntaxObjectRestSpread = require("@babel/plugin-syntax-object-rest-spread");
11
12var _core = require("@babel/core");
13
14var _pluginTransformParameters = require("@babel/plugin-transform-parameters");
15
16var _helperCompilationTargets = require("@babel/helper-compilation-targets");
17
18var _corejs2BuiltIns = require("@babel/compat-data/corejs2-built-ins");
19
20var _shouldStoreRHSInTemporaryVariable = require("./shouldStoreRHSInTemporaryVariable");
21
22const ZERO_REFS = (() => {
23 const node = _core.types.identifier("a");
24
25 const property = _core.types.objectProperty(_core.types.identifier("key"), node);
26
27 const pattern = _core.types.objectPattern([property]);
28
29 return _core.types.isReferenced(node, property, pattern) ? 1 : 0;
30})();
31
32var _default = (0, _helperPluginUtils.declare)((api, opts) => {
33 var _api$assumption, _api$assumption2, _api$assumption3, _api$assumption4;
34
35 api.assertVersion(7);
36 const targets = api.targets();
37 const supportsObjectAssign = !(0, _helperCompilationTargets.isRequired)("es6.object.assign", targets, {
38 compatData: _corejs2BuiltIns
39 });
40 const {
41 useBuiltIns = supportsObjectAssign,
42 loose = false
43 } = opts;
44
45 if (typeof loose !== "boolean") {
46 throw new Error(".loose must be a boolean, or undefined");
47 }
48
49 const ignoreFunctionLength = (_api$assumption = api.assumption("ignoreFunctionLength")) != null ? _api$assumption : loose;
50 const objectRestNoSymbols = (_api$assumption2 = api.assumption("objectRestNoSymbols")) != null ? _api$assumption2 : loose;
51 const pureGetters = (_api$assumption3 = api.assumption("pureGetters")) != null ? _api$assumption3 : loose;
52 const setSpreadProperties = (_api$assumption4 = api.assumption("setSpreadProperties")) != null ? _api$assumption4 : loose;
53
54 function getExtendsHelper(file) {
55 return useBuiltIns ? _core.types.memberExpression(_core.types.identifier("Object"), _core.types.identifier("assign")) : file.addHelper("extends");
56 }
57
58 function hasRestElement(path) {
59 let foundRestElement = false;
60 visitRestElements(path, restElement => {
61 foundRestElement = true;
62 restElement.stop();
63 });
64 return foundRestElement;
65 }
66
67 function hasObjectPatternRestElement(path) {
68 let foundRestElement = false;
69 visitRestElements(path, restElement => {
70 if (restElement.parentPath.isObjectPattern()) {
71 foundRestElement = true;
72 restElement.stop();
73 }
74 });
75 return foundRestElement;
76 }
77
78 function visitRestElements(path, visitor) {
79 path.traverse({
80 Expression(path) {
81 const parentType = path.parent.type;
82
83 if (parentType === "AssignmentPattern" && path.key === "right" || parentType === "ObjectProperty" && path.parent.computed && path.key === "key") {
84 path.skip();
85 }
86 },
87
88 RestElement: visitor
89 });
90 }
91
92 function hasSpread(node) {
93 for (const prop of node.properties) {
94 if (_core.types.isSpreadElement(prop)) {
95 return true;
96 }
97 }
98
99 return false;
100 }
101
102 function extractNormalizedKeys(path) {
103 const props = path.node.properties;
104 const keys = [];
105 let allLiteral = true;
106 let hasTemplateLiteral = false;
107
108 for (const prop of props) {
109 if (_core.types.isIdentifier(prop.key) && !prop.computed) {
110 keys.push(_core.types.stringLiteral(prop.key.name));
111 } else if (_core.types.isTemplateLiteral(prop.key)) {
112 keys.push(_core.types.cloneNode(prop.key));
113 hasTemplateLiteral = true;
114 } else if (_core.types.isLiteral(prop.key)) {
115 keys.push(_core.types.stringLiteral(String(prop.key.value)));
116 } else {
117 keys.push(_core.types.cloneNode(prop.key));
118 allLiteral = false;
119 }
120 }
121
122 return {
123 keys,
124 allLiteral,
125 hasTemplateLiteral
126 };
127 }
128
129 function replaceImpureComputedKeys(properties, scope) {
130 const impureComputedPropertyDeclarators = [];
131
132 for (const propPath of properties) {
133 const key = propPath.get("key");
134
135 if (propPath.node.computed && !key.isPure()) {
136 const name = scope.generateUidBasedOnNode(key.node);
137
138 const declarator = _core.types.variableDeclarator(_core.types.identifier(name), key.node);
139
140 impureComputedPropertyDeclarators.push(declarator);
141 key.replaceWith(_core.types.identifier(name));
142 }
143 }
144
145 return impureComputedPropertyDeclarators;
146 }
147
148 function removeUnusedExcludedKeys(path) {
149 const bindings = path.getOuterBindingIdentifierPaths();
150 Object.keys(bindings).forEach(bindingName => {
151 const bindingParentPath = bindings[bindingName].parentPath;
152
153 if (path.scope.getBinding(bindingName).references > ZERO_REFS || !bindingParentPath.isObjectProperty()) {
154 return;
155 }
156
157 bindingParentPath.remove();
158 });
159 }
160
161 function createObjectRest(path, file, objRef) {
162 const props = path.get("properties");
163 const last = props[props.length - 1];
164
165 _core.types.assertRestElement(last.node);
166
167 const restElement = _core.types.cloneNode(last.node);
168
169 last.remove();
170 const impureComputedPropertyDeclarators = replaceImpureComputedKeys(path.get("properties"), path.scope);
171 const {
172 keys,
173 allLiteral,
174 hasTemplateLiteral
175 } = extractNormalizedKeys(path);
176
177 if (keys.length === 0) {
178 return [impureComputedPropertyDeclarators, restElement.argument, _core.types.callExpression(getExtendsHelper(file), [_core.types.objectExpression([]), _core.types.cloneNode(objRef)])];
179 }
180
181 let keyExpression;
182
183 if (!allLiteral) {
184 keyExpression = _core.types.callExpression(_core.types.memberExpression(_core.types.arrayExpression(keys), _core.types.identifier("map")), [file.addHelper("toPropertyKey")]);
185 } else {
186 keyExpression = _core.types.arrayExpression(keys);
187
188 if (!hasTemplateLiteral && !_core.types.isProgram(path.scope.block)) {
189 const program = path.findParent(path => path.isProgram());
190 const id = path.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
200 return [impureComputedPropertyDeclarators, restElement.argument, _core.types.callExpression(file.addHelper(`objectWithoutProperties${objectRestNoSymbols ? "Loose" : ""}`), [_core.types.cloneNode(objRef), keyExpression])];
201 }
202
203 function replaceRestElement(parentPath, paramPath, container) {
204 if (paramPath.isAssignmentPattern()) {
205 replaceRestElement(parentPath, paramPath.get("left"), container);
206 return;
207 }
208
209 if (paramPath.isArrayPattern() && hasRestElement(paramPath)) {
210 const elements = paramPath.get("elements");
211
212 for (let i = 0; i < elements.length; i++) {
213 replaceRestElement(parentPath, elements[i], container);
214 }
215 }
216
217 if (paramPath.isObjectPattern() && hasRestElement(paramPath)) {
218 const uid = parentPath.scope.generateUidIdentifier("ref");
219
220 const declar = _core.types.variableDeclaration("let", [_core.types.variableDeclarator(paramPath.node, uid)]);
221
222 if (container) {
223 container.push(declar);
224 } else {
225 parentPath.ensureBlock();
226 parentPath.get("body").unshiftContainer("body", declar);
227 }
228
229 paramPath.replaceWith(_core.types.cloneNode(uid));
230 }
231 }
232
233 return {
234 name: "proposal-object-rest-spread",
235 inherits: _pluginSyntaxObjectRestSpread.default,
236 visitor: {
237 Function(path) {
238 const params = path.get("params");
239 const paramsWithRestElement = new Set();
240 const idsInRestParams = new Set();
241
242 for (let i = 0; i < params.length; ++i) {
243 const param = params[i];
244
245 if (hasRestElement(param)) {
246 paramsWithRestElement.add(i);
247
248 for (const name of Object.keys(param.getBindingIdentifiers())) {
249 idsInRestParams.add(name);
250 }
251 }
252 }
253
254 let idInRest = false;
255
256 const IdentifierHandler = function (path, functionScope) {
257 const name = path.node.name;
258
259 if (path.scope.getBinding(name) === functionScope.getBinding(name) && idsInRestParams.has(name)) {
260 idInRest = true;
261 path.stop();
262 }
263 };
264
265 let i;
266
267 for (i = 0; i < params.length && !idInRest; ++i) {
268 const param = params[i];
269
270 if (!paramsWithRestElement.has(i)) {
271 if (param.isReferencedIdentifier() || param.isBindingIdentifier()) {
272 IdentifierHandler(path, path.scope);
273 } else {
274 param.traverse({
275 "Scope|TypeAnnotation|TSTypeAnnotation": path => path.skip(),
276 "ReferencedIdentifier|BindingIdentifier": IdentifierHandler
277 }, path.scope);
278 }
279 }
280 }
281
282 if (!idInRest) {
283 for (let i = 0; i < params.length; ++i) {
284 const param = params[i];
285
286 if (paramsWithRestElement.has(i)) {
287 replaceRestElement(param.parentPath, param);
288 }
289 }
290 } else {
291 const shouldTransformParam = idx => idx >= i - 1 || paramsWithRestElement.has(idx);
292
293 (0, _pluginTransformParameters.convertFunctionParams)(path, ignoreFunctionLength, shouldTransformParam, replaceRestElement);
294 }
295 },
296
297 VariableDeclarator(path, file) {
298 if (!path.get("id").isObjectPattern()) {
299 return;
300 }
301
302 let insertionPath = path;
303 const originalPath = path;
304 visitRestElements(path.get("id"), path => {
305 if (!path.parentPath.isObjectPattern()) {
306 return;
307 }
308
309 if ((0, _shouldStoreRHSInTemporaryVariable.default)(originalPath.node.id) && !_core.types.isIdentifier(originalPath.node.init)) {
310 const initRef = path.scope.generateUidIdentifierBasedOnNode(originalPath.node.init, "ref");
311 originalPath.insertBefore(_core.types.variableDeclarator(initRef, originalPath.node.init));
312 originalPath.replaceWith(_core.types.variableDeclarator(originalPath.node.id, _core.types.cloneNode(initRef)));
313 return;
314 }
315
316 let ref = originalPath.node.init;
317 const refPropertyPath = [];
318 let kind;
319 path.findParent(path => {
320 if (path.isObjectProperty()) {
321 refPropertyPath.unshift(path);
322 } else if (path.isVariableDeclarator()) {
323 kind = path.parentPath.node.kind;
324 return true;
325 }
326 });
327 const impureObjRefComputedDeclarators = replaceImpureComputedKeys(refPropertyPath, path.scope);
328 refPropertyPath.forEach(prop => {
329 const {
330 node
331 } = prop;
332 ref = _core.types.memberExpression(ref, _core.types.cloneNode(node.key), node.computed || _core.types.isLiteral(node.key));
333 });
334 const objectPatternPath = path.findParent(path => path.isObjectPattern());
335 const [impureComputedPropertyDeclarators, argument, callExpression] = createObjectRest(objectPatternPath, file, ref);
336
337 if (pureGetters) {
338 removeUnusedExcludedKeys(objectPatternPath);
339 }
340
341 _core.types.assertIdentifier(argument);
342
343 insertionPath.insertBefore(impureComputedPropertyDeclarators);
344 insertionPath.insertBefore(impureObjRefComputedDeclarators);
345 insertionPath.insertAfter(_core.types.variableDeclarator(argument, callExpression));
346 insertionPath = insertionPath.getSibling(insertionPath.key + 1);
347 path.scope.registerBinding(kind, insertionPath);
348
349 if (objectPatternPath.node.properties.length === 0) {
350 objectPatternPath.findParent(path => path.isObjectProperty() || path.isVariableDeclarator()).remove();
351 }
352 });
353 },
354
355 ExportNamedDeclaration(path) {
356 const declaration = path.get("declaration");
357 if (!declaration.isVariableDeclaration()) return;
358 const hasRest = declaration.get("declarations").some(path => hasObjectPatternRestElement(path.get("id")));
359 if (!hasRest) return;
360 const specifiers = [];
361
362 for (const name of Object.keys(path.getOuterBindingIdentifiers(path))) {
363 specifiers.push(_core.types.exportSpecifier(_core.types.identifier(name), _core.types.identifier(name)));
364 }
365
366 path.replaceWith(declaration.node);
367 path.insertAfter(_core.types.exportNamedDeclaration(null, specifiers));
368 },
369
370 CatchClause(path) {
371 const paramPath = path.get("param");
372 replaceRestElement(paramPath.parentPath, paramPath);
373 },
374
375 AssignmentExpression(path, file) {
376 const leftPath = path.get("left");
377
378 if (leftPath.isObjectPattern() && hasRestElement(leftPath)) {
379 const nodes = [];
380 const refName = path.scope.generateUidBasedOnNode(path.node.right, "ref");
381 nodes.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(_core.types.identifier(refName), path.node.right)]));
382 const [impureComputedPropertyDeclarators, argument, callExpression] = createObjectRest(leftPath, file, _core.types.identifier(refName));
383
384 if (impureComputedPropertyDeclarators.length > 0) {
385 nodes.push(_core.types.variableDeclaration("var", impureComputedPropertyDeclarators));
386 }
387
388 const nodeWithoutSpread = _core.types.cloneNode(path.node);
389
390 nodeWithoutSpread.right = _core.types.identifier(refName);
391 nodes.push(_core.types.expressionStatement(nodeWithoutSpread));
392 nodes.push(_core.types.toStatement(_core.types.assignmentExpression("=", argument, callExpression)));
393 nodes.push(_core.types.expressionStatement(_core.types.identifier(refName)));
394 path.replaceWithMultiple(nodes);
395 }
396 },
397
398 ForXStatement(path) {
399 const {
400 node,
401 scope
402 } = path;
403 const leftPath = path.get("left");
404 const left = node.left;
405
406 if (!hasObjectPatternRestElement(leftPath)) {
407 return;
408 }
409
410 if (!_core.types.isVariableDeclaration(left)) {
411 const temp = scope.generateUidIdentifier("ref");
412 node.left = _core.types.variableDeclaration("var", [_core.types.variableDeclarator(temp)]);
413 path.ensureBlock();
414
415 if (node.body.body.length === 0 && path.isCompletionRecord()) {
416 node.body.body.unshift(_core.types.expressionStatement(scope.buildUndefinedNode()));
417 }
418
419 node.body.body.unshift(_core.types.expressionStatement(_core.types.assignmentExpression("=", left, _core.types.cloneNode(temp))));
420 } else {
421 const pattern = left.declarations[0].id;
422 const key = scope.generateUidIdentifier("ref");
423 node.left = _core.types.variableDeclaration(left.kind, [_core.types.variableDeclarator(key, null)]);
424 path.ensureBlock();
425 node.body.body.unshift(_core.types.variableDeclaration(node.left.kind, [_core.types.variableDeclarator(pattern, _core.types.cloneNode(key))]));
426 }
427 },
428
429 ArrayPattern(path) {
430 const objectPatterns = [];
431 visitRestElements(path, path => {
432 if (!path.parentPath.isObjectPattern()) {
433 return;
434 }
435
436 const objectPattern = path.parentPath;
437 const uid = path.scope.generateUidIdentifier("ref");
438 objectPatterns.push(_core.types.variableDeclarator(objectPattern.node, uid));
439 objectPattern.replaceWith(_core.types.cloneNode(uid));
440 path.skip();
441 });
442
443 if (objectPatterns.length > 0) {
444 const statementPath = path.getStatementParent();
445 statementPath.insertAfter(_core.types.variableDeclaration(statementPath.node.kind || "var", objectPatterns));
446 }
447 },
448
449 ObjectExpression(path, file) {
450 if (!hasSpread(path.node)) return;
451 let helper;
452
453 if (setSpreadProperties) {
454 helper = getExtendsHelper(file);
455 } else {
456 try {
457 helper = file.addHelper("objectSpread2");
458 } catch (_unused) {
459 this.file.declarations["objectSpread2"] = null;
460 helper = file.addHelper("objectSpread");
461 }
462 }
463
464 let exp = null;
465 let props = [];
466
467 function make() {
468 const hadProps = props.length > 0;
469
470 const obj = _core.types.objectExpression(props);
471
472 props = [];
473
474 if (!exp) {
475 exp = _core.types.callExpression(helper, [obj]);
476 return;
477 }
478
479 if (pureGetters) {
480 if (hadProps) {
481 exp.arguments.push(obj);
482 }
483
484 return;
485 }
486
487 exp = _core.types.callExpression(_core.types.cloneNode(helper), [exp, ...(hadProps ? [_core.types.objectExpression([]), obj] : [])]);
488 }
489
490 for (const prop of path.node.properties) {
491 if (_core.types.isSpreadElement(prop)) {
492 make();
493 exp.arguments.push(prop.argument);
494 } else {
495 props.push(prop);
496 }
497 }
498
499 if (props.length) make();
500 path.replaceWith(exp);
501 }
502
503 }
504 };
505});
506
507exports.default = _default;
Note: See TracBrowser for help on using the repository browser.