| [9af201e] | 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.default = void 0;
|
|---|
| 7 | var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
|---|
| 8 | var _helperSkipTransparentExpressionWrappers = require("@babel/helper-skip-transparent-expression-wrappers");
|
|---|
| 9 | var _core = require("@babel/core");
|
|---|
| 10 | var _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 | const node = spread.argument;
|
|---|
| 22 | if (_core.types.isIdentifier(node)) {
|
|---|
| 23 | const binding = scope.getBinding(node.name);
|
|---|
| 24 | if (binding != null && binding.constant && binding.path.isGenericType("Array")) {
|
|---|
| 25 | return node;
|
|---|
| 26 | }
|
|---|
| 27 | }
|
|---|
| 28 | if (_core.types.isArrayExpression(node)) {
|
|---|
| 29 | return node;
|
|---|
| 30 | }
|
|---|
| 31 | if (_core.types.isIdentifier(node, {
|
|---|
| 32 | name: "arguments"
|
|---|
| 33 | })) {
|
|---|
| 34 | return _core.template.expression.ast`
|
|---|
| 35 | Array.prototype.slice.call(${node})
|
|---|
| 36 | `;
|
|---|
| 37 | }
|
|---|
| 38 | const args = [node];
|
|---|
| 39 | let helperName = "toConsumableArray";
|
|---|
| 40 | if (arrayLikeIsIterable) {
|
|---|
| 41 | args.unshift(scope.path.hub.addHelper(helperName));
|
|---|
| 42 | helperName = "maybeArrayLike";
|
|---|
| 43 | }
|
|---|
| 44 | return _core.types.callExpression(scope.path.hub.addHelper(helperName), args);
|
|---|
| 45 | }
|
|---|
| 46 | }
|
|---|
| 47 | function hasHole(spread) {
|
|---|
| 48 | return spread.elements.includes(null);
|
|---|
| 49 | }
|
|---|
| 50 | function hasSpread(nodes) {
|
|---|
| 51 | for (let i = 0; i < nodes.length; i++) {
|
|---|
| 52 | if (_core.types.isSpreadElement(nodes[i])) {
|
|---|
| 53 | return true;
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 | return false;
|
|---|
| 57 | }
|
|---|
| 58 | function push(_props, nodes) {
|
|---|
| 59 | if (!_props.length) return _props;
|
|---|
| 60 | nodes.push(_core.types.arrayExpression(_props));
|
|---|
| 61 | return [];
|
|---|
| 62 | }
|
|---|
| 63 | function build(props, scope, file) {
|
|---|
| 64 | const nodes = [];
|
|---|
| 65 | let _props = [];
|
|---|
| 66 | for (const prop of props) {
|
|---|
| 67 | if (_core.types.isSpreadElement(prop)) {
|
|---|
| 68 | _props = push(_props, nodes);
|
|---|
| 69 | let spreadLiteral = getSpreadLiteral(prop, scope);
|
|---|
| 70 | if (_core.types.isArrayExpression(spreadLiteral) && hasHole(spreadLiteral)) {
|
|---|
| 71 | spreadLiteral = _core.types.callExpression(file.addHelper("arrayWithoutHoles"), [spreadLiteral]);
|
|---|
| 72 | }
|
|---|
| 73 | nodes.push(spreadLiteral);
|
|---|
| 74 | } else {
|
|---|
| 75 | _props.push(prop);
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 | push(_props, nodes);
|
|---|
| 79 | return nodes;
|
|---|
| 80 | }
|
|---|
| 81 | return {
|
|---|
| 82 | name: "transform-spread",
|
|---|
| 83 | visitor: {
|
|---|
| 84 | ArrayExpression(path) {
|
|---|
| 85 | const {
|
|---|
| 86 | node,
|
|---|
| 87 | scope
|
|---|
| 88 | } = path;
|
|---|
| 89 | const elements = node.elements;
|
|---|
| 90 | if (!hasSpread(elements)) return;
|
|---|
| 91 | const nodes = build(elements, scope, this.file);
|
|---|
| 92 | let first = nodes[0];
|
|---|
| 93 | if (nodes.length === 1 && first !== elements[0].argument) {
|
|---|
| 94 | path.replaceWith(first);
|
|---|
| 95 | return;
|
|---|
| 96 | }
|
|---|
| 97 | if (!_core.types.isArrayExpression(first)) {
|
|---|
| 98 | first = _core.types.arrayExpression([]);
|
|---|
| 99 | } else {
|
|---|
| 100 | nodes.shift();
|
|---|
| 101 | }
|
|---|
| 102 | path.replaceWith(_core.types.callExpression(_core.types.memberExpression(first, _core.types.identifier("concat")), nodes));
|
|---|
| 103 | },
|
|---|
| 104 | CallExpression(path) {
|
|---|
| 105 | const {
|
|---|
| 106 | node,
|
|---|
| 107 | scope
|
|---|
| 108 | } = path;
|
|---|
| 109 | const args = node.arguments;
|
|---|
| 110 | if (!hasSpread(args)) return;
|
|---|
| 111 | const calleePath = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("callee"));
|
|---|
| 112 | if (calleePath.isSuper()) {
|
|---|
| 113 | 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.");
|
|---|
| 114 | }
|
|---|
| 115 | let contextLiteral = scope.buildUndefinedNode();
|
|---|
| 116 | node.arguments = [];
|
|---|
| 117 | let nodes;
|
|---|
| 118 | if (args.length === 1 && _core.types.isIdentifier(args[0].argument, {
|
|---|
| 119 | name: "arguments"
|
|---|
| 120 | })) {
|
|---|
| 121 | nodes = [args[0].argument];
|
|---|
| 122 | } else {
|
|---|
| 123 | nodes = build(args, scope, this.file);
|
|---|
| 124 | }
|
|---|
| 125 | const first = nodes.shift();
|
|---|
| 126 | if (nodes.length) {
|
|---|
| 127 | node.arguments.push(_core.types.callExpression(_core.types.memberExpression(first, _core.types.identifier("concat")), nodes));
|
|---|
| 128 | } else {
|
|---|
| 129 | node.arguments.push(first);
|
|---|
| 130 | }
|
|---|
| 131 | const callee = calleePath.node;
|
|---|
| 132 | if (_core.types.isMemberExpression(callee)) {
|
|---|
| 133 | const temp = scope.maybeGenerateMemoised(callee.object);
|
|---|
| 134 | if (temp) {
|
|---|
| 135 | callee.object = _core.types.assignmentExpression("=", temp, callee.object);
|
|---|
| 136 | contextLiteral = temp;
|
|---|
| 137 | } else {
|
|---|
| 138 | contextLiteral = _core.types.cloneNode(callee.object);
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 | node.callee = _core.types.memberExpression(node.callee, _core.types.identifier("apply"));
|
|---|
| 142 | if (_core.types.isSuper(contextLiteral)) {
|
|---|
| 143 | contextLiteral = _core.types.thisExpression();
|
|---|
| 144 | }
|
|---|
| 145 | node.arguments.unshift(_core.types.cloneNode(contextLiteral));
|
|---|
| 146 | },
|
|---|
| 147 | NewExpression(path) {
|
|---|
| 148 | const {
|
|---|
| 149 | node,
|
|---|
| 150 | scope
|
|---|
| 151 | } = path;
|
|---|
| 152 | if (!hasSpread(node.arguments)) return;
|
|---|
| 153 | const nodes = build(node.arguments, scope, this.file);
|
|---|
| 154 | const first = nodes.shift();
|
|---|
| 155 | let args;
|
|---|
| 156 | if (nodes.length) {
|
|---|
| 157 | args = _core.types.callExpression(_core.types.memberExpression(first, _core.types.identifier("concat")), nodes);
|
|---|
| 158 | } else {
|
|---|
| 159 | args = first;
|
|---|
| 160 | }
|
|---|
| 161 | path.replaceWith(_core.types.callExpression(path.hub.addHelper("construct"), [node.callee, args]));
|
|---|
| 162 | }
|
|---|
| 163 | }
|
|---|
| 164 | };
|
|---|
| 165 | });
|
|---|
| 166 |
|
|---|
| 167 | //# sourceMappingURL=index.js.map
|
|---|