fix spread and super resolution - fixes #42

This commit is contained in:
Sebastian McKenzie
2014-10-11 10:44:51 +11:00
parent 603ae290cd
commit 3fb17b00a6
8 changed files with 44 additions and 11 deletions

View File

@@ -133,19 +133,23 @@ var superIdentifier = function (superName, methodNode, methodName, node, parent)
// super(); -> ClassName.prototype.MethodName.call(this);
if (parent.type === "CallExpression" && parent.callee === node) {
parent.arguments.unshift(b.thisExpression());
if (methodName === "constructor") {
// constructor() { super(); }
node.name += ".call";
return b.memberExpression(node, b.identifier("call"), false);
} else {
// foo() { super(); }
if (!methodNode.static) node.name += ".prototype";
node.name += "." + methodName + ".call";
}
if (!methodNode.static) {
node = b.memberExpression(node, b.identifier("prototype"), false);
}
parent.arguments.unshift(b.thisExpression());
node = b.memberExpression(node, b.identifier(methodName), false);
return b.memberExpression(node, b.identifier("call"), false);
}
} else if (parent.type === "MemberExpression") {
// super.test -> ClassName.prototype.test
node.name += ".prototype";
return b.memberExpression(node, b.identifier("prototype"), false);
}
};
@@ -154,7 +158,7 @@ var replaceInstanceSuperReferences = function (superName, method, methodNode, me
traverse(method, function (node, parent) {
if (node.type === "Identifier" && node.name === "super") {
superIdentifier(superName, methodNode, methodName, node, parent);
return superIdentifier(superName, methodNode, methodName, node, parent);
} else if (node.type === "CallExpression") {
var callee = node.callee;
if (callee.type !== "MemberExpression") return;

View File

@@ -3,8 +3,6 @@ var b = require("ast-types").builders;
var _ = require("lodash");
exports.ArrayExpression = function (node) {
//if (node.ignoreSpread) return;
var elements = node.elements;
if (!elements.length) return;
@@ -51,9 +49,9 @@ exports.CallExpression = function (node) {
if (callee.type === "MemberExpression") {
contextLiteral = callee.object;
callee.property.name += ".apply";
callee.property = b.memberExpression(callee.property, b.identifier("apply"), false);
} else {
node.callee.name += ".apply";
node.callee = b.memberExpression(node.callee, b.identifier("apply"), false);
}
node.arguments.unshift(contextLiteral);

View File

@@ -4,13 +4,23 @@ class Test extends Foo {
super();
super.test();
foob(super);
super(...arguments);
super("test", ...arguments);
super.test(...arguments);
super.test("test", ...arguments);
}
test() {
super();
super(...arguments);
super("test", ...arguments);
}
static foo() {
super();
super(...arguments);
super("test", ...arguments);
}
}

View File

@@ -4,6 +4,13 @@ var Test = function (Foo) {
Foo.call(this);
Foo.prototype.test.call(this);
foob(Foo);
Foo.call.apply(Foo, [this].concat(Array.prototype.slice.call(arguments)));
Foo.call.apply(Foo, [this, "test"].concat(Array.prototype.slice.call(arguments)));
Foo.prototype.test.call.apply(Foo.prototype, [this].concat(Array.prototype.slice.call(arguments)));
Foo.prototype.test.call.apply(
Foo.prototype,
[this, "test"].concat(Array.prototype.slice.call(arguments))
);
}
Test.prototype = Object.create(Foo.prototype, {
constructor: {
@@ -16,9 +23,16 @@ var Test = function (Foo) {
Test.__proto__ = Foo;
Test.prototype.test = function () {
Foo.prototype.test.call(this);
Foo.prototype.test.call.apply(Foo.prototype.test, [this].concat(Array.prototype.slice.call(arguments)));
Foo.prototype.test.call.apply(
Foo.prototype.test,
[this, "test"].concat(Array.prototype.slice.call(arguments))
);
};
Test.foo = function () {
Foo.foo.call(this);
Foo.foo.call.apply(Foo.foo, [this].concat(Array.prototype.slice.call(arguments)));
Foo.foo.call.apply(Foo.foo, [this, "test"].concat(Array.prototype.slice.call(arguments)));
};
return Test;
}(Foo);

View File

@@ -1 +1,2 @@
foob.add(foo, bar, ...numbers);
foob.test.add(foo, bar, ...numbers);

View File

@@ -2,3 +2,7 @@ foob.add.apply(foob, [
foo,
bar
].concat(numbers));
foob.test.add.apply(foob.test, [
foo,
bar
].concat(numbers));

View File

@@ -1 +1,2 @@
foob.add(...numbers);
foob.test.add(...numbers);

View File

@@ -1 +1,2 @@
foob.add.apply(foob, numbers);
foob.test.add.apply(foob.test, numbers);