Improve removal of parentheses around expressions
Currently stylua removes many redundant parens, and in one specific case it's triggering a luau analysis warning.
Ex:
local _ = (not true) == true
Changes to:
local _ = not true == true
This triggers the linter:
ComparisonPrecedence: not X == Y is equivalent to (not X) == Y; consider using X ~= Y, or add parentheses to silence
I noticed that a number of cases where redundant parens aren't removed, maybe this use case should be added? Should Stylua add parens to clarify the operator precedence?
Here are some cases I noticed where parens aren't removed:
local _ = true and (false or true)
local _ = (true and false or true)
local _ = (true and false) or true
local _ = (not true == false)
Another example from Prettier, it adds parens to show operator precedence between and/or:
(true && false) || true
Interested to hear your thoughts!
The first example is definitely a bug ~~as it is changing the meaning of the code~~ Actually no it doesn't, but it should still keep the parens (or even add it in like you mention).
it adds parens to show operator precedence between and/or:
Yup, this is something I've always wanted to do. IIRC, The main reason I didn't do it yet is because of the incredibly common and-or idiom which would make this annoying (it's superseded by if expressions but that is just Luau only)
Looks like parentheses removal could be improved in some of the cases you've shown - I'd probably say it's a separate enhancement compared to the initial bug, but I'll take a look at this, thanks!
The bug where the parentheses highlighting precedence were removed has been fixed
I do want to improve the parentheses removal, and start adding in new parentheses too.
The reason right now that parentheses are not being removed in the samples you showed is because the internal expressions are binary expressions. For simplicity, I don't touch them right now, but I do want to improve this. Will convert this issue into a feature request about this
Thanks so much!