fix spread and super resolution - fixes #42
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
foob.add(foo, bar, ...numbers);
|
||||
foob.test.add(foo, bar, ...numbers);
|
||||
|
||||
@@ -2,3 +2,7 @@ foob.add.apply(foob, [
|
||||
foo,
|
||||
bar
|
||||
].concat(numbers));
|
||||
foob.test.add.apply(foob.test, [
|
||||
foo,
|
||||
bar
|
||||
].concat(numbers));
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
foob.add(...numbers);
|
||||
foob.test.add(...numbers);
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
foob.add.apply(foob, numbers);
|
||||
foob.test.add.apply(foob.test, numbers);
|
||||
|
||||
Reference in New Issue
Block a user