Permalink
Browse files

Parenthize "in" in for-loop init, even when init has nested for-loop

  • Loading branch information...
zjmiller committed Feb 5, 2016
1 parent 98fa405 commit 33b4ef5d52151b1e9d18483c8951758ae56fc1d4
@@ -156,7 +156,7 @@ export function AssignmentPattern(node: Object) {
export function AssignmentExpression(node: Object, parent: Object) {
// Somewhere inside a for statement `init` node but doesn't usually
// needs a paren except for `in` expressions: `for (a in b ? a : b;;)`
let parens = this._inForStatementInit && node.operator === "in" &&
let parens = this._inForStatementInitCounter && node.operator === "in" &&
!n.needsParens(node, parent);
if (parens) {
@@ -52,9 +52,9 @@ export function ForStatement(node: Object) {
this.keyword("for");
this.push("(");
this._inForStatementInit = true;
this._inForStatementInitCounter++;
this.print(node.init, node);
this._inForStatementInit = false;
this._inForStatementInitCounter--;
this.push(";");
if (node.test) {
@@ -28,6 +28,7 @@ export class CodeGenerator extends Printer {
this.format = format;
this.opts = opts;
this.ast = ast;
this._inForStatementInitCounter = 0;
this.whitespace = new Whitespace(tokens);
this.map = new SourceMap(position, opts, code);
@@ -1 +1,2 @@
for ((a in b) ? a : b; i;);
for (function(){for(;;);} && (a in b);;);
@@ -1 +1,4 @@
for ((a in b) ? a : b; i;);
for (function () {
for (;;);
} && (a in b);;);

0 comments on commit 33b4ef5

Please sign in to comment.