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
Hug object types in function parameters in Flow like in TS #13396
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
(node, name) => | ||
node.type === "FunctionTypeParam" && name === "typeAnnotation", | ||
shouldHugParameter | ||
))) || |
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.
Would be cool if path.match
supported alternatives like this:
path.match(
isObjectType,
[
[
(node, name) => name === "typeAnnotation",
(node, name) => name === "typeAnnotation"
],
[
(node, name) =>
node.type === "FunctionTypeParam" && name === "typeAnnotation"
]
],
shouldHugParameter
)
src/language-js/print/object.js
Outdated
((number === 0 && (name === "params" || name === "parameters")) || | ||
name === "this" || | ||
name === "rest") && | ||
shouldHugFunctionParameters(node) |
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.
If function has .this
and normal parameters, won't this pass on second parameter?
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.
Use getFunctionParameters().indexOf
?
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.
shouldHugFunctionParameters
checks that there is only one parameter, but yes, doing what you propose would be easier to follow.
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.
If shouldHugFunctionParameters
only pass on one parameter. The number check is redundant. Only need make sure it's a parameter.
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.
I'm a little bit confused, the "node" is a function, right? It can't be another function's parameter, how name
can be "parameters"? The "name" is key of child?
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.
Ah yes, this is different from callback of AstPath#each
, the name is key of node there.
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.
Actually, getFunctionParameters().indexOf
looks clumsy because I need to pass a node to it, and to get this node I have to use name
and number
anyway. So I'm going to simply remove the number check.
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.
I also renamed shouldHugFunctionParameters
to shouldHugTheOnlyFunctionParameter
to make it clear that it checks that there is only one parameter.
@@ -157,7 +157,7 @@ function printFunctionParameters( | |||
]; | |||
} | |||
|
|||
function shouldHugFunctionParameters(node) { | |||
function shouldHugTheOnlyFunctionParameter(node) { |
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.
👍
Description
Fixes #13395
Checklist
docs/
directory).changelog_unreleased/*/XXXX.md
file followingchangelog_unreleased/TEMPLATE.md
.✨Try the playground for this PR✨