-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Superfluous type operation parentheses are not stripped #13500
Comments
It is by design because usually Prettier tries to preserve the shape of the AST. There are some exceptions from this rule. E.g., |
That makes sense, I was suspecting I'd be in a niche case. Some context for my case: I am building a tool that helps with code transformations and it parenthesizes operations when you insert them, to make sure new operations get the highest precedence, while superfluous parens would be stripped (which is what I assumed, based on the JS expression behavior, as shown by you). Totally understand if that's outside of scope for Prettier though! |
Your tool can flatten unions and intersection itself, but let's keep the issue open for discussion. There might be other arguments for or against doing this in Prettier. |
I have the same issue right now (typescript-eslint/typescript-eslint#6899) - generating some code using strings and wanting prettier to automatically unwrap unnecessarily parenthesised unions. From a human-written code POV - I can see this from an code formatting & standardisation POV - it's easy to leave unnecessary parens behind after refactoring like type T = (A | B) & C;
// refactor | to &
type T = (A & B) & C;
// oops parens don't get removed automatically In these super simple examples it's really easy to see the unnecessary parentheses and remove them by hand - but if you've got a union/intersection of object literals or generic types - things can get pretty messy and it can be hard to manually untangle it all. Prettier does currently also enforce parenthesisation of "ambiguous" type expressions (that are equivalent due to operator precedence): type T = {a: string} | {b: string} & {c: string} | {d: string}
// prettier formats as
type T = {a: string} | ({b: string} & {c: string}) | {d: string}; Though that doesn't change the AST at all. |
Here is an example of why it would be great for prettier to remove the unnecessary parentheses: // with unnecessary parentheses :(
type T = ({
a: string } | { b: string }) | { c: string }
// without unnecessary parentheses :)
type T = {
a: string } | { b: string } | { c: string } formatted: // with unnecessary parentheses :(
type T =
| (
| {
a: string;
}
| { b: string }
)
| { c: string };
// without unnecessary parentheses :)
type T =
| {
a: string;
}
| { b: string }
| { c: string }; |
I expected type operation parens stripping would work like the one for regular JS operations, i.e. superfluous parens would be removed. That seems to not be the case. Now I am wondering: is that by design?
Prettier 2.7.1
Playground link
Input:
Output:
Expected behavior:
The text was updated successfully, but these errors were encountered: