always slice spread literals
This commit is contained in:
parent
3b22cb283b
commit
e97086f5f7
1
lib/6to5/templates/call.js
Normal file
1
lib/6to5/templates/call.js
Normal file
@ -0,0 +1 @@
|
||||
OBJECT.call(CONTEXT);
|
||||
@ -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;
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
var _slice = Array.prototype.slice;
|
||||
|
||||
var lyrics = [
|
||||
"head",
|
||||
"and",
|
||||
"toes"
|
||||
].concat(parts);
|
||||
].concat(_slice.call(parts));
|
||||
|
||||
@ -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)));
|
||||
|
||||
@ -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));
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
var _slice = Array.prototype.slice;
|
||||
add.apply(null, [
|
||||
foo,
|
||||
bar
|
||||
].concat(numbers));
|
||||
].concat(_slice.call(numbers)));
|
||||
|
||||
@ -1 +1,2 @@
|
||||
add.apply(null, numbers);
|
||||
var _slice = Array.prototype.slice;
|
||||
add.apply(null, _slice.call(numbers));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user