From 03b4ed2fcf52c00af8482ab76041190d92c8dafe Mon Sep 17 00:00:00 2001 From: Davy Duperron Date: Wed, 25 Jan 2017 23:24:25 +0100 Subject: [PATCH] Added parens around in operator in for loops :rocket:. (#468) --- src/fast-path.js | 7 +++++++ tests/for/__snapshots__/jsfmt.spec.js.snap | 3 +++ tests/for/in.js | 1 + 3 files changed, 11 insertions(+) diff --git a/src/fast-path.js b/src/fast-path.js index a8dcc88fd..af44e0c9f 100644 --- a/src/fast-path.js +++ b/src/fast-path.js @@ -276,6 +276,13 @@ FPp.needsParens = function(assumeExpressionContext) { return true; } + if ( + node.operator === 'in' && + parent.type === 'AssignmentExpression' + ) { + return true; + } + case "LogicalExpression": switch (parent.type) { case "CallExpression": diff --git a/tests/for/__snapshots__/jsfmt.spec.js.snap b/tests/for/__snapshots__/jsfmt.spec.js.snap index 7ec23d49d..a6f873b0b 100644 --- a/tests/for/__snapshots__/jsfmt.spec.js.snap +++ b/tests/for/__snapshots__/jsfmt.spec.js.snap @@ -11,9 +11,12 @@ for (var i = 0; i < 10; ++i) { exports[`test in.js 1`] = ` "for ((x in a);;) {} +for (a=(a in b);;) {} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ for ((x in a); ; ) { } +for (a = (a in b); ; ) { +} " `; diff --git a/tests/for/in.js b/tests/for/in.js index 5fb450d16..d88f2c6f1 100644 --- a/tests/for/in.js +++ b/tests/for/in.js @@ -1 +1,2 @@ for ((x in a);;) {} +for (a=(a in b);;) {}