Skip to content

Commit 1a2def9

Browse files
authored
Always split enums that contain a line comment. (#1262)
Fix #1254.
1 parent 5047cf8 commit 1a2def9

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## 2.3.3-dev
22

3+
* Always split enum declarations containing a line comment (#1254).
34
* Fix regression in splitting type annotations with library prefixes (#1249).
45
* Remove support for `inline class` since that syntax has changed.
56
* Add `--enable-experiment` command-line option to enable language experiments.

lib/src/source_visitor.dart

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,13 @@ class SourceVisitor extends ThrowingAstVisitor {
10621062
_endBody(node.rightBracket,
10631063
forceSplit: semicolon != null ||
10641064
trailingComma != null ||
1065-
node.members.isNotEmpty);
1065+
node.members.isNotEmpty ||
1066+
// If there is a line comment after an enum constant, it won't
1067+
// automatically force the enum body to split since the rule for
1068+
// the constants is the hard rule used by the entire block and its
1069+
// hardening state doesn't actually change. Instead, look
1070+
// explicitly for a line comment here.
1071+
_containsLineComments(node.constants));
10661072
}
10671073

10681074
@override
@@ -3928,12 +3934,13 @@ class SourceVisitor extends ThrowingAstVisitor {
39283934
return Cost.assign;
39293935
}
39303936

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

39513958
// Look before the closing bracket.
3952-
return hasLineCommentBefore(rightBracket);
3959+
if (rightBracket != null) {
3960+
if (hasLineCommentBefore(rightBracket)) return true;
3961+
}
3962+
3963+
return false;
39533964
}
39543965

39553966
/// Begins writing a bracket-delimited body whose contents are a nested

test/comments/enums.unit

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,25 @@ enum Foo {
9595
enum Foo {
9696
a,
9797
b // comment
98+
}
99+
>>> line comment in middle
100+
enum Foo {
101+
a, // comment
102+
b
103+
}
104+
<<<
105+
enum Foo {
106+
a, // comment
107+
b
108+
}
109+
>>> line comment in member constructor
110+
enum Foo {
111+
a(// c
112+
),b()
113+
}
114+
<<<
115+
enum Foo {
116+
a(// c
117+
),
118+
b()
98119
}

test/regression/1200/1254.unit

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
>>>
2+
enum EE {
3+
a,
4+
b, // 'b'
5+
c
6+
}
7+
<<<
8+
enum EE {
9+
a,
10+
b, // 'b'
11+
c
12+
}

0 commit comments

Comments
 (0)