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.
This commit is contained in:
Kevin Gibbons authored and Jed Fox committed 2018-10-12 12:33:47 -04:00
1 parent 3056c5a5c0
commit cbcd24a5b2
4 files changed
+5 -11

No files matched your search

-9
View File
@@ -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;
}
+2 -2
View File
@@ -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;
@@ -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;
`;
+1
View File
@@ -6,3 +6,4 @@ a ** -b;
-(a**b);
(a * b) ** c;
a ** (b * c);
(a % b) ** c;