UNPKG

@babel/plugin-transform-spread

Version:
166 lines (163 loc) 5.72 kB
import { declare } from '@babel/helper-plugin-utils'; import { skipTransparentExprWrappers } from '@babel/helper-skip-transparent-expression-wrappers'; import { types, template } from '@babel/core'; const index = declare((api, options) => { api.assertVersion("^7.0.0-0 || ^8.0.0"); if ("loose" in options) { console.warn("@babel/plugin-transform-spread: The 'loose' option has been deprecated, " + "use the `iterableIsArray` assumption instead (https://babeljs.io/assumptions)."); } const iterableIsArray = api.assumption("iterableIsArray") ?? options.loose; const arrayLikeIsIterable = options.allowArrayLike ?? api.assumption("arrayLikeIsIterable"); function getSpreadLiteral(spread, scope) { if (iterableIsArray && !types.isIdentifier(spread.argument, { name: "arguments" })) { return spread.argument; } else { const node = spread.argument; if (types.isIdentifier(node)) { const binding = scope.getBinding(node.name); if (binding?.constant && binding.path.isGenericType("Array")) { return node; } } if (types.isArrayExpression(node)) { return node; } if (types.isIdentifier(node, { name: "arguments" })) { return template.expression.ast` Array.prototype.slice.call(${node}) `; } const args = [node]; let helperName = "toConsumableArray"; if (arrayLikeIsIterable) { args.unshift(scope.path.hub.addHelper(helperName)); helperName = "maybeArrayLike"; } return types.callExpression(scope.path.hub.addHelper(helperName), args); } } function hasHole(spread) { return spread.elements.includes(null); } function hasSpread(nodes) { for (let i = 0; i < nodes.length; i++) { if (types.isSpreadElement(nodes[i])) { return true; } } return false; } function push(_props, nodes) { if (!_props.length) return _props; nodes.push(types.arrayExpression(_props)); return []; } function build(props, scope, file) { const nodes = []; let _props = []; for (const prop of props) { if (types.isSpreadElement(prop)) { _props = push(_props, nodes); let spreadLiteral = getSpreadLiteral(prop, scope); if (types.isArrayExpression(spreadLiteral) && hasHole(spreadLiteral)) { spreadLiteral = types.callExpression(file.addHelper("arrayLikeToArray"), [spreadLiteral]); } nodes.push(spreadLiteral); } else { _props.push(prop); } } push(_props, nodes); return nodes; } return { name: "transform-spread", visitor: { ArrayExpression(path) { const { node, scope } = path; const elements = node.elements; if (!hasSpread(elements)) return; const nodes = build(elements, scope, this.file); let first = nodes[0]; if (nodes.length === 1 && first !== elements[0].argument) { path.replaceWith(first); return; } if (!types.isArrayExpression(first)) { first = types.arrayExpression([]); } else { nodes.shift(); } path.replaceWith(types.callExpression(types.memberExpression(first, types.identifier("concat")), nodes)); }, CallExpression(path) { const { node, scope } = path; const args = node.arguments; if (!hasSpread(args)) return; const calleePath = skipTransparentExprWrappers(path.get("callee")); if (calleePath.isSuper()) { 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."); } let contextLiteral = types.buildUndefinedNode(); node.arguments = []; let nodes; if (args.length === 1 && types.isIdentifier(args[0].argument, { name: "arguments" })) { nodes = [args[0].argument]; } else { nodes = build(args, scope, this.file); } const first = nodes.shift(); if (nodes.length) { node.arguments.push(types.callExpression(types.memberExpression(first, types.identifier("concat")), nodes)); } else { node.arguments.push(first); } const callee = calleePath.node; if (types.isMemberExpression(callee)) { const temp = scope.maybeGenerateMemoised(callee.object); if (temp) { callee.object = types.assignmentExpression("=", temp, callee.object); contextLiteral = temp; } else { contextLiteral = types.cloneNode(callee.object); } } node.callee = types.memberExpression(node.callee, types.identifier("apply")); if (types.isSuper(contextLiteral)) { contextLiteral = types.thisExpression(); } node.arguments.unshift(types.cloneNode(contextLiteral)); }, NewExpression(path) { const { node, scope } = path; if (!hasSpread(node.arguments)) return; const nodes = build(node.arguments, scope, this.file); const first = nodes.shift(); let args; if (nodes.length) { args = types.callExpression(types.memberExpression(first, types.identifier("concat")), nodes); } else { args = first; } path.replaceWith(types.callExpression(path.hub.addHelper("construct"), [node.callee, args])); } } }; }); export { index as default }; //# sourceMappingURL=index.js.map