You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Removes parentheses in expressions with mixed operators #187
Reacted by Heliton Nordt, alanimgur, Anton Kudris, Joni Korpi, Sylvan Mably, Titus Woo, Sid Jain, Jeremy Gollehon, ppvg, Jeoffrey Mendez and 239 more
Reacted by Bohdan, Justin Ridgewell, Bård Kalbakk, Daniela Bravo, emergent_reasons and helle253
Reacted by Emil Laine, Qichao Zhong, Martin Zaloudek, Patrick Stillhart, carcinocron, Eduardo Daniel Cuomo, Chief Sloperator, Jeremy Simkins, helle253 and Игорь Майоров / Poliklot
Reacted by Brian Kim, @brody2consult - Chris J. Brody, Johannes Millan, Dmitriy Petrov, Isaac Muniz, Patrick Stillhart, carcinocron, Eduardo Daniel Cuomo, Chief Sloperator and helle253
@demoneaux Oh, you are right. I updated the description with a correct example. It seems that prettier only removes parentheses when it doesn't lead to a change in logic. Nonetheless, I think it's important to keep the parentheses for clarity if that's possible.
Reacted by alanimgur, Anton Kudris, Keith J. Grant, Jeoffrey Mendez, Maciek Sakrejda, Robert, Adam Donahue, Chris, Andrei Xavier de Oliveira Calazans, zhilingege and 59 more
Reacted by Cyril Auburtin, Brian Kidd and phiresky
I agree. It should be opinionated about enforcing clear logic between operators. For example, it's just not clear, unless you remember your math classes from HS, that division happens before addition:
const result = 100 / 10 + 10; // 20 const result = 100 / (10 + 10); // 5 const result = (100 / 10) + 10; // 20
It currently enforces unclear operations, when the strong opinion should be to enforce clarity.
Reacted by Sylvan Mably, Rafal Stozek, George Kormaris, Jeremy Gollehon, Jeoffrey Mendez, c-vetter, Ana Kucherova, Micah Stubbs, Soney Mathew, Maciek Sakrejda and 109 more
Reacted by larpo, FallenAngel97, Keith Layne, Bohdan, Adrien Crivelli, Brian Kidd, Robert Monfera, Lautaro Cozzani and Giulio Malventi
The create-react-app ESLint config includes the no-mixed-operators warning. So even when the user writes mixed operator code with proper parentheses, Prettier currently converts it to code that causes warnings in create-react-app projects. Since Prettier and create-react-app seem to share some principles and generally work well in combination, it would be a nice design principle if Prettier's formatting rules match create-react-app's ESLint rules.
Reacted by Boris Serdiuk, Pedr Browne, Jeremy Gollehon, Maciek Sakrejda, Paul Kim, Rob Indesteege, Klas Mellbourn, Abdellah, Pierre Marchal, Sylvain Pont and 14 more
I think this is one of those cases where prettier should defer to the user's choice. Preserve brackets when they already exist, but don't add new ones.
Reacted by Peter J Weisberg, Torleif Halseth, Steve Barman, Jonatan Ramhöj, Thomas Carlsen, James Chetwood, marelyns, Patrick, Brian Kidd, Lucas Basquerotto and 25 more
@erbridge All of this information is lost from the AST. The AST doesn't have any information about extraneous parens, so it's impossible for us to keep the original structure. And in fact, that's the opposite of what we want to do; this project enforces a consistent style by disregarding the original style.
We need to choose whether or not we want to do this, and have it consistent everywhere. I'm a little worried that it would add a lot of noise when it's only helpful in a few situations, but we can talk about it more.
Reacted by Andrew Boyton, Artur Kornakov and Nathan Broadbent
@jlongster Understood. My preference is to get rid of all extraneous brackets. As you say, in general, they would be unnecessarily verbose. If extra clarity is needed, users can pull the sub-statements out into their own variables.
Reacted by Giulio Malventi and Chief Sloperator
Reacted by Robert Monfera, Noah, Lucas Basquerotto, Nico Verkest, Joaquin Valle Pinto, Skaÿ, Asher Cerka, aarowman, James Donnelly, Matt Sellars and 9 more
I would argue that the first example reads better because is clearly shows the addition applied to currentIndex. By stripping away the brackets, meaning is lost. Sure you could solve this by adding another variable, but I don't think this would make the code more readable.
Reacted by mbaer3000, Thomas Carlsen, James Chetwood, David Johnston, Sam Zalin-Miller, Jacob Wang, Robert Monfera, Guy Elsmore-Paddock, jakub, Lucas Basquerotto and 19 more
Reacted by Piotr Monwid-Olechnowicz, Ferdinand Antonius, Demian Ferreiro and Mason Dechaineux
@Undistraction prettier will keep the parens in that case. Those expressions are semantically different. 5 - (2 + 1) === 2 but 5 - 2 + 1 === 4. Prettier maintains the correct semantics and add parens where needed, naturally.
Reacted by @brody2consult - Chris J. Brody, Nikunj78 and cjOne7
We are now adding parenthesis around && inside of || which seems like the biggest issue people are facing in this issue. I'm closing this issue. If there are other places where you'd like parenthesis to be kept, please feel free to open another issue.
+1, I think so. Everyone learned the precedence of these operators in elementary school, but the parentheses were only for readability, not the result of the code. As Prettier does, if we don't format the code, the code logic is just as correct, it just can be ugly and difficult to read.
Those parentheses were not only for readability. In a programming context where the language follows common convention, things happen from left to right when there is ambiguity. But that is only a convention. Strictly speaking a / b / c has an undefined meaning because interpreting it left to right is a convention, not a fundamental part of the math.
That's not to mention the even more important point of everyone above about readability, intent, and resistance to future errors/uncertainty.
Overall, same as the comment above, having the other parts of prettier is great when it comes to just having something that works and everyone can like/hate it equally. But this is not one of them. It should do nothing by default and an option would allow it to reformat if you want it to. Or at the very least add an option to disable this behavior.
Reacted by devperate, Dmitriy Petrov, Lucas Basquerotto, section14, Kirill Agalakov, jinpom, Ben, Chief Sloperator, Shannon Fenn, Szymon Tondowski and 1 more
Maybe now is the time to make a fork like prettier-ce (Community Edition)?
With the addition of options that are really important for developers, like the one that has been discussed here for 7 years
This issue was big enough for our team that our convention has become a common agreement to // prettier-ignore in order to allow human-friendly parentheses. Outside of that, prettier has jurisdiction. Works well enough.
I understand the issue that special handling for parentheses is to some degree incompatible with prettier's "break it down to the tokens and rebuild" approach (maybe not a great summary, but something like that). Not sure how feasible it is to make an exception and under what heuristics. Having said that, if a credible maintainer really wants to fork, has a credible plan to handle this issue, and commits to maintaining it, I'd consider it!
I guess I should unsubscribe from this thread, it's been ongoing for years
and obvious that no one cares to fix it.
I've had to stop using prettier. I need a formatter that just formats
correctly, but doesn't change the code. Removing parenthesis whether the
tool thinks they are necessary or not, is not the job of a formatter. This
tool is broken.
On Sun, Jan 26, 2025, 4:23 AM emergent_reasons ***@***.***> wrote:
Maybe now is the time to make a fork like prettier-ce (Community Edition)?
With the addition of options that are really important for developers,
like the one that has been discussed here for 7 years
This issue was big enough for our team that our convention has become a
common agreement to // prettier-ignore in order to allow human-friendly
parentheses. Outside of that, prettier has jurisdiction. Works well enough.
I understand the issue that special handling for parentheses is to some
degree incompatible with prettier's "break it down to the tokens and
rebuild" approach (maybe not a great summary, but something like that). Not
sure how feasible it is to make an exception and under what heuristics.
Having said that, if a credible maintainer really wants to fork, has a
credible plan to handle this issue, and commits to maintaining it, I'd
consider it!
—
Reply to this email directly, view it on GitHub
<#187 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABSC72UNZEJMFJQEMDYUXAD2MSSRPAVCNFSM4C4OK232U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TENRRGQZDSMRQG4ZQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
Reacted by Jeremy Simkins, Shannon Fenn and Andreas Böhm
Folks (the last two, primarily), is it so hard to maintain a constructive discussion? I don't need a notification for your destructive antics. (And none of you want an email reminding you to have a constructive discussion either.)
Why is x * y / z rewritten as (x * y) / z, but parentheses in (a & b) && c are removed? I can understand if unambiguity was chosen over readability in the first case, but bitwise operator precedence is a nasty topic that warrants wrapping them in parentheses whenever possible imo.
I am strict about always using brackets explicitly in mathematical operations and I don't see why prettier should override that. It's just safer to include the parens to avoid mistakes.
Reacted by Vincent Esche, emergent_reasons and Matthew Pearson
I've found from discussion on this topic where I thought it would be obvious, that there seems to be a large contingent of developers who think something along the lines of "Strictly unnecessary parentheses are a sign of a developer being a noob and not having the operator priority internalized. Leaving them in makes it harder to read and ugly."
Which can be true in extreme cases, but in general I can't disagree with more.
But it gives some context to why the developers of prettier may prefer to ignore this. It surely seems possible to make this highly contentious topic one of the few options available in prettier configuration 🤷 . We can hope... for 10 more years.
Are we trying to be smart or safe. If you think leaving parens in for safety is a noob mistake driven by lack of knowledge you are deliberately deciding that noobs should be left to suffer the consequences of their mistakes which is unhelpful. Like it or not people including the developers of prettier create bugs, and leaving out parens and messing up precedence is one source of bugs.
Reacted by Matthew Pearson, sosoba, Lucas Basquerotto, Pham Minh Hoang Henry and R1D3R175
Reacted by emergent_reasons, sosoba, Lucas Basquerotto and Pham Minh Hoang Henry
Activity
bnjmnt4n commentedon Jan 14, 2017
Can't seem to reproduce this: https://jlongster.github.io/prettier/#%7B%22content%22%3A%22const%20foo%20%3D%20a%20%26%26%20(b%20%7C%7C%20c%20%7C%7C%20d)%3B%5Cn%22%2C%22options%22%3A%7B%22printWidth%22%3A80%2C%22tabWidth%22%3A2%2C%22singleQuote%22%3Afalse%2C%22trailingComma%22%3Afalse%2C%22bracketSpacing%22%3Atrue%7D%7D seems to generate the expected code. Maybe you can try updating your version of Prettier.
yenbekbay commentedon Jan 14, 2017
@demoneaux Oh, you are right. I updated the description with a correct example. It seems that prettier only removes parentheses when it doesn't lead to a change in logic. Nonetheless, I think it's important to keep the parentheses for clarity if that's possible.
alanimgur commentedon Jan 15, 2017
I agree. It should be opinionated about enforcing clear logic between operators. For example, it's just not clear, unless you remember your math classes from HS, that division happens before addition:
const result = 100 / 10 + 10; // 20const result = 100 / (10 + 10); // 5const result = (100 / 10) + 10; // 20It currently enforces unclear operations, when the strong opinion should be to enforce clarity.
smably commentedon Feb 9, 2017
A real-world example from some code I'm working on:
https://jlongster.github.io/prettier/#%7B%22content%22%3A%22style.left%20%3D%20left%20-%20(this.state.width%20%2F%202)%20%2B%20(parent.offsetWidth%20%2F%202)%3B%5Cn%22%2C%22options%22%3A%7B%22printWidth%22%3A80%2C%22tabWidth%22%3A2%2C%22singleQuote%22%3Afalse%2C%22trailingComma%22%3Afalse%2C%22bracketSpacing%22%3Atrue%2C%22doc%22%3Afalse%7D%7D
Both are correct, but I would say that the original is vastly more readable (and one might even say prettier).
lydell commentedon Feb 10, 2017
Related: https://github.com/lydell/eslint-config-prettier#no-mixed-operators
sander commentedon Feb 10, 2017
The
create-react-appESLint config includes theno-mixed-operatorswarning. So even when the user writes mixed operator code with proper parentheses, Prettier currently converts it to code that causes warnings in create-react-app projects. Since Prettier and create-react-app seem to share some principles and generally work well in combination, it would be a nice design principle if Prettier's formatting rules match create-react-app's ESLint rules.erbridge commentedon Feb 11, 2017
I think this is one of those cases where prettier should defer to the user's choice. Preserve brackets when they already exist, but don't add new ones.
jlongster commentedon Feb 20, 2017
@erbridge All of this information is lost from the AST. The AST doesn't have any information about extraneous parens, so it's impossible for us to keep the original structure. And in fact, that's the opposite of what we want to do; this project enforces a consistent style by disregarding the original style.
We need to choose whether or not we want to do this, and have it consistent everywhere. I'm a little worried that it would add a lot of noise when it's only helpful in a few situations, but we can talk about it more.
erbridge commentedon Feb 20, 2017
@jlongster Understood. My preference is to get rid of all extraneous brackets. As you say, in general, they would be unnecessarily verbose. If extra clarity is needed, users can pull the sub-statements out into their own variables.
Undistraction commentedon Feb 27, 2017
I think that there are many cases when judiciously placed brackets add meaning. For example:
vs
I would argue that the first example reads better because is clearly shows the addition applied to
currentIndex. By stripping away the brackets, meaning is lost. Sure you could solve this by adding another variable, but I don't think this would make the code more readable.jlongster commentedon Feb 27, 2017
@Undistraction prettier will keep the parens in that case. Those expressions are semantically different.
5 - (2 + 1) === 2but5 - 2 + 1 === 4. Prettier maintains the correct semantics and add parens where needed, naturally.Undistraction commentedon Feb 27, 2017
@jlongster Of course. Sorry for the noise.
vjeux commentedon Mar 1, 2017
We are now adding parenthesis around && inside of || which seems like the biggest issue people are facing in this issue. I'm closing this issue. If there are other places where you'd like parenthesis to be kept, please feel free to open another issue.
223 remaining items
emergent-reasons commentedon Aug 25, 2024
Those parentheses were not only for readability. In a programming context where the language follows common convention, things happen from left to right when there is ambiguity. But that is only a convention. Strictly speaking
a / b / chas an undefined meaning because interpreting it left to right is a convention, not a fundamental part of the math.That's not to mention the even more important point of everyone above about readability, intent, and resistance to future errors/uncertainty.
Overall, same as the comment above, having the other parts of prettier is great when it comes to just having something that works and everyone can like/hate it equally. But this is not one of them. It should do nothing by default and an option would allow it to reformat if you want it to. Or at the very least add an option to disable this behavior.
emreakdas commentedon Oct 1, 2024
Any news?
krutoo commentedon Oct 11, 2024
Maybe now is the time to make a fork like
prettier-ce(Community Edition)?With the addition of options that are really important for developers, like the one that has been discussed here for 7 years
[prettierx] Update graphql -> 14.6.0 (prettier#187)
F1LT3R commentedon Jan 26, 2025
Before prettier:
After prettier:
Why do we need to add 4 parenthesis for clarity, on a single turnary?
This is overkill.
Let's rename this tool, Uglier.
If I'm teaching someone JavaScript, I don't want them asking me, "Why am I adding things that do nothing?"
My answer can't be "clarity". Because it's a single turnary. It's very clear what is happening already.
emergent-reasons commentedon Jan 26, 2025
This issue was big enough for our team that our convention has become a common agreement to
// prettier-ignorein order to allow human-friendly parentheses. Outside of that, prettier has jurisdiction. Works well enough.I understand the issue that special handling for parentheses is to some degree incompatible with prettier's "break it down to the tokens and rebuild" approach (maybe not a great summary, but something like that). Not sure how feasible it is to make an exception and under what heuristics. Having said that, if a credible maintainer really wants to fork, has a credible plan to handle this issue, and commits to maintaining it, I'd consider it!
artiface commentedon Jan 26, 2025
maxrahder commentedon Jan 26, 2025
@artiface , I stay subscribed for the entertainment value. I stopped using Prettier years ago for this specific reason.
lgrahl commentedon Jan 27, 2025
Folks (the last two, primarily), is it so hard to maintain a constructive discussion? I don't need a notification for your destructive antics. (And none of you want an email reminding you to have a constructive discussion either.)
d07RiV commentedon May 4, 2025
Why is
x * y / zrewritten as(x * y) / z, but parentheses in(a & b) && care removed? I can understand if unambiguity was chosen over readability in the first case, but bitwise operator precedence is a nasty topic that warrants wrapping them in parentheses whenever possible imo.docwarren commentedon Jan 1, 2026
I am strict about always using brackets explicitly in mathematical operations and I don't see why prettier should override that. It's just safer to include the parens to avoid mistakes.
emergent-reasons commentedon Jan 2, 2026
I've found from discussion on this topic where I thought it would be obvious, that there seems to be a large contingent of developers who think something along the lines of "Strictly unnecessary parentheses are a sign of a developer being a noob and not having the operator priority internalized. Leaving them in makes it harder to read and ugly."
Which can be true in extreme cases, but in general I can't disagree with more.
But it gives some context to why the developers of prettier may prefer to ignore this. It surely seems possible to make this highly contentious topic one of the few options available in prettier configuration 🤷 . We can hope... for 10 more years.
rubengmurray commentedon Jan 2, 2026
Still not using prettier because of this. Time to unsubscribe from this thread 😃
sosoba commentedon Jan 2, 2026
Hi. I'm encountering the same problem. So I've developed a plugin that improves Prettier's behavior. Feel free to test it.
docwarren commentedon Jan 2, 2026
Are we trying to be smart or safe. If you think leaving parens in for safety is a noob mistake driven by lack of knowledge you are deliberately deciding that noobs should be left to suffer the consequences of their mistakes which is unhelpful. Like it or not people including the developers of prettier create bugs, and leaving out parens and messing up precedence is one source of bugs.