just return constructor if only a constructor exists in classes
This commit is contained in:
@@ -42,10 +42,9 @@ var buildClass = function (node, file) {
|
||||
|
||||
var block = container.callee.body;
|
||||
var body = block.body;
|
||||
var construct = body[0].declarations[0].init;
|
||||
|
||||
if (node.id) {
|
||||
body[0].declarations[0].init.id = className;
|
||||
}
|
||||
if (node.id) construct.id = className;
|
||||
|
||||
var returnStatement = body.pop();
|
||||
|
||||
@@ -56,19 +55,22 @@ var buildClass = function (node, file) {
|
||||
container.callee.params.push(superClassCallee);
|
||||
}
|
||||
|
||||
buildClassBody(body, className, superName, node);
|
||||
buildClassBody(construct, body, className, superName, node);
|
||||
|
||||
body.push(returnStatement);
|
||||
|
||||
return container;
|
||||
if (body.length === 1) {
|
||||
// only a constructor so no need for a closure container
|
||||
return construct;
|
||||
} else {
|
||||
body.push(returnStatement);
|
||||
return container;
|
||||
}
|
||||
};
|
||||
|
||||
var buildClassBody = function (body, className, superName, node) {
|
||||
var buildClassBody = function (construct, body, className, superName, node) {
|
||||
var instanceMutatorMap = {};
|
||||
var staticMutatorMap = {};
|
||||
var hasConstructor = false;
|
||||
|
||||
var construct = body[0].declarations[0].init;
|
||||
var classBody = node.body.body;
|
||||
|
||||
_.each(classBody, function (node) {
|
||||
@@ -127,21 +129,21 @@ var superIdentifier = function (superName, methodNode, methodName, node, parent)
|
||||
|
||||
if (methodName === "constructor") {
|
||||
// constructor() { super(); }
|
||||
return b.memberExpression(superName, b.identifier("call"), false);
|
||||
return b.memberExpression(superName, b.identifier("call"));
|
||||
} else {
|
||||
node = superName;
|
||||
|
||||
// foo() { super(); }
|
||||
if (!methodNode.static) {
|
||||
node = b.memberExpression(node, b.identifier("prototype"), false);
|
||||
node = b.memberExpression(node, b.identifier("prototype"));
|
||||
}
|
||||
|
||||
node = b.memberExpression(node, b.identifier(methodName), false);
|
||||
return b.memberExpression(node, b.identifier("call"), false);
|
||||
node = b.memberExpression(node, b.identifier(methodName));
|
||||
return b.memberExpression(node, b.identifier("call"));
|
||||
}
|
||||
} else if (parent.type === "MemberExpression" && !methodNode.static) {
|
||||
// super.test -> ClassName.prototype.test
|
||||
return b.memberExpression(superName, b.identifier("prototype"), false);
|
||||
return b.memberExpression(superName, b.identifier("prototype"));
|
||||
} else {
|
||||
return superName;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user