always slice spread literals

This commit is contained in:
Sebastian McKenzie 2014-10-14 09:29:21 +11:00
parent 3b22cb283b
commit e97086f5f7
7 changed files with 28 additions and 16 deletions

View File

@ -0,0 +1 @@
OBJECT.call(CONTEXT);

View File

@ -2,7 +2,18 @@ var util = require("../util");
var b = require("recast").types.builders; var b = require("recast").types.builders;
var _ = require("lodash"); var _ = require("lodash");
exports.ArrayExpression = function (node) { var getSpreadLiteral = function (spread, file) {
var literal = spread.argument;
if (literal.type !== "ArrayExpression") {
literal = util.template("call", {
OBJECT: file.addDeclaration("slice"),
CONTEXT: literal
});
}
return literal;
};
exports.ArrayExpression = function (node, parent, file) {
var elements = node.elements; var elements = node.elements;
if (!elements.length) return; if (!elements.length) return;
@ -13,7 +24,7 @@ exports.ArrayExpression = function (node) {
} }
var concat = util.template("array-concat", { var concat = util.template("array-concat", {
ARGUMENT: spread.argument ARGUMENT: getSpreadLiteral(spread, file)
}); });
concat.callee.object.elements = elements; concat.callee.object.elements = elements;
@ -27,18 +38,12 @@ exports.CallExpression = function (node, parent, file) {
if (args.length && _.last(args).type === "SpreadElement") { if (args.length && _.last(args).type === "SpreadElement") {
var spread = args.pop(); var spread = args.pop();
var spreadLiteral = spread.argument; var spreadLiteral = getSpreadLiteral(spread, file);
var contextLiteral = b.literal(null); var contextLiteral = b.literal(null);
node.arguments = []; node.arguments = [];
if (args.length) { if (args.length) {
if (spreadLiteral.name === "arguments") {
spreadLiteral = util.template("arguments-slice", {
SLICE_KEY: file.addDeclaration("slice")
});
}
var concat = util.template("array-concat"); var concat = util.template("array-concat");
concat.arguments = [spreadLiteral]; concat.arguments = [spreadLiteral];
concat.callee.object.elements = args; concat.callee.object.elements = args;

View File

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

View File

@ -1,8 +1,9 @@
var _slice = Array.prototype.slice;
foob.add.apply(foob, [ foob.add.apply(foob, [
foo, foo,
bar bar
].concat(numbers)); ].concat(_slice.call(numbers)));
foob.test.add.apply(foob.test, [ foob.test.add.apply(foob.test, [
foo, foo,
bar bar
].concat(numbers)); ].concat(_slice.call(numbers)));

View File

@ -1,2 +1,3 @@
foob.add.apply(foob, numbers); var _slice = Array.prototype.slice;
foob.test.add.apply(foob.test, numbers); foob.add.apply(foob, _slice.call(numbers));
foob.test.add.apply(foob.test, _slice.call(numbers));

View File

@ -1,4 +1,5 @@
var _slice = Array.prototype.slice;
add.apply(null, [ add.apply(null, [
foo, foo,
bar bar
].concat(numbers)); ].concat(_slice.call(numbers)));

View File

@ -1 +1,2 @@
add.apply(null, numbers); var _slice = Array.prototype.slice;
add.apply(null, _slice.call(numbers));