UNPKG

5.28 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7var _helperPluginUtils = require("@babel/helper-plugin-utils");
8var _helperSkipTransparentExpressionWrappers = require("@babel/helper-skip-transparent-expression-wrappers");
9var _core = require("@babel/core");
10var _default = exports.default = (0, _helperPluginUtils.declare)((api, options) => {
11 var _api$assumption, _options$allowArrayLi;
12 api.assertVersion(7);
13 const iterableIsArray = (_api$assumption = api.assumption("iterableIsArray")) != null ? _api$assumption : options.loose;
14 const arrayLikeIsIterable = (_options$allowArrayLi = options.allowArrayLike) != null ? _options$allowArrayLi : api.assumption("arrayLikeIsIterable");
15 function getSpreadLiteral(spread, scope) {
16 if (iterableIsArray && !_core.types.isIdentifier(spread.argument, {
17 name: "arguments"
18 })) {
19 return spread.argument;
20 } else {
21 return scope.toArray(spread.argument, true, arrayLikeIsIterable);
22 }
23 }
24 function hasHole(spread) {
25 return spread.elements.some(el => el === null);
26 }
27 function hasSpread(nodes) {
28 for (let i = 0; i < nodes.length; i++) {
29 if (_core.types.isSpreadElement(nodes[i])) {
30 return true;
31 }
32 }
33 return false;
34 }
35 function push(_props, nodes) {
36 if (!_props.length) return _props;
37 nodes.push(_core.types.arrayExpression(_props));
38 return [];
39 }
40 function build(props, scope, file) {
41 const nodes = [];
42 let _props = [];
43 for (const prop of props) {
44 if (_core.types.isSpreadElement(prop)) {
45 _props = push(_props, nodes);
46 let spreadLiteral = getSpreadLiteral(prop, scope);
47 if (_core.types.isArrayExpression(spreadLiteral) && hasHole(spreadLiteral)) {
48 spreadLiteral = _core.types.callExpression(file.addHelper("arrayWithoutHoles"), [spreadLiteral]);
49 }
50 nodes.push(spreadLiteral);
51 } else {
52 _props.push(prop);
53 }
54 }
55 push(_props, nodes);
56 return nodes;
57 }
58 return {
59 name: "transform-spread",
60 visitor: {
61 ArrayExpression(path) {
62 const {
63 node,
64 scope
65 } = path;
66 const elements = node.elements;
67 if (!hasSpread(elements)) return;
68 const nodes = build(elements, scope, this.file);
69 let first = nodes[0];
70 if (nodes.length === 1 && first !== elements[0].argument) {
71 path.replaceWith(first);
72 return;
73 }
74 if (!_core.types.isArrayExpression(first)) {
75 first = _core.types.arrayExpression([]);
76 } else {
77 nodes.shift();
78 }
79 path.replaceWith(_core.types.callExpression(_core.types.memberExpression(first, _core.types.identifier("concat")), nodes));
80 },
81 CallExpression(path) {
82 const {
83 node,
84 scope
85 } = path;
86 const args = node.arguments;
87 if (!hasSpread(args)) return;
88 const calleePath = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("callee"));
89 if (calleePath.isSuper()) {
90 throw path.buildCodeFrameError("It's not possible to compile spread arguments in `super()` without compiling classes.\n" + "Please add '@babel/plugin-transform-classes' to your Babel configuration.");
91 }
92 let contextLiteral = scope.buildUndefinedNode();
93 node.arguments = [];
94 let nodes;
95 if (args.length === 1 && _core.types.isIdentifier(args[0].argument, {
96 name: "arguments"
97 })) {
98 nodes = [args[0].argument];
99 } else {
100 nodes = build(args, scope, this.file);
101 }
102 const first = nodes.shift();
103 if (nodes.length) {
104 node.arguments.push(_core.types.callExpression(_core.types.memberExpression(first, _core.types.identifier("concat")), nodes));
105 } else {
106 node.arguments.push(first);
107 }
108 const callee = calleePath.node;
109 if (_core.types.isMemberExpression(callee)) {
110 const temp = scope.maybeGenerateMemoised(callee.object);
111 if (temp) {
112 callee.object = _core.types.assignmentExpression("=", temp, callee.object);
113 contextLiteral = temp;
114 } else {
115 contextLiteral = _core.types.cloneNode(callee.object);
116 }
117 }
118 node.callee = _core.types.memberExpression(node.callee, _core.types.identifier("apply"));
119 if (_core.types.isSuper(contextLiteral)) {
120 contextLiteral = _core.types.thisExpression();
121 }
122 node.arguments.unshift(_core.types.cloneNode(contextLiteral));
123 },
124 NewExpression(path) {
125 const {
126 node,
127 scope
128 } = path;
129 if (!hasSpread(node.arguments)) return;
130 const nodes = build(node.arguments, scope, this.file);
131 const first = nodes.shift();
132 let args;
133 if (nodes.length) {
134 args = _core.types.callExpression(_core.types.memberExpression(first, _core.types.identifier("concat")), nodes);
135 } else {
136 args = first;
137 }
138 path.replaceWith(_core.types.callExpression(path.hub.addHelper("construct"), [node.callee, args]));
139 }
140 }
141 };
142});
143
144//# sourceMappingURL=index.js.map