Skip to content

Removes parentheses in expressions with mixed operators #187

Open

Description

@yenbekbay

Before:

const foo = (a && b) || c || d;

After:

const foo = a && b || c || d;

The same goes for arithmetic and bitwise operators.

Relevant ESLint rule: http://eslint.org/docs/rules/no-mixed-operators.

Activity

  1. added
    status:needs discussion
    on Jan 14, 2017
  2. yenbekbay commented on Jan 14, 2017

    @yenbekbay
    Author

    @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.

  3. alanimgur commented on Jan 15, 2017

    @alanimgur

    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.

  4. smably commented on Feb 9, 2017

    @smably
    Contributor
  5. lydell commented on Feb 10, 2017

    @lydell
    Member
  6. sander commented on Feb 10, 2017

    @sander

    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.

  7. erbridge commented on Feb 11, 2017

    @erbridge

    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.

  8. jlongster commented on Feb 20, 2017

    @jlongster
    Member

    @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.

  9. erbridge commented on Feb 20, 2017

    @erbridge

    @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.

  10. Undistraction commented on Feb 27, 2017

    @Undistraction

    I think that there are many cases when judiciously placed brackets add meaning. For example:

    const remainingImageCount = totalImageCount - (currentIndex + 1);
    

    vs

    const remainingImageCount = totalImageCount - currentIndex + 1;
    

    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.

  11. jlongster commented on Feb 27, 2017

    @jlongster
    Member

    @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.

  12. Undistraction commented on Feb 27, 2017

    @Undistraction

    @jlongster Of course. Sorry for the noise.

  13. vjeux commented on Mar 1, 2017

    @vjeux
    Contributor

    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.

  14. 223 remaining items

  15. emergent-reasons commented on Aug 25, 2024

    @emergent-reasons

    +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.

  16. emreakdas commented on Oct 1, 2024

    @emreakdas

    Any news?

  17. krutoo commented on Oct 11, 2024

    @krutoo

    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

  18. F1LT3R commented on Jan 26, 2025

    @F1LT3R

    Before prettier:

    const counter = ((count = 0) => ({
      add: n => n ? count += n : count
    }))()

    After prettier:

    const counter = ((count = 0) => ({
      add: n => (n ? (count += n) : count)
    }))()

    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.

  19. emergent-reasons commented on Jan 26, 2025

    @emergent-reasons

    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!

  20. artiface commented on Jan 26, 2025

    @artiface
  21. maxrahder commented on Jan 26, 2025

    @maxrahder

    @artiface , I stay subscribed for the entertainment value. I stopped using Prettier years ago for this specific reason.

  22. lgrahl commented on Jan 27, 2025

    @lgrahl

    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.)

  23. d07RiV commented on May 4, 2025

    @d07RiV

    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.

  24. docwarren commented on Jan 1, 2026

    @docwarren

    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.

  25. emergent-reasons commented on Jan 2, 2026

    @emergent-reasons

    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.

  26. rubengmurray commented on Jan 2, 2026

    @rubengmurray

    84 years

    Still not using prettier because of this. Time to unsubscribe from this thread 😃

  27. sosoba commented on Jan 2, 2026

    @sosoba

    Hi. I'm encountering the same problem. So I've developed a plugin that improves Prettier's behavior. Feel free to test it.

  28. docwarren commented on Jan 2, 2026

    @docwarren

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:binary expressionsstatus:needs discussion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions