Skip to content

Commit d4cef9a

Browse files
Don't insert space into empty switches. (#1199)
Fixes #1198.
1 parent 568333d commit d4cef9a

File tree

4 files changed

+130
-8
lines changed

4 files changed

+130
-8
lines changed

lib/src/source_visitor.dart

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,6 +2723,15 @@ class SourceVisitor extends ThrowingAstVisitor {
27232723

27242724
@override
27252725
void visitSwitchExpression(SwitchExpression node) {
2726+
if (node.cases.isEmptyBody(node.rightBracket)) {
2727+
// Don't allow splitting an empty switch expression.
2728+
_visitSwitchValue(node.switchKeyword, node.leftParenthesis,
2729+
node.expression, node.rightParenthesis);
2730+
token(node.leftBracket);
2731+
token(node.rightBracket);
2732+
return;
2733+
}
2734+
27262735
// Start the rule for splitting between the cases before the value. That
27272736
// way, if the value expression splits, the cases do too. Avoids:
27282737
//
@@ -2735,12 +2744,10 @@ class SourceVisitor extends ThrowingAstVisitor {
27352744
node.rightParenthesis);
27362745

27372746
token(node.leftBracket);
2738-
builder = builder.startBlock(space: true);
2747+
builder = builder.startBlock(space: node.cases.isNotEmpty);
27392748

27402749
visitCommaSeparatedNodes(node.cases, between: split);
27412750

2742-
// A switch with no cases isn't syntactically valid, but handle it
2743-
// gracefully instead of crashing.
27442751
var hasTrailingComma =
27452752
node.cases.isNotEmpty && node.cases.last.commaAfter != null;
27462753
_endBody(node.rightBracket, forceSplit: hasTrailingComma);
@@ -2836,8 +2843,10 @@ class SourceVisitor extends ThrowingAstVisitor {
28362843
}
28372844
}
28382845

2839-
newline();
2840-
_endBody(node.rightBracket, forceSplit: true);
2846+
if (node.members.isNotEmpty) {
2847+
newline();
2848+
}
2849+
_endBody(node.rightBracket, forceSplit: node.members.isNotEmpty);
28412850
}
28422851

28432852
/// Visits the `switch (expr)` part of a switch statement or expression.

test/comments/switch.stmt

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,86 @@ e = switch (n) {
144144
0 => zero, // comment
145145
1 => one, // comment
146146
2 => two // comment
147-
};
147+
};
148+
>>> line comment
149+
switch (e) {
150+
// comment
151+
}
152+
<<<
153+
switch (e) {
154+
// comment
155+
}
156+
>>> line comment on opening line
157+
switch (e) { // comment
158+
}
159+
<<<
160+
switch (e) {
161+
// comment
162+
}
163+
>>> indented block comment
164+
switch (e) {
165+
/* comment */
166+
}
167+
<<<
168+
switch (e) {
169+
/* comment */
170+
}
171+
>>> block comment with trailing newline
172+
switch (e) {/* comment */
173+
}
174+
<<<
175+
switch (e) {
176+
/* comment */
177+
}
178+
>>> block comment with leading newline
179+
switch (e) {
180+
/* comment */}
181+
<<<
182+
switch (e) {
183+
/* comment */
184+
}
185+
>>> inline block comment
186+
switch (e) { /* comment */ }
187+
<<<
188+
switch (e) {/* comment */}
189+
>>> line comment
190+
e = switch (n) {
191+
// comment
192+
};
193+
<<<
194+
e = switch (n) {
195+
// comment
196+
};
197+
>>> line comment on opening line
198+
e = switch (n) { // comment
199+
};
200+
<<<
201+
e = switch (n) {
202+
// comment
203+
};
204+
>>> indented block comment
205+
e = switch (n) {
206+
/* comment */
207+
};
208+
<<<
209+
e = switch (n) {
210+
/* comment */
211+
};
212+
>>> block comment with trailing newline
213+
e = switch (n) {/* comment */
214+
};
215+
<<<
216+
e = switch (n) {
217+
/* comment */
218+
};
219+
>>> block comment with leading newline
220+
e = switch (n) {
221+
/* comment */};
222+
<<<
223+
e = switch (n) {
224+
/* comment */
225+
};
226+
>>> inline block comment
227+
e = switch (n) { /* comment */ };
228+
<<<
229+
e = switch (n) {/* comment */};

test/regression/1100/1198.stmt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
40 columns |
2+
>>>
3+
switch (e) {}
4+
<<<
5+
switch (e) {}
6+
>>>
7+
switch ("a long string that must wrap") {}
8+
<<<
9+
switch (
10+
"a long string that must wrap") {}
11+
>>>
12+
switch ([1,]) {}
13+
<<<
14+
switch ([
15+
1,
16+
]) {}
17+
>>>
18+
e = switch (e) {};
19+
<<<
20+
e = switch (e) {};
21+
>>>
22+
e = switch ("a long string that must wrap") {};
23+
<<<
24+
e = switch (
25+
"a long string that must wrap") {};
26+
>>>
27+
e = switch ([1,]) {};
28+
<<<
29+
e = switch ([
30+
1,
31+
]) {};

test/whitespace/switch.stmt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ switch (obj) {
289289
case 1 is! int:
290290
body;
291291
}
292-
>>> empty switch expression (error but handle gracefully)
292+
>>> empty switch expression
293293
var x = switch(y) {};
294294
<<<
295-
var x = switch (y) { };
295+
var x = switch (y) {};

0 commit comments

Comments
 (0)