Parenthesize statement argument when on a different line

This commit is contained in:
Amjad Masad
2015-04-30 05:43:08 -07:00
parent a540cbe801
commit 4c2ae5dd19
2 changed files with 14 additions and 4 deletions

View File

@@ -147,13 +147,22 @@ class CodeGenerator {
return print;
}
catchUp(node) {
catchUp(node, parent) {
// catch up to this nodes newline if we're behind
if (node.loc && this.format.retainLines && this.buffer.buf) {
var needsParens = false;
if (parent && (this.position.line < node.loc.start.line) &&
(t.isContinueStatement(parent) || t.isBreakStatement(parent) ||
t.isReturnStatement(parent) || t.isThrowStatement(parent))) {
needsParens = true;
this._push("(");
}
while (this.position.line < node.loc.start.line) {
this._push("\n");
}
return needsParens;
}
return false;
}
print(node, parent, opts = {}) {
@@ -207,7 +216,7 @@ class CodeGenerator {
this.printLeadingComments(node, parent);
this.catchUp(node);
var needsParensFromCatchup = this.catchUp(node, parent);
newline(true);
@@ -220,7 +229,7 @@ class CodeGenerator {
this.newline();
this.dedent();
}
if (needsParens) this.push(")");
if (needsParens || needsParensFromCatchup) this.push(")");
this.map.mark(node, "end");
if (opts.after) opts.after();