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

Preserve trailing commas for enums with members when trailing_commas are preserved #1703

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jellynoone
Copy link

@jellynoone jellynoone commented Apr 4, 2025

This PR changes the formatter to have it maintain trailing commas of last enum constants when there are members.

So:

enum E { a, b, c,; int x; }

Becomes:

enum E {
  a,
  b,
  c,
  ;

  int x;
}

Instead of:

enum E {
  a,
  b,
  c;

  int x;
}

Fixes #1678

Additionally, a similar style choice was presented in #1660 (comment)

Affected users:

  • Those passing the trailing_commas: preserve config option and adding a trailing comma to their enum cases.
  • Users who don't already pass a trailing comma, aren't affected. So this PR shouldn't have undesirable effects on existing users.

Benefits:

  • Maintains developer's intent
  • Less churn when adding additional member cases
    (Personally, I always add a trailing semicolon to avoid churn and to also make it even clearer this is an advanced enum with members.)

Downsides:

  • 7 additional logical lines of code to maintain

Additional considerations:
I also considered implementing this for enum E { a, b,; }, however, that would require more drastic changes to the formatter as far as I can tell.

Additionally, in this case the trailing semicolon provides fewer benefits:

  • churn when adding new members is the same if there was a trailing semicolon or not
  • giving the enum other members also only amounts to new lines being added rather than changed so again, no real churn.

Notes

  • One test was changed due to this being a change in behavior.
  • This was surprisingly easy and enjoyable to implement (compliment to the existing code base).
  • I didn't make any changes to the CHANGELOG.md since this trailing commas preservation hasn't shipped yet so its behaviour is still undefined.

@jellynoone
Copy link
Author

@munificent I saw your recent changes guarding the formatter with a language version flag. Please advise if the changes here require any additional modifications.

FYI, I pulled in the new changes, and all ran successfully.

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.

Trailing commas for enhanced enums are not preserved
1 participant