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"],

View File

@@ -1,6 +1,5 @@
"use strict";
var _slice = Array.prototype.slice;
var _classProps = function (child, staticProps, instanceProps) {
if (staticProps) Object.defineProperties(child, staticProps);
@@ -26,11 +25,11 @@ var Test = (function (Foo) {
Foo.prototype.test.call(this);
foob(Foo);
Foo.call.apply(Foo, [this].concat(_slice.call(arguments)));
Foo.call.apply(Foo, [this, "test"].concat(_slice.call(arguments)));
Foo.call.apply(Foo, [this].concat(Array.from(arguments)));
Foo.call.apply(Foo, [this, "test"].concat(Array.from(arguments)));
Foo.prototype.test.call.apply(Foo.prototype, [this].concat(_slice.call(arguments)));
Foo.prototype.test.call.apply(Foo.prototype, [this, "test"].concat(_slice.call(arguments)));
Foo.prototype.test.call.apply(Foo.prototype, [this].concat(Array.from(arguments)));
Foo.prototype.test.call.apply(Foo.prototype, [this, "test"].concat(Array.from(arguments)));
};
_extends(Test, Foo);
@@ -40,8 +39,8 @@ var Test = (function (Foo) {
writable: true,
value: function () {
Foo.foo.call(this);
Foo.foo.call.apply(Foo.foo, [this].concat(_slice.call(arguments)));
Foo.foo.call.apply(Foo.foo, [this, "test"].concat(_slice.call(arguments)));
Foo.foo.call.apply(Foo.foo, [this].concat(Array.from(arguments)));
Foo.foo.call.apply(Foo.foo, [this, "test"].concat(Array.from(arguments)));
}
}
}, {
@@ -49,8 +48,8 @@ var Test = (function (Foo) {
writable: true,
value: function () {
Foo.prototype.test.call(this);
Foo.prototype.test.call.apply(Foo.prototype.test, [this].concat(_slice.call(arguments)));
Foo.prototype.test.call.apply(Foo.prototype.test, [this, "test"].concat(_slice.call(arguments)));
Foo.prototype.test.call.apply(Foo.prototype.test, [this].concat(Array.from(arguments)));
Foo.prototype.test.call.apply(Foo.prototype.test, [this, "test"].concat(Array.from(arguments)));
}
}
});

View File

@@ -3,4 +3,4 @@
var _slice = Array.prototype.slice;
var concat = function () {
var arrs = _slice.call(arguments);
};
};

View File

@@ -7,4 +7,4 @@ var t = function (f) {
function t(f) {
var items = _slice.call(arguments, 1);
}
}

View File

@@ -7,4 +7,4 @@ var t = function () {
function t() {
var items = _slice.call(arguments);
}
}

View File

@@ -1,12 +1,11 @@
"use strict";
var _slice = Array.prototype.slice;
function foo() {
return bar.apply(null, ["test"].concat(_slice.call(arguments)));
return bar.apply(null, ["test"].concat(Array.from(arguments)));
}
function bar(one, two, three) {
return [one, two, three];
}
foo("foo", "bar");
foo("foo", "bar");

View File

@@ -1,4 +1,3 @@
"use strict";
var _slice = Array.prototype.slice;
var lyrics = _slice.call(parts).concat(["head", "and", "toes"]);
var lyrics = Array.from(parts).concat(["head", "and", "toes"]);

View File

@@ -1,4 +1,3 @@
"use strict";
var _slice = Array.prototype.slice;
var a = [b].concat(_slice.call(c), [d]);
var a = [b].concat(Array.from(c), [d]);

View File

@@ -1,4 +1,3 @@
"use strict";
var _slice = Array.prototype.slice;
var a = [b].concat(_slice.call(c), [d, e], _slice.call(f));
var a = [b].concat(Array.from(c), [d, e], Array.from(f));

View File

@@ -1,4 +1,3 @@
"use strict";
var _slice = Array.prototype.slice;
var lyrics = ["head", "and", "toes"].concat(_slice.call(parts));
var lyrics = ["head", "and", "toes"].concat(Array.from(parts));

View File

@@ -1,4 +1,3 @@
"use strict";
var _slice = Array.prototype.slice;
obj[method].apply(obj, [foo, bar].concat(_slice.call(args)));
obj[method].apply(obj, [foo, bar].concat(Array.from(args)));

View File

@@ -1,4 +1,3 @@
"use strict";
var _slice = Array.prototype.slice;
obj[method].apply(obj, _slice.call(args));
obj[method].apply(obj, Array.from(args));

View File

@@ -1,5 +1,4 @@
"use strict";
var _slice = Array.prototype.slice;
foob.add.apply(foob, [foo, bar].concat(_slice.call(numbers)));
foob.test.add.apply(foob.test, [foo, bar].concat(_slice.call(numbers)));
foob.add.apply(foob, [foo, bar].concat(Array.from(numbers)));
foob.test.add.apply(foob.test, [foo, bar].concat(Array.from(numbers)));

View File

@@ -1,5 +1,4 @@
"use strict";
var _slice = Array.prototype.slice;
foob.add.apply(foob, _slice.call(numbers));
foob.test.add.apply(foob.test, _slice.call(numbers));
foob.add.apply(foob, Array.from(numbers));
foob.test.add.apply(foob.test, Array.from(numbers));

View File

@@ -1,4 +1,3 @@
"use strict";
var _slice = Array.prototype.slice;
add.apply(null, _slice.call(numbers).concat([foo, bar]));
add.apply(null, Array.from(numbers).concat([foo, bar]));

View File

@@ -1,4 +1,3 @@
"use strict";
var _slice = Array.prototype.slice;
add.apply(null, [foo].concat(_slice.call(numbers), [bar]));
add.apply(null, [foo].concat(Array.from(numbers), [bar]));

View File

@@ -1,4 +1,3 @@
"use strict";
var _slice = Array.prototype.slice;
add.apply(null, [foo, bar].concat(_slice.call(numbers)));
add.apply(null, [foo, bar].concat(Array.from(numbers)));

View File

@@ -1,4 +1,3 @@
"use strict";
var _slice = Array.prototype.slice;
add.apply(null, [foo].concat(_slice.call(numbers), [bar, what], _slice.call(test)));
add.apply(null, [foo].concat(Array.from(numbers), [bar, what], Array.from(test)));

View File

@@ -1,4 +1,3 @@
"use strict";
var _slice = Array.prototype.slice;
add.apply(null, _slice.call(numbers));
add.apply(null, Array.from(numbers));

View File

@@ -0,0 +1,2 @@
new Numbers(...nums);
new Numbers(1, ...nums);

View File

@@ -0,0 +1,12 @@
"use strict";
var _applyConstructor = function (Constructor, args) {
var bindArgs = [null].concat(args);
var Factory = Constructor.bind.apply(Constructor, bindArgs);
return new Factory();
};
_applyConstructor(Numbers, Array.from(nums));
_applyConstructor(Numbers, [1].concat(Array.from(nums)));