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
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
Loading