Skip to content

Commit b2ceb47

Browse files
committed
fix: no break in single-block switch case
1 parent 983a09d commit b2ceb47

File tree

3 files changed

+111
-2
lines changed

3 files changed

+111
-2
lines changed

packages/prettier-plugin-java/src/printers/blocks-and-statements.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,20 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter {
265265

266266
switchBlockStatementGroup(ctx: SwitchBlockStatementGroupCtx) {
267267
const switchLabel = this.visit(ctx.switchLabel);
268-
269268
const blockStatements = this.visit(ctx.blockStatements);
270269

270+
const statements = ctx.blockStatements?.[0].children.blockStatement;
271+
const hasSingleStatementBlock =
272+
statements?.length === 1 &&
273+
statements[0].children.statement?.[0].children
274+
.statementWithoutTrailingSubstatement?.[0].children.block !== undefined;
275+
271276
return concat([
272277
switchLabel,
273278
ctx.Colon[0],
274-
blockStatements && indent([hardline, blockStatements])
279+
hasSingleStatementBlock
280+
? concat([" ", blockStatements])
281+
: blockStatements && indent([hardline, blockStatements])
275282
]);
276283
}
277284

packages/prettier-plugin-java/test/unit-test/switch/_input.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,41 @@ public String shouldWrapEvenForSmallSwitchCases() {
4848
switch (answer) { case "YES": return "YES"; default: return "NO"; }
4949
}
5050

51+
void switchCaseWithBlock1() {
52+
switch (a) {
53+
case 0: {}
54+
default: {}
55+
}
56+
}
57+
58+
void switchCaseWithBlock2() {
59+
switch (a) {
60+
case 0: { b(); }
61+
default: { c(); }
62+
}
63+
}
64+
65+
void switchCaseWithBlock3() {
66+
switch (a) {
67+
case 0: { b(); } { c(); }
68+
default: { d(); } { e(); }
69+
}
70+
}
71+
72+
void switchCaseWithBlock4() {
73+
switch (a) {
74+
case 0: b(); { c(); }
75+
default: d(); { e(); }
76+
}
77+
}
78+
79+
void switchCaseWithBlock5() {
80+
switch (a) {
81+
case 0: { b(); } c();
82+
default: { d(); } e();
83+
}
84+
}
85+
5186
// Switch rules
5287
static void howManyAgain(int k) {
5388
switch (k) {

packages/prettier-plugin-java/test/unit-test/switch/_output.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,73 @@ public String shouldWrapEvenForSmallSwitchCases() {
5353
}
5454
}
5555

56+
void switchCaseWithBlock1() {
57+
switch (a) {
58+
case 0: {}
59+
default: {}
60+
}
61+
}
62+
63+
void switchCaseWithBlock2() {
64+
switch (a) {
65+
case 0: {
66+
b();
67+
}
68+
default: {
69+
c();
70+
}
71+
}
72+
}
73+
74+
void switchCaseWithBlock3() {
75+
switch (a) {
76+
case 0:
77+
{
78+
b();
79+
}
80+
{
81+
c();
82+
}
83+
default:
84+
{
85+
d();
86+
}
87+
{
88+
e();
89+
}
90+
}
91+
}
92+
93+
void switchCaseWithBlock4() {
94+
switch (a) {
95+
case 0:
96+
b();
97+
{
98+
c();
99+
}
100+
default:
101+
d();
102+
{
103+
e();
104+
}
105+
}
106+
}
107+
108+
void switchCaseWithBlock5() {
109+
switch (a) {
110+
case 0:
111+
{
112+
b();
113+
}
114+
c();
115+
default:
116+
{
117+
d();
118+
}
119+
e();
120+
}
121+
}
122+
56123
// Switch rules
57124
static void howManyAgain(int k) {
58125
switch (k) {

0 commit comments

Comments
 (0)