Skip to content

Commit a6ad769

Browse files
authored
Force switch expressions to split if they contain a line comment. (#1405)
The fix here is pretty hacky, but this is in the old formatter which will be going away hopefully relatively soon. I honestly don't know how to fix it more elegantly and I'm a little worried that any more systemic fix will cause knock-on bugs since I don't have the old code loaded in my head as well as I used to. Fixes #1404.
1 parent 9dd81d6 commit a6ad769

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## 2.3.5-wip
22

3+
* Ensure switch expressions containing line comments split (#1404).
34
* Use language version `3.3` to parse so that code with extension types can be
45
formatted.
56
* Support formatting the `macro` modifier when the `macros` experiment flag
@@ -11,7 +12,7 @@
1112
formatting style (#1253).
1213
* Format extension types.
1314
* Normalize ignored whitespace and "escaped whitespace" on first line
14-
of multiline string literals. (#1235)
15+
of multiline string literals (#1235).
1516

1617
## 2.3.3
1718

lib/src/source_visitor.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2829,7 +2829,15 @@ class SourceVisitor extends ThrowingAstVisitor {
28292829

28302830
var hasTrailingComma =
28312831
node.cases.isNotEmpty && node.cases.last.commaAfter != null;
2832-
_endBody(node.rightBracket, forceSplit: hasTrailingComma);
2832+
2833+
// TODO(rnystrom): If there is a line comment at the end of a case, make
2834+
// sure the switch expression splits. Looking for line comments explicitly
2835+
// instead of having them harden the surrounding rules is a hack. But this
2836+
// code will be going away when we move to the new Piece representation, so
2837+
// going with something expedient.
2838+
var forceSplit = _containsLineComments(node.cases, node.rightBracket);
2839+
2840+
_endBody(node.rightBracket, forceSplit: hasTrailingComma || forceSplit);
28332841
}
28342842

28352843
@override

test/regression/1400/1404.stmt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
>>>
2+
int weird(Object o) {
3+
return switch (o) {
4+
int i => i, // evil comment
5+
_ => 42
6+
};
7+
}
8+
<<<
9+
int weird(Object o) {
10+
return switch (o) {
11+
int i => i, // evil comment
12+
_ => 42
13+
};
14+
}
15+
>>> line comment inside case value expression
16+
int weird(Object o) {
17+
return switch (o) {
18+
1 => f(// c
19+
),
20+
_ => 42
21+
};
22+
}
23+
<<<
24+
int weird(Object o) {
25+
return switch (o) {
26+
1 => f(// c
27+
),
28+
_ => 42
29+
};
30+
}
31+
>>> line comment inside case pattern
32+
int weird(Object o) {
33+
return switch (o) {
34+
[1 // c
35+
] => 2,
36+
3 => 4
37+
};
38+
}
39+
<<<
40+
int weird(Object o) {
41+
return switch (o) {
42+
[
43+
1 // c
44+
] =>
45+
2,
46+
3 => 4
47+
};
48+
}

0 commit comments

Comments
 (0)