Commit 7c4a960
authored
Format
Switch statements allow multiple cases to share a body like:
```dart
switch (obj) {
case pattern1:
case pattern2:
body;
}
```
Switch expressions don't support that, but `||` patterns are the idiomatic way to accomplish the same thing. Because of that, the formatter has some special formatting when the outermost pattern in a switch expression case is `||`:
```dart
x = switch (obj) {
pattern1 ||
pattern2 =>
body,
};
```
Note how the `pattern2` operand isn't indented.
This PR extends that special handling to allow the `=>` on the same line as the `=>` even if the pattern is a split `||` pattern, like:
```dart
x = switch (obj) {
pattern1 ||
pattern2 => body,
};
```
And it prefers to split the `||` over the body when the body is block formatted:
```dart
// Prefer:
x = switch (obj) {
pattern1 ||
pattern2 => function(argument),
};
// Over:
x = switch (obj) {
pattern1 || pattern2 => function(
argument,
),
};
```
This is one of those rules that's mostly a matter of taste, but I ran this on a large corpus and most of the diffs look better to me. Here are a few examples:
```dart
// Before:
typeName = switch (targetType) {
DriftSqlType.int || DriftSqlType.bigInt || DriftSqlType.bool =>
'INTEGER',
DriftSqlType.string => 'CHAR',
DriftSqlType.double => 'DOUBLE',
DriftSqlType.blob => 'BINARY',
DriftSqlType.dateTime => 'DATETIME',
DriftSqlType.any => '',
CustomSqlType() || DialectAwareSqlType() => targetType.sqlTypeName(
context,
),
};
// After:
typeName = switch (targetType) {
DriftSqlType.int ||
DriftSqlType.bigInt ||
DriftSqlType.bool => 'INTEGER',
DriftSqlType.string => 'CHAR',
DriftSqlType.double => 'DOUBLE',
DriftSqlType.blob => 'BINARY',
DriftSqlType.dateTime => 'DATETIME',
DriftSqlType.any => '',
CustomSqlType() ||
DialectAwareSqlType() => targetType.sqlTypeName(context),
};
// Before:
return switch (side) {
AxisSide.right || AxisSide.left =>
titlesPadding.vertical + borderPadding.vertical,
AxisSide.top || AxisSide.bottom =>
titlesPadding.horizontal + borderPadding.horizontal,
};
// After:
return switch (side) {
AxisSide.right ||
AxisSide.left => titlesPadding.vertical + borderPadding.vertical,
AxisSide.top ||
AxisSide.bottom => titlesPadding.horizontal + borderPadding.horizontal,
};
// Before:
final defaultConstraints = switch (side) {
ShadSheetSide.top || ShadSheetSide.bottom => BoxConstraints(
minWidth: mSize.width,
),
ShadSheetSide.left || ShadSheetSide.right => BoxConstraints(
minHeight: mSize.height,
),
};
final defaultConstraints = switch (side) {
ShadSheetSide.top ||
ShadSheetSide.bottom => BoxConstraints(minWidth: mSize.width),
ShadSheetSide.left ||
ShadSheetSide.right => BoxConstraints(minHeight: mSize.height),
};
```
Fix #1602.|| patterns like fallthrough cases in switch expressions. (#1620)1 parent 47bcc07 commit 7c4a960
File tree
6 files changed
+69
-29
lines changed- lib/src/piece
- test/tall
- expression
- regression
- 1100
- 1600
6 files changed
+69
-29
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
11 | 18 | | |
12 | | - | |
| 19 | + | |
13 | 20 | | |
14 | 21 | | |
15 | | - | |
| 22 | + | |
16 | 23 | | |
17 | | - | |
| 24 | + | |
18 | 25 | | |
19 | 26 | | |
20 | 27 | | |
| |||
54 | 61 | | |
55 | 62 | | |
56 | 63 | | |
| 64 | + | |
57 | 65 | | |
58 | 66 | | |
59 | 67 | | |
60 | 68 | | |
61 | 69 | | |
62 | 70 | | |
63 | 71 | | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
64 | 78 | | |
65 | | - | |
66 | | - | |
67 | | - | |
| 79 | + | |
| 80 | + | |
68 | 81 | | |
69 | 82 | | |
70 | 83 | | |
| |||
94 | 107 | | |
95 | 108 | | |
96 | 109 | | |
97 | | - | |
| 110 | + | |
| 111 | + | |
98 | 112 | | |
99 | 113 | | |
100 | 114 | | |
101 | 115 | | |
102 | | - | |
| 116 | + | |
103 | 117 | | |
104 | 118 | | |
105 | 119 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
123 | | - | |
124 | | - | |
| 123 | + | |
125 | 124 | | |
126 | 125 | | |
127 | 126 | | |
| |||
175 | 174 | | |
176 | 175 | | |
177 | 176 | | |
178 | | - | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | | - | |
100 | | - | |
| 99 | + | |
101 | 100 | | |
102 | 101 | | |
103 | 102 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
28 | 31 | | |
29 | 32 | | |
30 | 33 | | |
| |||
38 | 41 | | |
39 | 42 | | |
40 | 43 | | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
49 | 51 | | |
50 | 52 | | |
51 | 53 | | |
| |||
77 | 79 | | |
78 | 80 | | |
79 | 81 | | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
80 | 85 | | |
81 | 86 | | |
82 | 87 | | |
| |||
92 | 97 | | |
93 | 98 | | |
94 | 99 | | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
103 | 107 | | |
104 | 108 | | |
105 | 109 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
0 commit comments