Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/src/front_end/ast_node_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -648,14 +648,26 @@ final class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
builder.leftBracket(node.leftBracket);

for (var constant in node.constants) {
var isLast = constant == node.constants.last;
var treatAsLast = isLast;
if (isLast && formatter.trailingCommas == TrailingCommas.preserve) {
treatAsLast = constant.commaAfter == null;
}
builder.addCommentsBefore(constant.firstNonCommentToken);
builder.add(
createEnumConstant(
constant,
isLastConstant: constant == node.constants.last,
isLastConstant: treatAsLast,
semicolon: node.semicolon,
),
);
// If this the last constant and wasn't treated as last, we need
// to append the ending semicolon.
if (isLast && !treatAsLast) {
if (node.semicolon case var token?) {
builder.add(tokenPiece(token));
}
}
}

// Insert a blank line between the constants and members.
Expand Down
5 changes: 3 additions & 2 deletions test/tall/preserve_trailing_commas/enum.unit
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ enum E {e,;}
enum E {
e,
}
>>> Remove trailing comma and split if there are members.
>>> Preserve trailing comma and split if there are members.
enum E { a, b, c,; int x; }
<<<
enum E {
a,
b,
c;
c,
;

int x;
}
Expand Down