Skip to content

Commit 9e8e3a1

Browse files
committed
Preserve trailing commas for enums with members when commas are preserved
1 parent 100db45 commit 9e8e3a1

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
@@ -648,14 +648,26 @@ final class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
648648
builder.leftBracket(node.leftBracket);
649649

650650
for (var constant in node.constants) {
651+
var isLast = constant == node.constants.last;
652+
var treatAsLast = isLast;
653+
if (isLast && formatter.trailingCommas == TrailingCommas.preserve) {
654+
treatAsLast = constant.commaAfter == null;
655+
}
651656
builder.addCommentsBefore(constant.firstNonCommentToken);
652657
builder.add(
653658
createEnumConstant(
654659
constant,
655-
isLastConstant: constant == node.constants.last,
660+
isLastConstant: treatAsLast,
656661
semicolon: node.semicolon,
657662
),
658663
);
664+
// If this the last constant and wasn't treated as last, we need
665+
// to append the ending semicolon.
666+
if (isLast && !treatAsLast) {
667+
if (node.semicolon case var token?) {
668+
builder.add(tokenPiece(token));
669+
}
670+
}
659671
}
660672

661673
// 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)