HomeObject isn't dynamic - fixes #690

This commit is contained in:
Sebastian McKenzie
2015-02-04 18:47:58 +11:00
parent 39fe737cb6
commit d6b39bc89b
13 changed files with 84 additions and 113 deletions

View File

@@ -183,6 +183,7 @@ File.prototype.buildTransformers = function () {
if (pass.canRun(file)) {
stack.push(pass);
if (transformer.secondPass) {
secondaryStack.push(pass);
}

View File

@@ -38,12 +38,7 @@ ReplaceSupers.prototype.setSuperProperty = function (property, value, isStatic,
return t.callExpression(
this.file.addHelper("set"),
[
t.callExpression(
t.memberExpression(t.identifier("Object"), t.identifier("getPrototypeOf")),
[
isStatic ? this.className : t.memberExpression(this.className, t.identifier("prototype"))
]
),
isStatic ? this.superName : t.memberExpression(this.superName, t.identifier("prototype")),
isComputed ? property : t.literal(property.name),
value,
thisExpression
@@ -65,63 +60,17 @@ ReplaceSupers.prototype.setSuperProperty = function (property, value, isStatic,
* @returns {Node}
*/
ReplaceSupers.prototype.superProperty = function (property, isStatic, isComputed, thisExpression) {
ReplaceSupers.prototype.getSuperProperty = function (property, isStatic, isComputed, thisExpression) {
return t.callExpression(
this.file.addHelper("get"),
[
t.callExpression(
t.memberExpression(t.identifier("Object"), t.identifier("getPrototypeOf")),
[
isStatic ? this.className : t.memberExpression(this.className, t.identifier("prototype"))
]
),
isStatic ? this.superName : t.memberExpression(this.superName, t.identifier("prototype")),
isComputed ? property : t.literal(property.name),
thisExpression
]
);
};
/**
* Description
*
* @param {Object} node
* @param {Object} id
* @param {Object} parent
* @returns {Object}
*/
ReplaceSupers.prototype.looseSuperProperty = function (methodNode, id, parent) {
var methodName = methodNode.key;
var superName = this.superName || t.identifier("Function");
if (parent.property === id) {
return;
} else if (t.isCallExpression(parent, { callee: id })) {
// super(); -> ClassName.prototype.MethodName.call(this);
parent.arguments.unshift(t.thisExpression());
if (methodName.name === "constructor") {
// constructor() { super(); }
return t.memberExpression(superName, 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(id, 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;
}
};
/**
* Description
*/
@@ -186,6 +135,47 @@ ReplaceSupers.prototype.getThisReference = function () {
}
};
/**
* Description
*
* @param {Object} node
* @param {Object} id
* @param {Object} parent
* @returns {Object}
*/
ReplaceSupers.prototype.getLooseSuperProperty = function (methodNode, id, parent) {
var methodName = methodNode.key;
var superName = this.superName || t.identifier("Function");
if (parent.property === id) {
return;
} else if (t.isCallExpression(parent, { callee: id })) {
// super(); -> ClassName.prototype.MethodName.call(this);
parent.arguments.unshift(t.thisExpression());
if (methodName.name === "constructor") {
// constructor() { super(); }
return t.memberExpression(superName, 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(id, 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;
}
};
/**
* Description
*
@@ -196,7 +186,7 @@ ReplaceSupers.prototype.getThisReference = function () {
ReplaceSupers.prototype.looseHandle = function (getThisReference, node, parent) {
if (t.isIdentifier(node, { name: "super" })) {
return this.looseSuperProperty(this.methodNode, node, parent);
return this.getLooseSuperProperty(this.methodNode, node, parent);
} else if (t.isCallExpression(node)) {
var callee = node.callee;
if (!t.isMemberExpression(callee)) return;
@@ -269,7 +259,7 @@ ReplaceSupers.prototype.specHandle = function (getThisReference, node, parent) {
if (!property) return;
thisReference = getThisReference();
var superProperty = this.superProperty(property, methodNode.static, computed, thisReference);
var superProperty = this.getSuperProperty(property, methodNode.static, computed, thisReference);
if (args) {
if (args.length === 1 && t.isSpreadElement(args[0])) {
// super(...arguments);

View File

@@ -1,3 +1,3 @@
if (Object.getPrototypeOf(CLASS_NAME) !== null) {
Object.getPrototypeOf(CLASS_NAME).apply(this, arguments);
if (SUPER_NAME != null) {
SUPER_NAME.apply(this, arguments);
}

View File

@@ -50,7 +50,8 @@ function Class(node, file, scope, isStatement) {
this.staticMutatorMap = {};
this.hasConstructor = false;
this.className = node.id || scope.generateUidIdentifier("class");
this.superName = node.superClass;
this.superName = node.superClass || t.identifier("Function");
this.hasSuper = !!node.superClass;
this.isLoose = file.isLoose("es6.classes");
}
@@ -93,7 +94,7 @@ Class.prototype.run = function () {
//
if (superName) {
if (this.hasSuper) {
closureArgs.push(superName);
if (!t.isIdentifier(superName)) {
@@ -169,7 +170,7 @@ Class.prototype.buildBody = function () {
}
// we have no constructor, we have a super, and the super doesn't appear to be falsy
if (!this.hasConstructor && superName && !t.isFalsyExpression(superName)) {
if (!this.hasConstructor && this.hasSuper && !t.isFalsyExpression(superName)) {
var helperName = "class-super-constructor-call";
if (this.isLoose) helperName += "-loose";
constructor.body.body.push(util.template(helperName, {

View File

@@ -7,7 +7,7 @@ var t = require("../../../types");
exports.experimental = true;
exports.manipulateOptions = function (opts) {
opts.whitelist.push("es6.destructuring");
if (opts.whitelist.length) opts.whitelist.push("es6.destructuring");
};
var hasSpread = function (node) {