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 _ = 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;
if (!elements.length) return;
@ -13,7 +24,7 @@ exports.ArrayExpression = function (node) {
}
var concat = util.template("array-concat", {
ARGUMENT: spread.argument
ARGUMENT: getSpreadLiteral(spread, file)
});
concat.callee.object.elements = elements;
@ -27,18 +38,12 @@ exports.CallExpression = function (node, parent, file) {
if (args.length && _.last(args).type === "SpreadElement") {
var spread = args.pop();
var spreadLiteral = spread.argument;
var spreadLiteral = getSpreadLiteral(spread, file);
var contextLiteral = b.literal(null);
node.arguments = [];
if (args.length) {
if (spreadLiteral.name === "arguments") {
spreadLiteral = util.template("arguments-slice", {
SLICE_KEY: file.addDeclaration("slice")
});
}
var concat = util.template("array-concat");
concat.arguments = [spreadLiteral];
concat.callee.object.elements = args;

View File

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

View File

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

View File

@ -1,2 +1,3 @@
foob.add.apply(foob, numbers);
foob.test.add.apply(foob.test, numbers);
var _slice = Array.prototype.slice;
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, [
foo,
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));