use Array.from instead of Array.prototype.slice in spread transformer and support NewExpression spreads - fixes #148
This commit is contained in:
@@ -1,5 +0,0 @@
|
||||
(function (obj) {
|
||||
var arr = [];
|
||||
for (var val of obj) arr.push(val);
|
||||
return arr;
|
||||
});
|
||||
@@ -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]);
|
||||
};
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
"IfStatement": ["test", "consequent", "alternate"],
|
||||
"Literal": ["value"],
|
||||
"MemberExpression": ["object", "property", "computed"],
|
||||
"NewExpression": ["callee", "arguments"],
|
||||
"ObjectExpression": ["properties"],
|
||||
"ParenthesizedExpression": ["expression"],
|
||||
"Program": ["body"],
|
||||
|
||||
Reference in New Issue
Block a user