source: imaps-frontend/node_modules/@babel/plugin-transform-object-rest-spread/lib/index.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

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