Skip to content

Commit d2f8f14

Browse files
Merge pull request #440 from clementdessoude/fix/space-before-comma-multiple-value-switch
fix: space before comma in multi-value switch expression case label
2 parents 0980a79 + 9bf0bfc commit d2f8f14

File tree

4 files changed

+70
-6
lines changed

4 files changed

+70
-6
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,21 @@ class BlocksAndStatementPrettierVisitor {
229229
switchLabel(ctx) {
230230
if (ctx.Case) {
231231
const caseConstants = this.mapVisit(ctx.caseConstant);
232-
return rejectAndConcat([
233-
concat([ctx.Case[0], " "]),
234-
rejectAndJoin(" ,", caseConstants)
235-
]);
232+
233+
const commas = ctx.Comma
234+
? ctx.Comma.map(elt => {
235+
return concat([elt, line]);
236+
})
237+
: [];
238+
239+
return group(
240+
indent(
241+
rejectAndConcat([
242+
concat([ctx.Case[0], " "]),
243+
rejectAndJoinSeps(commas, caseConstants)
244+
])
245+
)
246+
);
236247
}
237248

238249
return concat([ctx.Default[0]]);

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,22 @@ public Location getAdjacentLocation(Direction direction) {
7575
return this;
7676
}
7777
}
78+
79+
public void multipleCaseConstants(TestEnum testEnum) {
80+
switch (testEnum) {
81+
case FOO -> System.out.println("Foo!");
82+
case BAR, BAZ -> System.out.println("Not Foo!");
83+
case BAR, BAZ, BAZ, BAZ, BAZ, BAZ, BAZ, BAZ, BAZ, BAZ, BAZ, BAZ, BAZ, BAZ, BAZ, BAZ, BAZ, BAZ, BAZ, BAZ -> System.out.println("Not Foo!");
84+
85+
}
86+
}
87+
88+
public void caseConstantsWithComments(TestEnum testEnum) {
89+
switch (testEnum) {
90+
case BAR /* foo */, BAZ -> System.out.println("Not Foo!");
91+
case BAR /* foo */, /* bar */ BAZ -> System.out.println("Not Foo!");
92+
case BAR, /* bar */ BAZ -> System.out.println("Not Foo!");
93+
94+
}
95+
}
7896
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,39 @@ public Location getAdjacentLocation(Direction direction) {
8181
return this;
8282
}
8383
}
84+
85+
public void multipleCaseConstants(TestEnum testEnum) {
86+
switch (testEnum) {
87+
case FOO -> System.out.println("Foo!");
88+
case BAR, BAZ -> System.out.println("Not Foo!");
89+
case BAR,
90+
BAZ,
91+
BAZ,
92+
BAZ,
93+
BAZ,
94+
BAZ,
95+
BAZ,
96+
BAZ,
97+
BAZ,
98+
BAZ,
99+
BAZ,
100+
BAZ,
101+
BAZ,
102+
BAZ,
103+
BAZ,
104+
BAZ,
105+
BAZ,
106+
BAZ,
107+
BAZ,
108+
BAZ -> System.out.println("Not Foo!");
109+
}
110+
}
111+
112+
public void caseConstantsWithComments(TestEnum testEnum) {
113+
switch (testEnum) {
114+
case BAR/* foo */, BAZ -> System.out.println("Not Foo!");
115+
case BAR/* foo */, /* bar */BAZ -> System.out.println("Not Foo!");
116+
case BAR, /* bar */BAZ -> System.out.println("Not Foo!");
117+
}
118+
}
84119
}

packages/prettier-plugin-java/test/unit-test/yield-statement/_output.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ enum Day {
1212

1313
public int calculate(Day d) {
1414
switch (d) {
15-
case SATURDAY ,SUNDAY -> d.ordinal();
15+
case SATURDAY, SUNDAY -> d.ordinal();
1616
default -> {
1717
int len = d.toString().length();
1818
yield len * len;
@@ -23,7 +23,7 @@ public int calculate(Day d) {
2323

2424
public int calculate(Day d) {
2525
return switch (d) {
26-
case SATURDAY ,SUNDAY -> d.ordinal();
26+
case SATURDAY, SUNDAY -> d.ordinal();
2727
default -> {
2828
int len = d.toString().length();
2929
yield len * len;

0 commit comments

Comments
 (0)