Skip to content

Commit b02717b

Browse files
authored
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.
1 parent 5d2ad56 commit b02717b

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/src/front_end/ast_node_visitor.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,14 +673,26 @@ final class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
673673
builder.leftBracket(node.leftBracket);
674674

675675
for (var constant in node.constants) {
676+
var isLast = constant == node.constants.last;
677+
var treatAsLast = isLast;
678+
if (isLast && formatter.trailingCommas == TrailingCommas.preserve) {
679+
treatAsLast = constant.commaAfter == null;
680+
}
676681
builder.addCommentsBefore(constant.firstNonCommentToken);
677682
builder.add(
678683
createEnumConstant(
679684
constant,
680-
isLastConstant: constant == node.constants.last,
685+
isLastConstant: treatAsLast,
681686
semicolon: node.semicolon,
682687
),
683688
);
689+
// If this the last constant and wasn't treated as last, we need
690+
// to append the ending semicolon.
691+
if (isLast && !treatAsLast) {
692+
if (node.semicolon case var token?) {
693+
builder.add(tokenPiece(token));
694+
}
695+
}
684696
}
685697

686698
// Insert a blank line between the constants and members.

test/tall/preserve_trailing_commas/enum.unit

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ enum E {e,;}
2525
enum E {
2626
e,
2727
}
28-
>>> Remove trailing comma and split if there are members.
28+
>>> Preserve trailing comma and split if there are members.
2929
enum E { a, b, c,; int x; }
3030
<<<
3131
enum E {
3232
a,
3333
b,
34-
c;
34+
c,
35+
;
3536

3637
int x;
3738
}

0 commit comments

Comments
 (0)