Skip to content

Conversation

@munificent
Copy link
Member

This is the same change you just reviewed + @jellynoone's original PR. Now I'm merging it into main.

jellynoone and others added 2 commits July 28, 2025 14:03
…rved (#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.
@munificent munificent requested review from kallentu and natebosch July 28, 2025 21:54
@munificent munificent merged commit 7278eff into main Jul 28, 2025
7 checks passed
@munificent munificent deleted the preserve-trailing-commas-enum-with-members branch July 28, 2025 22:09
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.

3 participants