Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always split enums that contain a line comment. #1262

Merged
merged 1 commit into from
Sep 12, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 2.3.3-dev

* Always split enum declarations containing a line comment (#1254).
* Fix regression in splitting type annotations with library prefixes (#1249).
* Remove support for `inline class` since that syntax has changed.
* Add `--enable-experiment` command-line option to enable language experiments.
Expand Down
19 changes: 15 additions & 4 deletions lib/src/source_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,13 @@ class SourceVisitor extends ThrowingAstVisitor {
_endBody(node.rightBracket,
forceSplit: semicolon != null ||
trailingComma != null ||
node.members.isNotEmpty);
node.members.isNotEmpty ||
// If there is a line comment after an enum constant, it won't
// automatically force the enum body to split since the rule for
// the constants is the hard rule used by the entire block and its
// hardening state doesn't actually change. Instead, look
// explicitly for a line comment here.
_containsLineComments(node.constants));
}

@override
Expand Down Expand Up @@ -3928,12 +3934,13 @@ class SourceVisitor extends ThrowingAstVisitor {
return Cost.assign;
}

/// Returns `true` if the collection withs [elements] delimited by
/// Returns `true` if the collection with [elements] delimited by
/// [rightBracket] contains any line comments.
///
/// This only looks for comments at the element boundary. Comments within an
/// element are ignored.
bool _containsLineComments(Iterable<AstNode> elements, Token rightBracket) {
bool _containsLineComments(Iterable<AstNode> elements,
[Token? rightBracket]) {
bool hasLineCommentBefore(Token token) {
Token? comment = token.precedingComments;
for (; comment != null; comment = comment.next) {
Expand All @@ -3949,7 +3956,11 @@ class SourceVisitor extends ThrowingAstVisitor {
}

// Look before the closing bracket.
return hasLineCommentBefore(rightBracket);
if (rightBracket != null) {
if (hasLineCommentBefore(rightBracket)) return true;
}

return false;
}

/// Begins writing a bracket-delimited body whose contents are a nested
Expand Down
21 changes: 21 additions & 0 deletions test/comments/enums.unit
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,25 @@ enum Foo {
enum Foo {
a,
b // comment
}
>>> line comment in middle
enum Foo {
a, // comment
b
}
<<<
enum Foo {
a, // comment
b
}
>>> line comment in member constructor
enum Foo {
a(// c
),b()
}
<<<
enum Foo {
a(// c
),
b()
}
12 changes: 12 additions & 0 deletions test/regression/1200/1254.unit
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
>>>
enum EE {
a,
b, // 'b'
c
}
<<<
enum EE {
a,
b, // 'b'
c
}