Skip to content

Support doc comment before variant #7535

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

shulhi
Copy link
Member

@shulhi shulhi commented Jun 3, 2025

Fix for #7534

@shulhi shulhi force-pushed the fix/doc-comment-before-variant branch from 1f36000 to e71246b Compare June 3, 2025 12:46
Copy link

pkg-pr-new bot commented Jun 3, 2025

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript-lang/rescript@7535

@rescript/darwin-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-arm64@7535

@rescript/darwin-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-x64@7535

@rescript/linux-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-arm64@7535

@rescript/linux-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-x64@7535

@rescript/win32-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/win32-x64@7535

commit: e6afb36

@shulhi shulhi force-pushed the fix/doc-comment-before-variant branch 3 times, most recently from d77e5e6 to 1c17940 Compare June 4, 2025 04:44
| O_CLOEXEC

| O_KEEPEXEC
Copy link
Member Author

Choose a reason for hiding this comment

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

With this PR, no newlines in between variant constructors. This makes the implementation simpler since I don't need to hack around node's location.

Personally, I prefer the formatting this way too. What do you think? @cknitt @zth @nojaf

Copy link
Collaborator

Choose a reason for hiding this comment

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

Fine for me!

Copy link
Member

Choose a reason for hiding this comment

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

This is great! 🎉

Copy link
Member

Choose a reason for hiding this comment

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

Ok, I meant the new formatting of doc comments before variants is great.

Regarding the newlines, it would be nice if this was still possible:

type x =
  // first group
  | A
  | B
  | C

  // second group
  | D
  | E
  | F

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can't you just put an empty comment line where you want the break?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@shulhi, this is better! I wasn't a fan of adjusting the ranges in the parser to satisfy the format. It felt wrong.

Copy link
Member Author

Choose a reason for hiding this comment

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

This will reformat it to

type x =
  // first group
  | A
  | B
  | C
  // second group
  | D
  | E
  | F

I can still investigate this if we want to preserve the previous behavior. I actually like it without the newline.

The challenge with preserving the previous behavior has to do with how we treat comments and doc comments (comment table and attribute). If we compare the location for the comment for the above example, first group is attached to line 3, while it would be at line 2 for doc comment. We would have to adjust the ranges to make it print the same way for both - which felt like a hack.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, that sounds quite hacky indeed.
I guess I can live with no newlines in that case. 🙂

@shulhi shulhi marked this pull request as ready for review June 4, 2025 04:59
@zth
Copy link
Collaborator

zth commented Jun 4, 2025

@shulhi this is fantastic, a very needed change! I'll let people more familiar with the parser review, but just wanted to give a thumbs up 👍

Copy link
Collaborator

@nojaf nojaf left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

| O_CLOEXEC

| O_KEEPEXEC
Copy link
Collaborator

Choose a reason for hiding this comment

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

@shulhi, this is better! I wasn't a fan of adjusting the ranges in the parser to satisfy the format. It felt wrong.

@shulhi shulhi force-pushed the fix/doc-comment-before-variant branch from 8a71372 to aec4bc7 Compare June 4, 2025 08:06
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.

4 participants