@babel/plugin-transform-spread
Version:
Compile ES2015 spread to ES5
194 lines (191 loc) • 7.01 kB
JavaScript
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 isConstantArray(node, scope) {
if (!types.isIdentifier(node)) return false;
const binding = scope.getBinding(node.name);
return Boolean(binding?.constant && binding.path.isGenericType("Array"));
}
function isPureElement(prop, scope) {
if (prop === null) return true;
if (types.isSpreadElement(prop)) {
const {
argument
} = prop;
return Boolean(iterableIsArray && scope.isPure(argument)) || isConstantArray(argument, scope) || types.isArrayExpression(argument) && scope.isPure(argument);
}
return scope.isPure(prop);
}
function getSpreadLiteral(spread, scope, hasLaterSideEffects) {
if (iterableIsArray && !types.isIdentifier(spread.argument, {
name: "arguments"
})) {
if (hasLaterSideEffects && !types.isArrayExpression(spread.argument)) {
return types.callExpression(types.memberExpression(spread.argument, types.identifier("slice")), []);
}
return spread.argument;
} else {
const node = spread.argument;
if (isConstantArray(node, scope)) {
if (!hasLaterSideEffects) return node;
return types.callExpression(scope.path.hub.addHelper("toConsumableArray"), [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 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) {
const nodes = [];
let _props = [];
let lastImpureIndex = -1;
for (let i = props.length - 1; i >= 0; i--) {
if (!isPureElement(props[i], scope)) {
lastImpureIndex = i;
break;
}
}
for (let i = 0; i < props.length; i++) {
const prop = props[i];
if (types.isSpreadElement(prop)) {
_props = push(_props, nodes);
const spreadLiteral = getSpreadLiteral(prop, scope, i < lastImpureIndex);
if (types.isArrayExpression(spreadLiteral)) {
spreadLiteral.elements = spreadLiteral.elements.map(el => el ?? types.buildUndefinedNode());
}
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);
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);
}
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);
}
}
if (types.isSuper(contextLiteral)) {
contextLiteral = types.thisExpression();
}
const isPureArg = arg => types.isIdentifier(arg, {
name: "arguments"
}) || scope.isPure(arg);
if (args.every(arg => types.isSpreadElement(arg) ? isPureArg(arg.argument) : isPureArg(arg)) || !this.availableHelper("callSpread")) {
node.callee = types.memberExpression(node.callee, types.identifier("apply"));
node.arguments.unshift(types.cloneNode(contextLiteral));
} else {
node.arguments.unshift(node.callee, types.cloneNode(contextLiteral));
node.callee = this.addHelper("callSpread");
}
},
NewExpression(path) {
const {
node,
scope
} = path;
if (!hasSpread(node.arguments)) return;
const nodes = build(node.arguments, scope);
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 };