Skip to content

Commit 5749a94

Browse files
authored
Always split switch expressions (#1537)
Always split switch expressions. I figured that since other comma-delimited expression forms don't split if they don't have to, switch expressions should be the same. But I went and checked on a huge corpus and every single place where a switch expression wasn't split was clearly much worse than splitting it would be. Fix #1529.
1 parent 02d5bc2 commit 5749a94

File tree

5 files changed

+81
-11
lines changed

5 files changed

+81
-11
lines changed

lib/src/front_end/ast_node_visitor.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1718,7 +1718,7 @@ class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
17181718
}
17191719

17201720
list.rightBracket(node.rightBracket);
1721-
pieces.add(list.build());
1721+
pieces.add(list.build(forceSplit: node.cases.isNotEmpty));
17221722
}
17231723

17241724
@override

lib/src/front_end/delimited_list_builder.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class DelimitedListBuilder {
5151

5252
/// Creates the final [ListPiece] out of the added brackets, delimiters,
5353
/// elements, and style.
54-
Piece build() {
54+
Piece build({bool forceSplit = false}) {
5555
// To simplify the piece tree, if there are no elements, just return the
5656
// brackets concatenated together. We don't have to worry about comments
5757
// here since they would be in the [_elements] list if there were any.
@@ -66,7 +66,7 @@ class DelimitedListBuilder {
6666

6767
var piece =
6868
ListPiece(_leftBracket, _elements, _blanksAfter, _rightBracket, _style);
69-
if (_mustSplit) piece.pin(State.split);
69+
if (_mustSplit || forceSplit) piece.pin(State.split);
7070
return piece;
7171
}
7272

test/tall/expression/switch.stmt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@
33
e = switch(y) {};
44
<<<
55
e = switch (y) {};
6-
>>> All one line.
6+
>>> Always split cases even if they would fit.
77
e = switch (c) { 0 => a, 1 => b };
88
<<<
9-
e = switch (c) { 0 => a, 1 => b };
9+
e = switch (c) {
10+
0 => a,
11+
1 => b,
12+
};
1013
>>> One case per line.
1114
e = switch (c) { 0 => first, 1 => second };
1215
<<<
1316
e = switch (c) {
1417
0 => first,
1518
1 => second,
1619
};
17-
>>> Remove trailing comma if cases fit on one line.
18-
e = switch (c) { 0 => a, 1 => b, };
19-
<<<
20-
e = switch (c) { 0 => a, 1 => b };
2120
>>> Split some cases at "=>" but not all.
2221
e = switch (c) {
2322
first => a,

test/tall/invocation/block_argument_multiple.stmt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,12 @@ function(before, switch (n) {
137137
function(switch (a) { 1 => 2 }, switch (b) { 1 => 2 });
138138
<<<
139139
function(
140-
switch (a) { 1 => 2 },
141-
switch (b) { 1 => 2 },
140+
switch (a) {
141+
1 => 2,
142+
},
143+
switch (b) {
144+
1 => 2,
145+
},
142146
);
143147
>>> Empty and non-empty switches.
144148
function(switch (a) {}, switch (b) { 1 => 2 }, switch (c) {});

test/tall/regression/1500/1529.stmt

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
>>> (indent 4)
2+
return switch (that) { Family() => that, $FamilyOverride() => that.from };
3+
<<<
4+
return switch (that) {
5+
Family() => that,
6+
$FamilyOverride() => that.from,
7+
};
8+
>>> (indent 6)
9+
x = switch (event) {
10+
ProviderDisposeEvent() => switch (event
11+
.debugOrigin) { ProviderDisposeEvent e => [e.refenaId], _ => null },
12+
};
13+
<<<
14+
x = switch (event) {
15+
ProviderDisposeEvent() => switch (event.debugOrigin) {
16+
ProviderDisposeEvent e => [e.refenaId],
17+
_ => null,
18+
},
19+
};
20+
>>> (indent 6)
21+
x = switch (event) {
22+
RebuildEvent() => switch (event
23+
.debugOrigin) { BaseReduxAction a => a.refenaId, _ => null },
24+
ActionDispatchedEvent() => switch (event
25+
.debugOriginRef) { BaseReduxAction a => a.refenaId, _ => null },
26+
MessageEvent() => switch (event
27+
.origin) { BaseReduxAction a => a.refenaId, _ => null },
28+
};
29+
<<<
30+
x = switch (event) {
31+
RebuildEvent() => switch (event.debugOrigin) {
32+
BaseReduxAction a => a.refenaId,
33+
_ => null,
34+
},
35+
ActionDispatchedEvent() => switch (event.debugOriginRef) {
36+
BaseReduxAction a => a.refenaId,
37+
_ => null,
38+
},
39+
MessageEvent() => switch (event.origin) {
40+
BaseReduxAction a => a.refenaId,
41+
_ => null,
42+
},
43+
};
44+
>>> (indent 2)
45+
T? valueOrNull() => switch (this) { Data(:var value) => value, _ => null };
46+
<<<
47+
T? valueOrNull() => switch (this) {
48+
Data(:var value) => value,
49+
_ => null,
50+
};
51+
>>> (indent 2)
52+
String transcription(Locale intl) => switch (intl
53+
.languageCode) { 'zh' => zhCN(this), 'be' => be(this), _ => basic(this) };
54+
<<<
55+
String transcription(Locale intl) => switch (intl.languageCode) {
56+
'zh' => zhCN(this),
57+
'be' => be(this),
58+
_ => basic(this),
59+
};
60+
>>> (indent 4)
61+
return switch (x) { <= 1 => x, >= 3 => x - 4, _ => 2 - x };
62+
<<<
63+
return switch (x) {
64+
<= 1 => x,
65+
>= 3 => x - 4,
66+
_ => 2 - x,
67+
};

0 commit comments

Comments
 (0)