Merge pull request #2606 from amasad/for-in-edge-case

Special case paren printing in for-loop init node
This commit is contained in:
Sebastian McKenzie 2015-10-27 23:32:55 +00:00
commit 86b4f0dbe8
4 changed files with 19 additions and 1 deletions

View File

@ -1,5 +1,6 @@
import isInteger from "is-integer";
import isNumber from "lodash/lang/isNumber";
import n from "../node"
import * as t from "../../types";
/**
@ -220,7 +221,16 @@ export function AssignmentPattern(node, print) {
* Prints AssignmentExpression, prints left, operator, and right.
*/
export function AssignmentExpression(node, print) {
export function AssignmentExpression(node, print, parent) {
// Somewhere inside a for statement `init` node but doesn't usually
// needs a paren except for `in` expressions: `for (a in b ? a : b;;)`
var parens = this._inForStatementInit && node.operator === "in" &&
!n.needsParens(node, parent);
if (parens) {
this.push("(");
}
// todo: add cases where the spaces can be dropped when in compact mode
print.plain(node.left);
@ -241,6 +251,10 @@ export function AssignmentExpression(node, print) {
this.space(spaces);
print.plain(node.right);
if (parens) {
this.push(")");
}
}
/**

View File

@ -41,7 +41,9 @@ export function ForStatement(node, print) {
this.keyword("for");
this.push("(");
this._inForStatementInit = true;
print.plain(node.init);
this._inForStatementInit = false;
this.push(";");
if (node.test) {

View File

@ -0,0 +1 @@
for ((a in b) ? a : b; i;);

View File

@ -0,0 +1 @@
for ((a in b) ? a : b; i;);