add support for spreads anywhere in list - fixes #73

This commit is contained in:
Sebastian McKenzie 2014-10-19 14:52:37 +11:00
parent a75248d2d2
commit 42a7973a9d
13 changed files with 67 additions and 17 deletions

View File

@ -14,42 +14,68 @@ var getSpreadLiteral = function (spread, file) {
};
var hasSpread = function (nodes) {
return nodes.length && _.last(nodes).type === "SpreadElement";
var has = false;
_.each(nodes, function (node) {
if (node.type === "SpreadElement") {
has = true;
return false;
}
});
return has;
};
var build = function (props, file) {
var nodes = [];
var _props = [];
var push = function () {
if (!_props.length) return;
nodes.push(b.arrayExpression(_props));
_props = [];
};
_.each(props, function (prop) {
if (prop.type === "SpreadElement") {
push();
nodes.push(getSpreadLiteral(prop, file));
} else {
_props.push(prop);
}
});
push();
return nodes;
};
exports.ArrayExpression = function (node, parent, file) {
var elements = node.elements;
if (!hasSpread(elements)) return;
var spread = elements.pop();
var nodes = build(elements, file);
var first = nodes.shift();
var concat = util.template("array-concat", {
ARGUMENT: getSpreadLiteral(spread, file)
});
if (!nodes.length) return first;
concat.callee.object.elements = elements;
return concat;
return b.callExpression(b.memberExpression(first, b.identifier("concat"), false), nodes);
};
exports.CallExpression = function (node, parent, file) {
var args = node.arguments;
if (!hasSpread(args)) return;
var spread = args.pop();
var spreadLiteral = getSpreadLiteral(spread, file);
var contextLiteral = b.literal(null);
node.arguments = [];
if (args.length) {
var concat = util.template("array-concat");
concat.arguments = [spreadLiteral];
concat.callee.object.elements = args;
node.arguments.push(concat);
var nodes = build(args, file);
var first = nodes.shift();
if (nodes.length) {
node.arguments.push(b.callExpression(b.memberExpression(first, b.identifier("concat"), false), nodes));
} else {
node.arguments.push(spreadLiteral);
node.arguments.push(first);
}
var callee = node.callee;

View File

@ -0,0 +1 @@
var lyrics = [...parts, "head", "and", "toes"];

View File

@ -0,0 +1,3 @@
"use strict";
var _slice = Array.prototype.slice;
var lyrics = _slice.call(parts).concat(["head", "and", "toes"]);

View File

@ -0,0 +1 @@
var a = [b, ...c, d];

View File

@ -0,0 +1,3 @@
"use strict";
var _slice = Array.prototype.slice;
var a = [b].concat(_slice.call(c), [d]);

View File

@ -0,0 +1 @@
var a = [b, ...c, d, e, ...f];

View File

@ -0,0 +1,3 @@
"use strict";
var _slice = Array.prototype.slice;
var a = [b].concat(_slice.call(c), [d, e], _slice.call(f));

View File

@ -0,0 +1 @@
add(...numbers, foo, bar);

View File

@ -0,0 +1,3 @@
"use strict";
var _slice = Array.prototype.slice;
add.apply(null, _slice.call(numbers).concat([foo, bar]));

View File

@ -0,0 +1 @@
add(foo, ...numbers, bar);

View File

@ -0,0 +1,3 @@
"use strict";
var _slice = Array.prototype.slice;
add.apply(null, [foo].concat(_slice.call(numbers), [bar]));

View File

@ -0,0 +1 @@
add(foo, ...numbers, bar, what, ...test);

View File

@ -0,0 +1,3 @@
"use strict";
var _slice = Array.prototype.slice;
add.apply(null, [foo].concat(_slice.call(numbers), [bar, what], _slice.call(test)));