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

No parenthesis for Flow shorthand with one arg #972

Merged

Conversation

yamafaktory
Copy link
Contributor

Experimental PR trying to solve #969.

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 &&
Copy link
Contributor Author

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...

@vjeux
Copy link
Contributor

vjeux commented Mar 9, 2017

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;

@vjeux
Copy link
Contributor

vjeux commented Mar 9, 2017

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.

@yamafaktory
Copy link
Contributor Author

yamafaktory commented Mar 9, 2017

Yeah I've just seen that... will try to see what I can do...

@yamafaktory
Copy link
Contributor Author

Checking the equality of the start of the fun and the one of the first param seems to fix it.

@yamafaktory
Copy link
Contributor Author

Ok, this is now a regression for some other tests.

@yamafaktory
Copy link
Contributor Author

@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.

@yamafaktory
Copy link
Contributor Author

@cpojer do you have some other examples coming in mind so that I can test my implementation against it?

Copy link
Contributor

@vjeux vjeux left a 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" &&
Copy link
Contributor

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.

Copy link
Contributor Author

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

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 }) {}

Copy link
Contributor Author

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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Similar to isObjectTypePropertyAFunction.

@yamafaktory
Copy link
Contributor Author

@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;
Copy link
Contributor

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;

Copy link
Contributor Author

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!

@vjeux
Copy link
Contributor

vjeux commented Mar 16, 2017

Looks like both builds got stuck downloading node after 50min. I just restarted them.

@yamafaktory
Copy link
Contributor Author

@vjeux done!

@vjeux vjeux merged commit 915967b into prettier:master Mar 16, 2017
@vjeux
Copy link
Contributor

vjeux commented Mar 16, 2017

Boom! Thanks a lot!

@yamafaktory yamafaktory deleted the 969-flow-shorthand-one-arg-no-parens branch March 16, 2017 17:34
@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jan 21, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jan 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants