From cbcd24a5b2e859eb319047883ea3d11329c240b8 Mon Sep 17 00:00:00 2001 From: Kevin Gibbons Date: Fri, 12 Oct 2018 09:33:47 -0700 Subject: [PATCH] Correct parentheses for mixed exp/mod (#5243) Fixes #5238. cc @duailibe for the [original change](https://github.com/prettier/prettier/pull/4413). I basically undid that PR because it didn't look to me like `shouldFlatten` made sense as the place to introduce the behavior it was going for, but I might have misunderstood something. --- src/common/util.js | 9 --------- src/language-js/needs-parens.js | 4 ++-- .../binary-expressions/__snapshots__/jsfmt.spec.js.snap | 2 ++ tests/binary-expressions/exp.js | 1 + 4 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/common/util.js b/src/common/util.js index 8c3d4c6f3..bf0e1fc56 100644 --- a/src/common/util.js +++ b/src/common/util.js @@ -267,10 +267,6 @@ const equalityOperators = { "===": true, "!==": true }; -const additiveOperators = { - "+": true, - "-": true -}; const multiplicativeOperators = { "*": true, "/": true, @@ -284,11 +280,6 @@ const bitshiftOperators = { function shouldFlatten(parentOp, nodeOp) { if (getPrecedence(nodeOp) !== getPrecedence(parentOp)) { - // x + y % z --> (x + y) % z - if (nodeOp === "%" && !additiveOperators[parentOp]) { - return true; - } - return false; } diff --git a/src/language-js/needs-parens.js b/src/language-js/needs-parens.js index d6432aeaf..b0e2c03d9 100644 --- a/src/language-js/needs-parens.js +++ b/src/language-js/needs-parens.js @@ -291,10 +291,10 @@ function needsParens(path, options) { } if (pp < np && no === "%") { - return !util.shouldFlatten(po, no); + return po === "+" || po === "-"; } - // Add parenthesis when working with binary operators + // Add parenthesis when working with bitwise operators // It's not stricly needed but helps with code understanding if (util.isBitwiseOperator(po)) { return true; diff --git a/tests/binary-expressions/__snapshots__/jsfmt.spec.js.snap b/tests/binary-expressions/__snapshots__/jsfmt.spec.js.snap index cbb26b7c0..7fd9c65c2 100644 --- a/tests/binary-expressions/__snapshots__/jsfmt.spec.js.snap +++ b/tests/binary-expressions/__snapshots__/jsfmt.spec.js.snap @@ -101,6 +101,7 @@ a ** -b; -(a**b); (a * b) ** c; a ** (b * c); +(a % b) ** c; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ a ** (b ** c); (a ** b) ** c; @@ -110,6 +111,7 @@ a ** -b; -(a ** b); (a * b) ** c; a ** (b * c); +(a % b) ** c; `; diff --git a/tests/binary-expressions/exp.js b/tests/binary-expressions/exp.js index 903466894..e0bac4a5f 100644 --- a/tests/binary-expressions/exp.js +++ b/tests/binary-expressions/exp.js @@ -6,3 +6,4 @@ a ** -b; -(a**b); (a * b) ** c; a ** (b * c); +(a % b) ** c;