Merge pull request #3324 from zjmiller/master

Parenthize "in" in for-loop init, even when init has nested for-loop
This commit is contained in:
Henry Zhu 2016-02-05 15:50:11 -05:00
commit 4e619db873
5 changed files with 8 additions and 3 deletions

View File

@ -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) {

View File

@ -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) {

View File

@ -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);

View File

@ -1 +1,2 @@
for ((a in b) ? a : b; i;);
for (function(){for(;;);} && (a in b);;);

View File

@ -1 +1,4 @@
for ((a in b) ? a : b; i;);
for (function () {
for (;;);
} && (a in b);;);