Skip to content

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

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

Conversation

jellynoone
Copy link
Contributor

@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
Fixes #1729

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

@jellynoone
Copy link
Contributor Author

@munificent Have you had a change to go over the PR, yet?

@kevmoo kevmoo requested a review from munificent May 7, 2025 20:50
@jellynoone
Copy link
Contributor Author

@munificent Is there anything else I should do here?

@jellynoone
Copy link
Contributor Author

@munificent Any plans to review this in the near future? Should I add a changelog entry?

@munificent munificent changed the base branch from main to preserve-trailing-commas-enum-with-members July 28, 2025 21:01
@munificent
Copy link
Member

Sorry for the long delay. After getting 3.8 out the door, I needed to take a break from working on the formatter and get caught up on other stuff.

I really like this change. Thank you for the PR!

We're moving to a model where style changes are language versioned. I'll take care of handling that. So I'll merge this into a branch instead of main and then apply the language versioning change on top of that before merging into main. I hope that's OK.

I'll also take care of adding this to the CHANGELOG.

@munificent munificent merged commit b02717b into dart-lang:preserve-trailing-commas-enum-with-members Jul 28, 2025
5 checks passed
@jellynoone
Copy link
Contributor Author

Thank you!

munificent added a commit that referenced this pull request Jul 28, 2025
Preserve trailing commas for enums with members when commas are preserved (#1703)

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

So:
```dart
enum E { a, b, c,; int x; }
```

Becomes:
```dart
enum E {
  a,
  b,
  c,
  ;

  int x;
}
```
Instead of:

```dart
enum E {
  a,
  b,
  c;

  int x;
}
```

Fixes #1678 
Fixes #1729

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.

Co-authored-by: jellynoone <[email protected]>
@jellynoone
Copy link
Contributor Author

We're moving to a model where style changes are language versioned.

I saw this change happening. I considered adding the check but, I figured this was a non-breaking change that it might make sense to always apply since the only people affected are people running trailing-commas: preserve and are already explicitly adding a trailing enum comma. (so this seemed more like a bug fix rather than a feature / style change).

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