don't use function variable declaration for class constructor

This commit is contained in:
Sebastian McKenzie
2015-01-13 00:03:52 +11:00
parent 157bb48776
commit ea627ed57c
25 changed files with 89 additions and 54 deletions

View File

@@ -125,7 +125,7 @@ CodeGenerator.prototype.print = function (node, parent, opts) {
var lines = 0;
if (node.start != null) {
if (node.start != null && !node._ignoreUserWhitespace) {
// user node
if (leading) {
lines = self.whitespace.getNewlinesBefore(node);

View File

@@ -62,11 +62,15 @@ _.each({
CallExpression: { after: 1 },
Literal: { after: 1 }
}, function (amounts, type) {
if (_.isNumber(amounts)) amounts = { after: amounts, before: amounts };
if (_.isNumber(amounts)) {
amounts = { after: amounts, before: amounts };
}
_.each(amounts, function (amount, key) {
exports[key].nodes[type] = function () {
return amount;
};
});
_.each([type].concat(t.FLIPPED_ALIAS_KEYS[type] || []), function (type) {
_.each(amounts, function (amount, key) {
exports[key].nodes[type] = function () {
return amount;
};
});
})
});

View File

@@ -50,14 +50,18 @@ Class.prototype.run = function () {
var body = this.body = [];
var constructor = t.functionExpression(null, [], t.blockStatement([]));
if (this.node.id) constructor.id = className;
var constructor;
if (this.node.id) {
constructor = t.functionDeclaration(className, [], t.blockStatement([]));
body.push(constructor);
} else {
constructor = t.functionExpression(null, [], t.blockStatement([]));
body.push(t.variableDeclaration("var", [
t.variableDeclarator(className, constructor)
]));
}
this.constructor = constructor;
body.push(t.variableDeclaration("let", [
t.variableDeclarator(className, constructor)
]));
var closureArgs = [];
var closureParams = [];
@@ -85,7 +89,7 @@ Class.prototype.run = function () {
if (body.length === 1) {
// only a constructor so no need for a closure container
init = constructor;
init = t.toExpression(constructor);
} else {
body.push(t.returnStatement(className));
init = t.callExpression(
@@ -294,8 +298,9 @@ Class.prototype.pushConstructor = function (method) {
t.inherits(construct, fn);
t.inheritsComments(construct, method);
construct.defaults = fn.defaults;
construct.params = fn.params;
construct.body = fn.body;
construct.rest = fn.rest;
construct._ignoreUserWhitespace = true;
construct.defaults = fn.defaults;
construct.params = fn.params;
construct.body = fn.body;
construct.rest = fn.rest;
};

View File

@@ -9,6 +9,7 @@
"ExpressionStatement": ["expression"],
"File": ["program", "comments", "tokens"],
"FunctionExpression": ["id", "params", "body", "generator"],
"FunctionDeclaration": ["id", "params", "body", "generator"],
"Identifier": ["name"],
"IfStatement": ["test", "consequent", "alternate"],
"ImportDeclaration": ["specifiers", "source"],

View File

@@ -307,6 +307,31 @@ t.toStatement = function (node, ignore) {
return node;
};
/**
* Description
*
* @param {Object} node
* @returns {Object}
*/
exports.toExpression = function (node) {
if (t.isExpressionStatement(node)) {
node = node.expression;
}
if (t.isClass(node)) {
node.type = "ClassExpression";
} else if (t.isFunction(node)) {
node.type = "FunctionExpression";
}
if (t.isExpression(node)) {
return node;
} else {
throw new Error("cannot turn " + node.type + " to an expression");
}
};
/**
* Description
*