use Array.from instead of Array.prototype.slice in spread transformer and support NewExpression spreads - fixes #148

This commit is contained in:
Sebastian McKenzie
2014-11-12 18:38:30 +11:00
parent c235780611
commit 458e3d48f6
23 changed files with 63 additions and 52 deletions

View File

@@ -1,5 +0,0 @@
(function (obj) {
var arr = [];
for (var val of obj) arr.push(val);
return arr;
});

View File

@@ -5,10 +5,10 @@ var _ = require("lodash");
var getSpreadLiteral = function (spread, file) {
var literal = spread.argument;
if (!t.isArrayExpression(literal)) {
literal = util.template("call", {
OBJECT: file.addDeclaration("slice"),
CONTEXT: literal
});
literal = t.callExpression(
t.memberExpression(t.identifier("Array"), t.identifier("from")),
[literal]
);
}
return literal;
};
@@ -96,3 +96,19 @@ exports.CallExpression = function (node, parent, file) {
node.arguments.unshift(contextLiteral);
};
exports.NewExpression = function (node, parent, file) {
var args = node.arguments;
if (!hasSpread(args)) return;
var nodes = build(args, file);
var first = nodes.shift();
if (nodes.length) {
args = t.callExpression(t.memberExpression(first, t.identifier("concat")), nodes);
} else {
args = first;
}
return t.callExpression(file.addDeclaration("apply-constructor"), [node.callee, args]);
};

View File

@@ -12,6 +12,7 @@
"IfStatement": ["test", "consequent", "alternate"],
"Literal": ["value"],
"MemberExpression": ["object", "property", "computed"],
"NewExpression": ["callee", "arguments"],
"ObjectExpression": ["properties"],
"ParenthesizedExpression": ["expression"],
"Program": ["body"],