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
No parenthesis for Flow shorthand with one arg #972
No parenthesis for Flow shorthand with one arg #972
Conversation
src/printer.js
Outdated
@@ -2062,8 +2062,12 @@ function printFunctionParams(path, print, options) { | |||
return concat(["(", join(", ", printed), ")"]); | |||
} | |||
|
|||
const isFlowShorthandWithOneArg = fun.type === "FunctionTypeAnnotation" && | |||
parent.type === "ObjectTypeProperty" && fun.params.length === 1 && |
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, I don't need that...
Can you add test cases for: type T = { method: (a) => void };
type T = { method(a): void };
declare class X { method(a): void }
declare function f(a): void;
var f: (a) => void; |
Yeah, that's what I suspected, the "normal" solution completely breaks: type T = { method: a => void };
type T = { methoda: void };
declare class X { methoda: void }
declare function fa: void;
var f: a => void; Also, this is just a sample of all the ways you can combine this ast. I'm honestly super scared about breaking something here. |
Yeah I've just seen that... will try to see what I can do... |
Checking the equality of the |
Ok, this is now a regression for some other tests. |
@vjeux I think I got something better 🤞. At least the tests are passing, but I maybe need more test cases, not quite sure about it. |
@cpojer do you have some other examples coming in mind so that I can test my implementation against it? |
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.
Sorry for the super late reviews, React Conf has been kind of crazy. If you can address the two comments, then it looks good to go. I really hope that it's not going to break anything else :)
Great work on tackling a non trivial thing!
src/printer.js
Outdated
@@ -2062,8 +2062,12 @@ function printFunctionParams(path, print, options) { | |||
return concat(["(", join(", ", printed), ")"]); | |||
} | |||
|
|||
const isFlowShorthandWithOneArg = fun.type === "FunctionTypeAnnotation" && |
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.
Could you use isObjectTypePropertyAFunction helper in order to do this check: https://github.com/prettier/prettier/blob/master/src/printer.js#L3064-L3072
This way we don't have random location comparison spread across the codebase.
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.
Ok, done. But I needed to create a similar one called isTypeAnnotationAFunction
and also keep a checking for TypeAlias too 😄.
|
||
declare function f(a): void; | ||
|
||
var f: (a) => void; |
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.
A few more things to test:
interface F { m(string): number };
interface F { m: (string) => number };
function f(o: { f: (string) => void }) {}
function f(o: { f(string): void }) {}
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.
Done and passing with my implementation!
// Hack to differentiate between the following two which have the same ast | ||
// declare function f(a): void; | ||
// var f: (a) => void; | ||
function isTypeAnnotationAFunction(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.
Similar to isObjectTypePropertyAFunction
.
@vjeux I've just noticed the build passed for v4 only and failed for no reason of the other two. |
@@ -2062,8 +2062,12 @@ function printFunctionParams(path, print, options) { | |||
return concat(["(", join(", ", printed), ")"]); | |||
} | |||
|
|||
const isFlowShorthandWithOneArg = (isObjectTypePropertyAFunction(parent) || | |||
isTypeAnnotationAFunction(parent) || parent.type === "TypeAlias") && | |||
fun.params.length === 1 && fun.params[0].name === null && fun.rest === 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.
Looks like this has less cases than arrow function:
n.params.length === 1 &&
!n.rest &&
n.params[0].type === "Identifier" &&
!n.params[0].typeAnnotation &&
!n.params[0].leadingComments &&
!n.params[0].trailingComments &&
!n.params[0].optional &&
!n.predicate &&
!n.returnType
https://github.com/prettier/prettier/blob/master/src/printer.js#L304-L313
Can you add the following tests as well:
type f = (...arg) => void;
type f = (/* comment */ arg) => void;
type f = (arg /* comment */) => void;
type f = (?arg) => void;
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.
Ok, I will do it!
Looks like both builds got stuck downloading node after 50min. I just restarted them. |
@vjeux done! |
Boom! Thanks a lot! |
Experimental PR trying to solve #969.