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
Always print parentheses around arrow function parameters #13202
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/45555/ |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit ddb4f9e:
|
I have an alternate version of this change that tries to retain no-parens in some cases, and instead just tries to make the logic simpler and more robust. But I like the idea of just always spending two characters to guarantee correct output. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the output, maybe we could still keep the single identifier param, no async, no comments, no types case (which is one of the most common ones) parens-less, or at least do it when the compact: true
option is set.
It should still be safe, since it doesn't need to check loc and accidental line breaks don't cause problems.
(x): %checks => x !== null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh this was a bug
OK, I'll polish up the version that tries to keep the no-parens in simple cases, and see what that looks like. |
I opened a separate PR for the alternate version at #13204, to avoid having to force-push back and forth between the two versions. |
Closed in favour of #13204. |
Some arrow functions can safely be printed without parentheses around a single identifier parameter.
However, there are many factors that can make this unsafe (e.g. type annotations, comments), and several generator features that can silently introduce edge cases (
retainLines
,auxiliaryCommentBefore
/auxiliaryCommentAfter
). These make it hard to be confident that printing the bare identifier really is safe.This PR therefore makes the generator always print the parentheses, even when they aren't strictly necessary, greatly simplifying the implementation.