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

Simplify the special-case printing of single-param arrow functions #13204

Merged
merged 3 commits into from Apr 26, 2021

Conversation

Zalathar
Copy link
Contributor

@Zalathar Zalathar commented Apr 25, 2021

Q                       A
Fixed Issues? Fixes #13198
Patch: Bug Fix? Yes
Major: Breaking Change? No
Minor: New Feature? No
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes? No
License MIT

This is an alternate version of #13202 that retains the special-case handling for single-parameter arrow functions, and instead tries to make it simpler and more robust.

We still remove the complex special-case code for printing single-parameter arrow functions with parentheses, and just delegate to the regular parameter-printing code in that case.

@babel-bot
Copy link
Collaborator

babel-bot commented Apr 25, 2021

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/45569/

@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 25, 2021

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 c2716a7:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@@ -1,7 +1,7 @@
var _ref2;

const {
[(_ref) => {
[_ref => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked into why some of these arrow functions are losing their parens.

It turns out that the previous implementation of hasTypes was checking param.trailingComments, which is truthy when trailingComments is an empty array.

Switching to param.trailingComments?.length makes the generator conclude that this arrow function doesn't need parentheses after all.

Comment on lines +147 to +148
// @ts-expect-error
node.predicate ||
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't type-check because predicate isn't declared on ArrowFunctionExpression.

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@nicolo-ribaudo nicolo-ribaudo added pkg: generator PR: Bug Fix 🐛 A type of pull request used for our changelog categories labels Apr 25, 2021
@JLHwung JLHwung merged commit 3d4b801 into babel:main Apr 26, 2021
@Zalathar Zalathar deleted the simple-arrow-functions branch April 27, 2021 09:08
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jul 28, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: generator PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Edge-case exception while printing single-parameter arrow function
4 participants