Skip to content

Commit

Permalink
Merge pull request #3324 from zjmiller/master
Browse files Browse the repository at this point in the history
Parenthize "in" in for-loop init, even when init has nested for-loop
  • Loading branch information
hzoo committed Feb 5, 2016
2 parents a757e26 + 33b4ef5 commit 4e619db
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/babel-generator/src/generators/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-generator/src/generators/statements.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions packages/babel-generator/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
for ((a in b) ? a : b; i;);
for (function(){for(;;);} && (a in b);;);
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
for ((a in b) ? a : b; i;);
for (function () {
for (;;);
} && (a in b);;);

0 comments on commit 4e619db

Please sign in to comment.