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

Feature request: new value for the preserveParens option #183

Open
thorn0 opened this issue Mar 3, 2021 · 6 comments
Open

Feature request: new value for the preserveParens option #183

thorn0 opened this issue Mar 3, 2021 · 6 comments
Labels
enhancement New feature or request

Comments

@thorn0
Copy link

thorn0 commented Mar 3, 2021

Would be good to have a mode in which ParenthesizedExpression nodes would be created only for parentheses used for JSDoc type assertions.

The syntax of JSDoc type assertions is simple: @type {...} JSDoc comment followed by a parenthesized expression. Anything else is not a type assertion. Reference: Closure Compiler, TypeScript.

@3cp
Copy link
Member

3cp commented Mar 5, 2021

This is probably difficult for meriyah, as the content of comment needs to go through some kind of syntax parsing too.

@thorn0
Copy link
Author

thorn0 commented Mar 5, 2021

The syntax is super simple. See the links. No need to parse the type itself. Just the fact that the comment starts with * and contains @type followed by at least something.

@3cp
Copy link
Member

3cp commented Mar 5, 2021

I see.

@3cp 3cp added the enhancement New feature or request label Mar 5, 2021
@3cp
Copy link
Member

3cp commented Mar 5, 2021

@KFlash can you have a look? I am not sure how to temporarily turn on Context.OptionsPreserveParens if next token is (. Also has to properly reset it after the ParenthesizedExpression. I sensed it might be complicated if need to support recursion .

I am not sure if recursive type casting is possible. @thorn0 ?

/** @type xxx */ ({a: /** @type xxx */ (b) })

Right now, I guess you are turning on preserveParens option globally.

@KFlash
Copy link
Contributor

KFlash commented Mar 5, 2021

Can't you just create a new option? Name it thorn. Then you can do the same as you do for the other options

if (options.thorn) context | Context.OptionsThorn | OptionsPreserveParens

@thorn What's the use case for this? And why do you want to turn off ParenthesizedExpression ? It's in the spec and if used in Prettier you can save tons of works for cases like (((((x))))) where your expected output should be '(x)' or no paren at all. Just check if 'type' exist, and avoid printing paren if nested.

@thorn0
Copy link
Author

thorn0 commented Mar 6, 2021

I am not sure if recursive type casting is possible

Yes, possible. Also they can be nested directly (people really use this):

/** @type MyType */ (/** @type any */ (x))

What's the use case for this? And why do you want to turn off ParenthesizedExpression ? It's in the spec

If you mean ESTree, ParenthesizedExpression is not there. It's a non-standard extension. Overall, ESTree's approach is to ignore insignificant parentheses. However, the JSDoc convention that uses such parentheses is widespread and important. So from the point of view of tools like formatters and linters, it makes sense to create these non-standard nodes only for JSDoc. In other cases, they only get in the way. E.g. Prettier doesn't simply print the AST. It also does a lot of checks of the AST structure along the way to choose between different ways to print things. Taking ParenthesizedExpression nodes into account in all those checks would ridiculously complicate them, so actually what happens now in Prettier is an extra step that removes all ParenthesizedExpressions that aren't JSDoc type assertions from the AST before passing it to printing.

Name it thorn

I sincerely believe something like PreserveJsdocParens would be a slightly better name. 😆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants