Skip to content

Commit 4630b3d

Browse files
committed
Preserve trailing commas for enums with members when commas are preserved
1 parent eaa3f2d commit 4630b3d

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

628628
for (var constant in node.constants) {
629+
var isLast = constant == node.constants.last;
630+
var treatAsLast = isLast;
631+
if (isLast && formatter.trailingCommas == TrailingCommas.preserve) {
632+
treatAsLast = constant.commaAfter == null;
633+
}
629634
builder.addCommentsBefore(constant.firstNonCommentToken);
630635
builder.add(
631636
createEnumConstant(
632637
constant,
633-
isLastConstant: constant == node.constants.last,
638+
isLastConstant: treatAsLast,
634639
semicolon: node.semicolon,
635640
),
636641
);
642+
// If this the last constant and wasn't treated as last, we need
643+
// to append the ending semicolon.
644+
if (isLast && !treatAsLast) {
645+
if (node.semicolon case var token?) {
646+
builder.add(tokenPiece(token));
647+
}
648+
}
637649
}
638650

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