Visitar URL original
Simplify the special-case printing of single-param arrow functions by Zalathar 路 Pull Request #13204 路 babel/babel 路 GitHub
Skip to content

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

Merged
JLHwung merged 3 commits into
babel:mainfrom
Zalathar:simple-arrow-functions
Apr 26, 2021
Merged

JLHwung merged 3 commits into
babel:mainfrom
Zalathar:simple-arrow-functions

Conversation

@Zalathar

@Zalathar Zalathar commented Apr 25, 2021 •

Copy link
Copy Markdown
Contributor
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

babel-bot commented Apr 25, 2021 •

Copy link
Copy Markdown
Collaborator

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

@codesandbox-ci

codesandbox-ci Bot commented Apr 25, 2021 •

Copy link
Copy Markdown

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


const {
[(_ref) => {
[_ref => {

Copy link
Copy Markdown
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
Copy Markdown
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.

@nicolo-ribaudo nicolo-ribaudo left a comment

Copy link
Copy Markdown
Member

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