Skip to content

Fix bug parsing anonymous closure types from Flow. #672

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

Merged
merged 1 commit into from
Feb 19, 2017

Conversation

BurntSushi
Copy link
Contributor

If no parameter name exists in a function type, then set it to empty. Also,
fix the formatter so that {param}: {ty} is formatted as {ty} when
{param} is empty.

Fixes #671

If no parameter name exists in a function type, then set it to empty. Also,
fix the formatter so that `{param}: {ty}` is formatted as `{ty}` when
`{param}` is empty.

Fixes documentationjs#671
@BurntSushi
Copy link
Contributor Author

BurntSushi commented Feb 15, 2017

I tried to write a test in test/format_type.js, but simple expressions like function(): any => any failed to parse with not ok TypeError: Cannot read property 'type' of undefined. (Specifcally, it looks like tags is empty in the result of doctrine.parse.)

@arv
Copy link
Contributor

arv commented Feb 15, 2017

I feel like this just tries to work around the issue. We should understand what is really going on here first.

@BurntSushi
Copy link
Contributor Author

I feel like this just tries to work around the issue. We should understand what is really going on here first.

Is there really anything deeper here other than "function parameters in a type signature may be unnamed"?

I don't keep abreast of Flow changes, so maybe a dep bump is needed, but it seems reasonable to have some case analysis like there is in this PR for unnamed parameters. Or were you thinking of something else?

Copy link
Contributor

@arv arv 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 being slow to review this.

I've now looked at the AST that Babel/Flow uses:

https://flowtype.org/try/#0PTAEAEDMBsHsHcBQiAuBPADgU1AFVALygB2ArgLYBGWAToQHygDOKNAlsQOYDcQA

and the name ends up as null so this change is good.

Can you add a file to test/fixtures that contains

// @flow
/** doc */
type T = number => string;

and run the test with UPDATE=1 ... to generate the files.

@@ -118,7 +118,10 @@ function formatType(getHref/*: Function*/, node/*: ?Object */) {
case Syntax.NameExpression:
return [link(node.name, getHref)];
case Syntax.ParameterType:
return [t(node.name + ': ')].concat(formatType(getHref, node.expression));
if (node.name) {
result.push(t(node.name + ': '));
Copy link
Contributor

Choose a reason for hiding this comment

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

So this prints ': T' for the type number => string. Maybe it should just print T?

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, it prints nothing in the case of an empty name... not sure that is what we want?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the name is empty, then it prints the value of result.concat(formatType(getHref, node.expression)).

@arv
Copy link
Contributor

arv commented Feb 19, 2017

Landing this with a follow up commit that has a fixture test.

@arv arv merged commit 5bd42d6 into documentationjs:master Feb 19, 2017
arv added a commit to arv/documentation that referenced this pull request Feb 19, 2017
This allows `type T = number => string`. Previously we were only
handling `type T = (x: number) => string` correctly.

Followup to documentationjs#672 that:
 - Adds a test fixture
 - Fixes lint error
arv added a commit that referenced this pull request Feb 19, 2017
This allows `type T = number => string`. Previously we were only
handling `type T = (x: number) => string` correctly.

Followup to #672 that:
 - Adds a test fixture
 - Fixes lint errors
@BurntSushi BurntSushi deleted the fix-671 branch February 20, 2017 00:27
@BurntSushi
Copy link
Contributor Author

@arv Thank you so much. :-)

@BurntSushi
Copy link
Contributor Author

@arv Sorry for the ping, but is there any chance of pushing out a new release with this fix? Thanks. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fails on flow function type
2 participants