add toConsumableArray helper for spread - fixes #757

This commit is contained in:
Sebastian McKenzie
2015-02-12 07:37:40 +11:00
parent ae344aa23e
commit faa10df84c
21 changed files with 55 additions and 49 deletions

View File

@@ -41,6 +41,7 @@ File.helpers = [
"tagged-template-literal-loose",
"interop-require",
"to-array",
"to-consumable-array",
"sliced-to-array",
"object-without-properties",
"has-own",
@@ -216,19 +217,21 @@ File.prototype.debug = function (msg) {
util.debug(parts);
};
File.prototype.toArray = function (node, i) {
File.prototype.toArray = function (node, i, forceHelperName) {
if (t.isArrayExpression(node)) {
return node;
} else if (t.isIdentifier(node) && node.name === "arguments") {
return t.callExpression(t.memberExpression(this.addHelper("slice"), t.identifier("call")), [node]);
} else {
var declarationName = "to-array";
var helperName = "to-array";
var args = [node];
if (i) {
if (i === true) {
helperName = "to-consumable-array";
} else if (i) {
args.push(t.literal(i));
declarationName = "sliced-to-array";
helperName = "sliced-to-array";
}
return t.callExpression(this.addHelper(declarationName), args);
return t.callExpression(this.addHelper(helperName), args);
}
};

View File

@@ -1,9 +1,3 @@
(function (arr) {
if (Array.isArray(arr)) {
var arr2 = [];
for (var i = 0; i < arr.length; i++) arr2.push(arr[i]);
return arr2;
} else {
return Array.from(arr);
}
return Array.isArray(arr) ? arr : Array.from(arr);
});

View File

@@ -0,0 +1,9 @@
(function (arr) {
if (Array.isArray(arr)) {
var arr2 = [];
for (var i = 0; i < arr.length; i++) arr2.push(arr[i]);
return arr2;
} else {
return Array.from(arr);
}
});

View File

@@ -6,7 +6,7 @@ var t = require("../../../types");
exports.check = t.isSpreadElement;
var getSpreadLiteral = function (spread, file) {
return file.toArray(spread.argument);
return file.toArray(spread.argument, true);
};
var hasSpread = function (nodes) {