better whitespace for code generation
This commit is contained in:
@@ -9,15 +9,15 @@ exports.ComprehensionBlock = function (node, print) {
|
||||
|
||||
exports.ComprehensionExpression = function (node, print) {
|
||||
this.push("[");
|
||||
this.printJoin(print, node.blocks, " ");
|
||||
this.push(" ");
|
||||
this.printJoin(print, node.blocks, { separator: " " });
|
||||
this.space();
|
||||
|
||||
if (node.filter) {
|
||||
this.keyword("if");
|
||||
this.push("(");
|
||||
print(node.filter);
|
||||
this.push(")");
|
||||
this.push(" ");
|
||||
this.space();
|
||||
}
|
||||
|
||||
print(node.body);
|
||||
|
||||
@@ -13,7 +13,7 @@ exports.BlockStatement = function (node, print) {
|
||||
this.push("{");
|
||||
this.newline();
|
||||
print.sequence(node.body, { indent: true });
|
||||
this.newline(true);
|
||||
this.push("}");
|
||||
this.removeLast("\n");
|
||||
this.rightBrace();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -3,7 +3,7 @@ exports.ClassDeclaration = function (node, print) {
|
||||
this.push("class");
|
||||
|
||||
if (node.id) {
|
||||
this.push(" ");
|
||||
this.space();
|
||||
print(node.id);
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ exports.ClassDeclaration = function (node, print) {
|
||||
print(node.superClass);
|
||||
}
|
||||
|
||||
this.push(" ");
|
||||
this.space();
|
||||
print(node.body);
|
||||
};
|
||||
|
||||
@@ -27,7 +27,6 @@ exports.ClassBody = function (node, print) {
|
||||
print.sequence(node.body);
|
||||
this.dedent();
|
||||
|
||||
this.newline(true);
|
||||
this.push("}");
|
||||
this.rightBrace();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
var t = require("../types");
|
||||
|
||||
exports.UnaryExpression = function (node, print) {
|
||||
this.push(node.operator);
|
||||
|
||||
var hasSpace = /[a-z]$/.test(node.operator);
|
||||
var arg = node.argument;
|
||||
if (/[a-z]$/.test(node.operator) || t.isUpdateExpression(arg) || t.isUnaryExpression(arg)) {
|
||||
this.push(" ");
|
||||
|
||||
if (t.isUpdateExpression(arg) || t.isUnaryExpression(arg)) {
|
||||
hasSpace = true;
|
||||
}
|
||||
|
||||
if (t.isUnaryExpression(arg) && arg.operator === "!") {
|
||||
hasSpace = false;
|
||||
}
|
||||
|
||||
this.push(node.operator);
|
||||
if (hasSpace) this.space();
|
||||
print(node.argument);
|
||||
};
|
||||
|
||||
@@ -29,18 +35,16 @@ exports.ConditionalExpression = function (node, print) {
|
||||
print(node.alternate);
|
||||
};
|
||||
|
||||
exports.NewExpression = function (node, print) {
|
||||
exports.NewExpression = function (node, print, parent) {
|
||||
this.push("new ");
|
||||
print(node.callee);
|
||||
if (node.arguments.length) {
|
||||
this.push("(");
|
||||
this.printJoin(print, node.arguments, ", ");
|
||||
this.push(")");
|
||||
}
|
||||
this.push("(");
|
||||
this.printJoin(print, node.arguments, { separator: ", " });
|
||||
this.push(")");
|
||||
};
|
||||
|
||||
exports.SequenceExpression = function (node, print) {
|
||||
this.printJoin(print, node.expressions, ", ");
|
||||
this.printJoin(print, node.expressions, { separator: ", " });
|
||||
};
|
||||
|
||||
exports.ThisExpression = function () {
|
||||
@@ -50,7 +54,7 @@ exports.ThisExpression = function () {
|
||||
exports.CallExpression = function (node, print) {
|
||||
print(node.callee);
|
||||
this.push("(");
|
||||
this.printJoin(print, node.arguments, ", ");
|
||||
this.printJoin(print, node.arguments, { separator: ", " });
|
||||
this.push(")");
|
||||
};
|
||||
|
||||
@@ -58,7 +62,7 @@ exports.YieldExpression = function (node, print) {
|
||||
this.push("yield");
|
||||
if (node.delegate) this.push("*");
|
||||
if (node.argument) {
|
||||
this.push(" ");
|
||||
this.space();
|
||||
print(node.argument);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -65,8 +65,8 @@ exports.XJSOpeningElement = function (node, print) {
|
||||
this.push("<");
|
||||
print(node.name);
|
||||
if (node.attributes.length > 0) {
|
||||
this.push(" ");
|
||||
this.printJoin(print, node.attributes, " ");
|
||||
this.space();
|
||||
this.printJoin(print, node.attributes, { separator: " " });
|
||||
}
|
||||
this.push(node.selfClosing ? " />" : ">");
|
||||
};
|
||||
|
||||
@@ -5,7 +5,8 @@ exports._params = function (node, print) {
|
||||
|
||||
this.push("(");
|
||||
|
||||
this.printJoin(print, node.params, ", ", {
|
||||
this.printJoin(print, node.params, {
|
||||
separator: ", ",
|
||||
iterator: function (param, i) {
|
||||
var def = node.defaults && node.defaults[i];
|
||||
if (def) {
|
||||
@@ -49,7 +50,7 @@ exports._method = function (node, print) {
|
||||
}
|
||||
|
||||
this._params(value, print);
|
||||
this.push(" ");
|
||||
this.space();
|
||||
print(value.body);
|
||||
};
|
||||
|
||||
@@ -64,7 +65,7 @@ exports.MethodDefinition = function (node, print) {
|
||||
exports.ReturnStatement = function (node, print) {
|
||||
this.push("return");
|
||||
if (node.argument) {
|
||||
this.push(" ");
|
||||
this.space();
|
||||
print(node.argument);
|
||||
}
|
||||
this.semicolon();
|
||||
@@ -74,10 +75,10 @@ exports.FunctionDeclaration =
|
||||
exports.FunctionExpression = function (node, print) {
|
||||
this.push("function");
|
||||
if (node.generator) this.push("*");
|
||||
this.push(" ");
|
||||
this.space();
|
||||
if (node.id) print(node.id);
|
||||
this._params(node, print);
|
||||
this.push(" ");
|
||||
this.space();
|
||||
print(node.body);
|
||||
};
|
||||
|
||||
|
||||
@@ -31,9 +31,9 @@ exports.ExportDeclaration = function (node, print) {
|
||||
} else {
|
||||
this.push("{");
|
||||
if (specifiers.length) {
|
||||
this.push(" ");
|
||||
this.printJoin(print, specifiers, ", ");
|
||||
this.push(" ");
|
||||
this.space();
|
||||
this.printJoin(print, specifiers, { separator: ", " });
|
||||
this.space();
|
||||
}
|
||||
this.push("}");
|
||||
}
|
||||
|
||||
@@ -13,12 +13,12 @@ exports.IfStatement = function (node, print) {
|
||||
print(node.test);
|
||||
this.push(") ");
|
||||
|
||||
print(node.consequent);
|
||||
this.printAndIndentOnComments(print, node.consequent);
|
||||
|
||||
if (node.alternate) {
|
||||
if (this.isLast("}")) this.push(" ");
|
||||
if (this.isLast("}")) this.space();
|
||||
this.keyword("else");
|
||||
print(node.alternate);
|
||||
this.printAndIndentOnComments(print, node.alternate);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -30,13 +30,13 @@ exports.ForStatement = function (node, print) {
|
||||
this.push(";");
|
||||
|
||||
if (node.test) {
|
||||
this.push(" ");
|
||||
this.space();
|
||||
print(node.test);
|
||||
}
|
||||
this.push(";");
|
||||
|
||||
if (node.update) {
|
||||
this.push(" ");
|
||||
this.space();
|
||||
print(node.update);
|
||||
}
|
||||
|
||||
@@ -73,10 +73,11 @@ exports.ForOfStatement = function (node, print) {
|
||||
};
|
||||
|
||||
exports.DoWhileStatement = function (node, print) {
|
||||
this.push("do ");
|
||||
this.keyword("do");
|
||||
print(node.body);
|
||||
this.push(" while");
|
||||
this.push(" (");
|
||||
this.space();
|
||||
this.keyword("while");
|
||||
this.push("(");
|
||||
print(node.test);
|
||||
this.push(");");
|
||||
};
|
||||
@@ -84,7 +85,7 @@ exports.DoWhileStatement = function (node, print) {
|
||||
exports.BreakStatement = function (node, print) {
|
||||
this.push("break");
|
||||
if (node.label) {
|
||||
this.push(" ");
|
||||
this.space();
|
||||
print(node.label);
|
||||
}
|
||||
this.semicolon();
|
||||
@@ -93,7 +94,7 @@ exports.BreakStatement = function (node, print) {
|
||||
exports.ContinueStatement = function (node, print) {
|
||||
this.push("continue");
|
||||
if (node.label) {
|
||||
this.push(" ");
|
||||
this.space();
|
||||
print(node.label);
|
||||
}
|
||||
this.semicolon();
|
||||
@@ -108,10 +109,11 @@ exports.LabeledStatement = function (node, print) {
|
||||
exports.TryStatement = function (node, print) {
|
||||
this.keyword("try");
|
||||
print(node.block);
|
||||
this.push(" ");
|
||||
this.space();
|
||||
print(node.handler);
|
||||
if (node.finalizer) {
|
||||
this.push(" finally ");
|
||||
this.space();
|
||||
this.push("finally ");
|
||||
print(node.finalizer);
|
||||
}
|
||||
};
|
||||
@@ -135,14 +137,17 @@ exports.SwitchStatement = function (node, print) {
|
||||
this.push("(");
|
||||
print(node.discriminant);
|
||||
this.push(") {");
|
||||
|
||||
if (node.cases.length > 0) {
|
||||
this.newline();
|
||||
print.sequence(node.cases, { indent: true });
|
||||
this.newline();
|
||||
}
|
||||
|
||||
print.sequence(node.cases, { indent: true });
|
||||
this.push("}");
|
||||
|
||||
//if (node.cases.length) {
|
||||
// this.newline();
|
||||
// print.sequence(node.cases, { indent: true });
|
||||
// this.newline();
|
||||
// this.rightBrace();
|
||||
//} else {
|
||||
// this.push("}");
|
||||
//}
|
||||
};
|
||||
|
||||
exports.SwitchCase = function (node, print) {
|
||||
@@ -155,7 +160,7 @@ exports.SwitchCase = function (node, print) {
|
||||
}
|
||||
|
||||
if (node.consequent.length === 1) {
|
||||
this.push(" ");
|
||||
this.space();
|
||||
print(node.consequent[0]);
|
||||
} else if (node.consequent.length > 1) {
|
||||
this.newline();
|
||||
@@ -170,7 +175,7 @@ exports.DebuggerStatement = function () {
|
||||
exports.VariableDeclaration = function (node, print, parent) {
|
||||
this.push(node.kind + " ");
|
||||
|
||||
this.printJoin(print, node.declarations, ", ");
|
||||
this.printJoin(print, node.declarations, { separator: ", " });
|
||||
|
||||
if (!t.isFor(parent)) {
|
||||
this.semicolon();
|
||||
|
||||
@@ -6,7 +6,7 @@ exports.TaggedTemplateExpression = function (node, print) {
|
||||
};
|
||||
|
||||
exports.TemplateElement = function (node) {
|
||||
this.push(node.value.raw, true);
|
||||
this._push(node.value.raw);
|
||||
};
|
||||
|
||||
exports.TemplateLiteral = function (node, print) {
|
||||
|
||||
@@ -15,17 +15,11 @@ exports.ObjectPattern = function (node, print) {
|
||||
|
||||
if (props.length) {
|
||||
this.push("{");
|
||||
this.space();
|
||||
|
||||
if (props.length === 1) {
|
||||
this.push(" ");
|
||||
this.print(props[0]);
|
||||
this.push(" ");
|
||||
} else {
|
||||
this.newline();
|
||||
this.printJoin(print, props, ",\n", { indent: true });
|
||||
this.newline();
|
||||
}
|
||||
this.printJoin(print, props, { separator: ", ", indent: true });
|
||||
|
||||
this.space();
|
||||
this.push("}");
|
||||
} else {
|
||||
this.push("{}");
|
||||
|
||||
Reference in New Issue
Block a user