Replace superIdentifier with superProperty.
This also disallows the usage of bare `super` that is not part of a member expression, call expression, or new expression.
This commit is contained in:
@@ -215,67 +215,45 @@ Class.prototype.pushMethod = function (node) {
|
||||
};
|
||||
|
||||
/**
|
||||
* Given a `methodNode`, produce a `MemberExpression` super class reference.
|
||||
* Gets a node representing the super class value of the named property.
|
||||
*
|
||||
* @param {Node} methodNode MethodDefinition
|
||||
* @param {Node} id Identifier
|
||||
* @param {Node} parent
|
||||
* @example
|
||||
*
|
||||
* _get(Object.getPrototypeOf(CLASS.prototype), "METHOD", this)
|
||||
*
|
||||
* @param {Node} property
|
||||
* @param {boolean} isStatic
|
||||
* @param {boolean} isComputed
|
||||
*
|
||||
* @returns {Node}
|
||||
*/
|
||||
|
||||
Class.prototype.superIdentifier = function (methodNode, id, parent) {
|
||||
var methodName = methodNode.key;
|
||||
var superName = this.superName || t.identifier("Function");
|
||||
var className = this.className;
|
||||
|
||||
if (parent.property === id) {
|
||||
return;
|
||||
} else if (t.isCallExpression(parent, { callee: id })) {
|
||||
// super(); -> _get(Object.getPrototypeOf(ClassName.prototype), "MethodName", this).call(this);
|
||||
var superProto = t.callExpression(
|
||||
t.memberExpression(
|
||||
t.identifier("Object"),
|
||||
t.identifier("getPrototypeOf"),
|
||||
false
|
||||
Class.prototype.superProperty = function (property, isStatic, isComputed) {
|
||||
return t.callExpression(
|
||||
this.file.addHelper("get"),
|
||||
[
|
||||
t.callExpression(
|
||||
t.memberExpression(
|
||||
t.identifier("Object"),
|
||||
t.identifier("getPrototypeOf"),
|
||||
false
|
||||
),
|
||||
[
|
||||
isStatic ?
|
||||
this.className :
|
||||
t.memberExpression(
|
||||
this.className,
|
||||
t.identifier("prototype"),
|
||||
false
|
||||
)
|
||||
]
|
||||
),
|
||||
[
|
||||
methodNode.static ?
|
||||
className :
|
||||
t.memberExpression(className, t.identifier("prototype"))
|
||||
]
|
||||
);
|
||||
var superCallee = t.callExpression(
|
||||
this.file.addHelper("get"),
|
||||
[
|
||||
superProto,
|
||||
methodNode.computed ? methodName : t.literal(methodName.name),
|
||||
t.thisExpression()
|
||||
]
|
||||
);
|
||||
|
||||
parent.arguments.unshift(t.thisExpression());
|
||||
|
||||
if (methodName.name === "constructor") {
|
||||
// constructor() { super(); }
|
||||
return t.memberExpression(superCallee, t.identifier("call"));
|
||||
} else {
|
||||
id = superName;
|
||||
|
||||
// foo() { super(); }
|
||||
//if (!methodNode.static) {
|
||||
// id = t.memberExpression(id, t.identifier("prototype"));
|
||||
//}
|
||||
|
||||
//id = t.memberExpression(id, methodName, methodNode.computed);
|
||||
return t.memberExpression(superCallee, t.identifier("call"));
|
||||
}
|
||||
} else if (t.isMemberExpression(parent) && !methodNode.static) {
|
||||
// super.test -> ClassName.prototype.test
|
||||
return t.memberExpression(superName, t.identifier("prototype"));
|
||||
} else {
|
||||
return superName;
|
||||
}
|
||||
isComputed ?
|
||||
property :
|
||||
t.literal(property.name),
|
||||
t.thisExpression()
|
||||
]
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -295,7 +273,9 @@ Class.prototype.replaceInstanceSuperReferences = function (methodNode) {
|
||||
var args;
|
||||
|
||||
if (t.isIdentifier(node, { name: "super" })) {
|
||||
return self.superIdentifier(methodNode, node, parent);
|
||||
if (!(t.isMemberExpression(parent) && !parent.computed && parent.property === node)) {
|
||||
throw self.file.errorWithNode(node, "illegal use of bare super");
|
||||
}
|
||||
} else if (t.isCallExpression(node)) {
|
||||
var callee = node.callee;
|
||||
if (t.isIdentifier(callee) && callee.name === "super") {
|
||||
@@ -324,7 +304,7 @@ Class.prototype.replaceInstanceSuperReferences = function (methodNode) {
|
||||
var superProperty = self.superProperty(property, methodNode.static, computed);
|
||||
if (args) {
|
||||
return t.callExpression(
|
||||
t.memberExpression(superProperty, t.identifier('call'), false),
|
||||
t.memberExpression(superProperty, t.identifier("call"), false),
|
||||
[t.thisExpression()].concat(args)
|
||||
);
|
||||
} else {
|
||||
@@ -335,48 +315,6 @@ Class.prototype.replaceInstanceSuperReferences = function (methodNode) {
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets a node representing the super class value of the named property.
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* _get(Object.getPrototypeOf(CLASS.prototype), "METHOD", this)
|
||||
*
|
||||
* @param {Node} property
|
||||
* @param {boolean} isStatic
|
||||
* @param {boolean} isComputed
|
||||
*
|
||||
* @returns {Node}
|
||||
*/
|
||||
|
||||
Class.prototype.superProperty = function (property, isStatic, isComputed) {
|
||||
return t.callExpression(
|
||||
this.file.addHelper('get'),
|
||||
[
|
||||
t.callExpression(
|
||||
t.memberExpression(
|
||||
t.identifier('Object'),
|
||||
t.identifier('getPrototypeOf'),
|
||||
false
|
||||
),
|
||||
[
|
||||
isStatic ?
|
||||
this.className :
|
||||
t.memberExpression(
|
||||
this.className,
|
||||
t.identifier('prototype'),
|
||||
false
|
||||
)
|
||||
]
|
||||
),
|
||||
isComputed ?
|
||||
property :
|
||||
t.literal(property.name),
|
||||
t.thisExpression()
|
||||
]
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Replace the constructor body of our class.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user