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

Fix excess parens on awaits #1411

Closed
wants to merge 1 commit into from
Closed

Conversation

KernelDeimos
Copy link

recast doesn't do what it says on the tin: preserve the formatting of the code. Any code using async and await syntax is very unlikely to be rendered the same coming out of recast as it went in.

On this PR, @techLeadGuy suggested removing the AwaitExpression case in needsParens(). This worked for me. Maybe it works in all cases, but I'm not 100% sure. I think it's worth making this change to bring recast closer to its goal, and people like me should be expected to try and improve recast if our code breaks.

@KernelDeimos
Copy link
Author

I'm starting to realize this sucks just a little bit more than I previously thought. The babel parser indeed does not care to express the information about whether or not parens were included originally. The parser is lossy, except for when the tokens option is set, 'cause then it gives a nice little

17058       "type": {$
17059         "label": "(",$
17060         "beforeExpr": true,$
17061         "startsExpr": true,$
17062         "rightAssociative": false,$
17063         "isLoop": false,$
17064         "isAssign": false,$
17065         "prefix": false,$
17066         "postfix": false,$
17067         "binop": null,$
17068         "updateContext": null$
17069       },$

Separately though - it looks like you get lexer output pasted at the end of the AST. I don't understand why we can't just have a CST-producing parser that supports modern javascript but 🤷

@KernelDeimos
Copy link
Author

Closing this because it won't work

@eventualbuddha
Copy link
Collaborator

I don't understand why we can't just have a CST-producing parser that supports modern javascript.

While everyone surely has their own reasons, I am less interested in this than I once was because of Prettier. As long as Recast can parse the code and preserve formatting ~95% while producing valid JS syntax, then for my use cases (codemods) it’s fine. This isn’t good enough for all use cases such as IDE integration with running something on save, though.

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

Successfully merging this pull request may close these issues.

2 participants