Skip to content

Commit

Permalink
Merge pull request #440 from clementdessoude/fix/space-before-comma-m…
Browse files Browse the repository at this point in the history
…ultiple-value-switch

fix: space before comma in multi-value switch expression case label
  • Loading branch information
clementdessoude authored Nov 27, 2020
2 parents 0980a79 + 9bf0bfc commit d2f8f14
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,21 @@ class BlocksAndStatementPrettierVisitor {
switchLabel(ctx) {
if (ctx.Case) {
const caseConstants = this.mapVisit(ctx.caseConstant);
return rejectAndConcat([
concat([ctx.Case[0], " "]),
rejectAndJoin(" ,", caseConstants)
]);

const commas = ctx.Comma
? ctx.Comma.map(elt => {
return concat([elt, line]);
})
: [];

return group(
indent(
rejectAndConcat([
concat([ctx.Case[0], " "]),
rejectAndJoinSeps(commas, caseConstants)
])
)
);
}

return concat([ctx.Default[0]]);
Expand Down
18 changes: 18 additions & 0 deletions packages/prettier-plugin-java/test/unit-test/switch/_input.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,22 @@ public Location getAdjacentLocation(Direction direction) {
return this;
}
}

public void multipleCaseConstants(TestEnum testEnum) {
switch (testEnum) {
case FOO -> System.out.println("Foo!");
case BAR, BAZ -> System.out.println("Not Foo!");
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!");

}
}

public void caseConstantsWithComments(TestEnum testEnum) {
switch (testEnum) {
case BAR /* foo */, BAZ -> System.out.println("Not Foo!");
case BAR /* foo */, /* bar */ BAZ -> System.out.println("Not Foo!");
case BAR, /* bar */ BAZ -> System.out.println("Not Foo!");

}
}
}
35 changes: 35 additions & 0 deletions packages/prettier-plugin-java/test/unit-test/switch/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,39 @@ public Location getAdjacentLocation(Direction direction) {
return this;
}
}

public void multipleCaseConstants(TestEnum testEnum) {
switch (testEnum) {
case FOO -> System.out.println("Foo!");
case BAR, BAZ -> System.out.println("Not Foo!");
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!");
}
}

public void caseConstantsWithComments(TestEnum testEnum) {
switch (testEnum) {
case BAR/* foo */, BAZ -> System.out.println("Not Foo!");
case BAR/* foo */, /* bar */BAZ -> System.out.println("Not Foo!");
case BAR, /* bar */BAZ -> System.out.println("Not Foo!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ enum Day {

public int calculate(Day d) {
switch (d) {
case SATURDAY ,SUNDAY -> d.ordinal();
case SATURDAY, SUNDAY -> d.ordinal();
default -> {
int len = d.toString().length();
yield len * len;
Expand All @@ -23,7 +23,7 @@ public int calculate(Day d) {

public int calculate(Day d) {
return switch (d) {
case SATURDAY ,SUNDAY -> d.ordinal();
case SATURDAY, SUNDAY -> d.ordinal();
default -> {
int len = d.toString().length();
yield len * len;
Expand Down

0 comments on commit d2f8f14

Please sign in to comment.