Skip to content
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

Add parentheses for TSTypeQuery in some case #13546

Closed
fisker opened this issue Sep 29, 2022 · 9 comments
Closed

Add parentheses for TSTypeQuery in some case #13546

fisker opened this issue Sep 29, 2022 · 9 comments
Labels
good first issue Good fist issue! lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues) type:bug Issues identifying ugly output, or a defect in the program

Comments

@fisker
Copy link
Sponsor Member

fisker commented Sep 29, 2022

Prettier 2.7.1
Playground link

--parser typescript

Input:

a as (typeof node.children)[number]

a as (typeof node.children)[]

a as ((typeof node.children)[number])[]

Output:

a as typeof node.children[number];

a as typeof node.children[];

a as typeof node.children[number][];

Expected behavior:

Same as input

@fisker fisker added status:needs discussion Issues needing discussion and a decision to be made before action can be taken lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues) labels Sep 29, 2022
@thorn0
Copy link
Member

thorn0 commented Oct 3, 2022

a as ((typeof node.children)[number])[]

This one is too many parens. It looks fine and unambiguous without the outer ones:

a as (typeof node.children)[number][]

Otherwise, I don't think any discussion is needed here. It's a regression in 1.19.

@thorn0 thorn0 added type:bug Issues identifying ugly output, or a defect in the program good first issue Good fist issue! and removed status:needs discussion Issues needing discussion and a decision to be made before action can be taken labels Oct 7, 2022
@fabioStori
Copy link

fabioStori commented Nov 18, 2022

@thorn0 the goal is to make the input
a as ((typeof node.children)[number])[]
result in
a as (typeof node.children)[number][]
right?

@fisker
Copy link
Sponsor Member Author

fisker commented Nov 18, 2022

Yes, a as (typeof node.children)[number][] is expected.

@fabioStori
Copy link

fabioStori commented Nov 18, 2022

@fisker Thank you for the confirmation.
Just to better understand the motivation, can you explain the reasoning behind the logic of maintaining the parentheses around the typeof node.children in the scenario a as ((typeof node.children)[number])[] , but removing it in the a as (typeof node.children)[number] scenario?
I understand that, in this first example, the outer parentheses can make the code look ambiguous, but in this second one the reason is still not clear to me.

I have another question, based on a scenario I encountered while working on this.
If the input is something like a as typeof node.children[number][];, should the parentheses be added, resulting in a as (typeof node.children)[number][];?

@fisker
Copy link
Sponsor Member Author

fisker commented Nov 24, 2022

can you explain the reasoning

#13546 (comment)

If the input is something like a as typeof node.children[number][];

Yes.

@fisker
Copy link
Sponsor Member Author

fisker commented Jan 5, 2023

Fixed by #14042

@zomars
Copy link

zomars commented Jan 27, 2023

Do we have an option to disable this?

@mikedidomizio
Copy link

mikedidomizio commented Jan 30, 2023

If the purpose of this was for readability, should we have it as optional?

@demc
Copy link

demc commented Feb 13, 2023

Please either make this rule optional or revert. Thank you

@prettier prettier locked as resolved and limited conversation to collaborators Feb 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Good fist issue! lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues) type:bug Issues identifying ugly output, or a defect in the program
Projects
None yet
Development

No branches or pull requests

6 participants