add toConsumableArray helper for spread - fixes #757
This commit is contained in:
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
9
lib/6to5/transformation/templates/to-consumable-array.js
Normal file
9
lib/6to5/transformation/templates/to-consumable-array.js
Normal 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);
|
||||
}
|
||||
});
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user