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

Fix Flow generic comment positioning #5290

merged 2 commits into from Oct 25, 2018


None yet
4 participants

swac commented Oct 24, 2018

This fixes a bug in Prettier where Flow generic comments get repositioned, breaking Flow's syntax. An example:

foo /*:: <bar> */ (baz);

Currently, Prettier will rewrite this (source) as:

foo(/*:: <bar> */ baz);

This PR attempts to fix this behavior. One issue that I ran into was dealing with the Flow parser's AST that it generates from the comment syntax. Code like this:

/*:: type Props = {
  foo?: ?string,
  bar: number,
}; */

gets turned into this (source):

type Props = {
  foo?: ?string,
  bar: number

I wasn't able to find an easy way to have Prettier reliably traverse the code backward to see if the annotation is inside a comment block. Since it's not in the AST at all (source), the options seem limited, particularly since the comment block could have begun many lines above the type definition. For this reason, this PR only ensures the correct behavior for non-inline type definitions like these for the Babylon parser. The Flow parser's behavior here remains the same as before.

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory)
  • I’ve read the contributing guidelines.


Can you add one more test to make sure calls without arguments work too?

Playground link


foo/*:: <bar>*/()


foo/*:: <bar> */();

lydell approved these changes Oct 25, 2018


j-f1 approved these changes Oct 25, 2018

@j-f1 j-f1 merged commit 253716d into prettier:master Oct 25, 2018

10 checks passed

ci/circleci: build_prod Your tests passed on CircleCI!
ci/circleci: checkout_code Your tests passed on CircleCI!
ci/circleci: test_prod_node4 Your tests passed on CircleCI!
ci/circleci: test_prod_node9 Your tests passed on CircleCI!
ci/circleci: test_prod_standalone Your tests passed on CircleCI!
codecov/patch 100% of diff hit (target 80%)
codecov/project 96.4% (+<.01%) compared to 6a17465
continuous-integration/appveyor/pr AppVeyor build succeeded
continuous-integration/travis-ci/pr The Travis CI build passed
deploy/netlify Deploy preview ready!

This comment has been minimized.


j-f1 commented Oct 25, 2018

Thanks for contributing!

ikatyang added a commit to ikatyang/prettier that referenced this pull request Oct 25, 2018

@ikatyang ikatyang added this to the 1.15 milestone Oct 25, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment