Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/printers/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,15 +705,12 @@ export default {
},

guard(path, print) {
return [
"when ",
group([
ifBreak("("),
indent([softline, path.call(print, "namedChildren", 0)]),
softline,
ifBreak(")")
])
];
// Break after `when` without wrapping the guard expression in parentheses
// (issue #1001). A space is used when the group fits on one line.
return group([
"when",
indent([line, path.call(print, "namedChildren", 0)])
]);
}
} satisfies Partial<NamedNodePrinters>;

Expand Down
5 changes: 2 additions & 3 deletions test/unit-test/expressions/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,8 @@ void parenthesesWithLeadingAndTrailingBreak() {
)[ffffffffff];

switch (a) {
case Bbbbbbbbbb bbbbbbbbbb when (
cccccccccc && dddddddddd && eeeeeeeeee
) -> ffffffffff;
case Bbbbbbbbbb bbbbbbbbbb when
cccccccccc && dddddddddd && eeeeeeeeee -> ffffffffff;
}

return (
Expand Down
35 changes: 14 additions & 21 deletions test/unit-test/pattern-matching/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,27 @@ void test(Buyer other) {
case Buyer b when this.bestPrice > b.bestPrice -> {
return true;
}
case Buyer titi when (
this.bestPriceaaaaaaaazzzzzaaaaaaaaaq > b.bestPrice
) -> true;
case Buyer titi when (
this.bestPriceaaaaaazzzaaaaaaaaaq > b.bestPrice
) -> true;
case Buyer b when (
case Buyer titi when
this.bestPriceaaaaaaaazzzzzaaaaaaaaaq > b.bestPrice -> true;
case Buyer titi when
this.bestPriceaaaaaazzzaaaaaaaaaq > b.bestPrice -> true;
case Buyer b when
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice
) -> true;
case Buyer b when (
this.bestPrice > b.bestPrice -> true;
case Buyer b when
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice
) -> {
this.bestPrice > b.bestPrice -> {
return true;
}
case Buyer b when (
case Buyer b when
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice
) -> {
this.bestPrice > b.bestPrice -> {
return true;
}
default -> false;
Expand Down Expand Up @@ -94,17 +89,15 @@ case MyRecord(
case MyRecord(
LongTypeName longVariableName,
LongTypeName longVariableName
) when (
) when
this.longVariableName > longVariableName &&
this.longVariableName > longVariableName
) -> 0;
this.longVariableName > longVariableName -> 0;
case MyRecord(
LongTypeName longVariableName,
LongTypeName longVariableName
) when (
) when
this.longVariableName > longVariableName &&
this.longVariableName > longVariableName
) -> longMethodName(
this.longVariableName > longVariableName -> longMethodName(
longVariableName,
longVariableName,
longVariableName,
Expand Down
3 changes: 3 additions & 0 deletions test/unit-test/switch-when-guard-parens/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"printWidth": 40
}
8 changes: 8 additions & 0 deletions test/unit-test/switch-when-guard-parens/_input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class T {
boolean f(Object x) {
return switch (x) {
case A a when a.equals(a.aaaa) -> true;
default -> false;
};
}
}
9 changes: 9 additions & 0 deletions test/unit-test/switch-when-guard-parens/_output.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class T {
boolean f(Object x) {
return switch (x) {
case A a when
a.equals(a.aaaa) -> true;
default -> false;
};
}
}
5 changes: 2 additions & 3 deletions test/unit-test/unnamed-variables-and-patterns/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,9 @@ int wrappingMultipleSwitchPatterns() {
case
MyRecord(LongTypeName longVariableName, LongTypeName longVariableName),
MyRecord(LongTypeName longVariableName, LongTypeName longVariableName)
when (
when
this.longVariableName > longVariableName &&
this.longVariableName > longVariableName
) -> longMethodName(
this.longVariableName > longVariableName -> longMethodName(
longVariableName,
longVariableName,
longVariableName,
Expand Down