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.master
parent
3056c5a5c0
commit
cbcd24a5b2
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
`;
|
||||
|
||||
|
|
|
@ -6,3 +6,4 @@ a ** -b;
|
|||
-(a**b);
|
||||
(a * b) ** c;
|
||||
a ** (b * c);
|
||||
(a % b) ** c;
|
||||
|
|
Loading…
Reference in New Issue