Merge pull request #2606 from amasad/for-in-edge-case
Special case paren printing in for-loop init node
This commit is contained in:
commit
86b4f0dbe8
@ -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(")");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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) {
|
||||
|
||||
1
packages/babel/test/fixtures/generation/edgecase/for-loop-in/actual.js
vendored
Normal file
1
packages/babel/test/fixtures/generation/edgecase/for-loop-in/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
for ((a in b) ? a : b; i;);
|
||||
1
packages/babel/test/fixtures/generation/edgecase/for-loop-in/expected.js
vendored
Normal file
1
packages/babel/test/fixtures/generation/edgecase/for-loop-in/expected.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
for ((a in b) ? a : b; i;);
|
||||
Loading…
x
Reference in New Issue
Block a user