From dde248b3ba14e9593b2d38df885497dcba66118a Mon Sep 17 00:00:00 2001 From: milan-oscard-8561 Date: Sat, 8 Aug 2026 14:56:48 +0200 Subject: [PATCH 1/7] fix(printer): don't wrap broken when-guards in parentheses Signed-off-by: milan-oscard-8561 --- src/printers/expressions.ts | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/printers/expressions.ts b/src/printers/expressions.ts index 8380d7e1..b434c53f 100644 --- a/src/printers/expressions.ts +++ b/src/printers/expressions.ts @@ -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; From 39822985dc65de91409f6f7e9915f708dfc5514e Mon Sep 17 00:00:00 2001 From: milan-oscard-8561 Date: Sat, 8 Aug 2026 14:56:49 +0200 Subject: [PATCH 2/7] fix(printer): don't wrap broken when-guards in parentheses Signed-off-by: milan-oscard-8561 --- test/unit-test/unnamed-variables-and-patterns/_output.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/unit-test/unnamed-variables-and-patterns/_output.java b/test/unit-test/unnamed-variables-and-patterns/_output.java index 7d1a28af..477b9e13 100644 --- a/test/unit-test/unnamed-variables-and-patterns/_output.java +++ b/test/unit-test/unnamed-variables-and-patterns/_output.java @@ -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, From 728b40513cc1174f1266f5c8ec23a33c7d85d276 Mon Sep 17 00:00:00 2001 From: milan-oscard-8561 Date: Sat, 8 Aug 2026 14:56:51 +0200 Subject: [PATCH 3/7] fix(printer): don't wrap broken when-guards in parentheses Signed-off-by: milan-oscard-8561 --- test/unit-test/switch-when-guard-parens/.prettierrc.json | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 test/unit-test/switch-when-guard-parens/.prettierrc.json diff --git a/test/unit-test/switch-when-guard-parens/.prettierrc.json b/test/unit-test/switch-when-guard-parens/.prettierrc.json new file mode 100644 index 00000000..be9cb124 --- /dev/null +++ b/test/unit-test/switch-when-guard-parens/.prettierrc.json @@ -0,0 +1,3 @@ +{ + "printWidth": 40 +} From bcdb1e7e6234e10a81a45dd4a190ed549aea6356 Mon Sep 17 00:00:00 2001 From: milan-oscard-8561 Date: Sat, 8 Aug 2026 14:56:53 +0200 Subject: [PATCH 4/7] fix(printer): don't wrap broken when-guards in parentheses Signed-off-by: milan-oscard-8561 --- test/unit-test/switch-when-guard-parens/_input.java | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 test/unit-test/switch-when-guard-parens/_input.java diff --git a/test/unit-test/switch-when-guard-parens/_input.java b/test/unit-test/switch-when-guard-parens/_input.java new file mode 100644 index 00000000..cadee9b1 --- /dev/null +++ b/test/unit-test/switch-when-guard-parens/_input.java @@ -0,0 +1,8 @@ +class T { + boolean f(Object x) { + return switch (x) { + case A a when a.equals(a.aaaa) -> true; + default -> false; + }; + } +} From 461644058fa9c000ad2938296e0ad091aa62f7db Mon Sep 17 00:00:00 2001 From: milan-oscard-8561 Date: Sat, 8 Aug 2026 14:56:55 +0200 Subject: [PATCH 5/7] fix(printer): don't wrap broken when-guards in parentheses Signed-off-by: milan-oscard-8561 --- test/unit-test/switch-when-guard-parens/_output.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 test/unit-test/switch-when-guard-parens/_output.java diff --git a/test/unit-test/switch-when-guard-parens/_output.java b/test/unit-test/switch-when-guard-parens/_output.java new file mode 100644 index 00000000..047545e9 --- /dev/null +++ b/test/unit-test/switch-when-guard-parens/_output.java @@ -0,0 +1,9 @@ +class T { + boolean f(Object x) { + return switch (x) { + case A a when + a.equals(a.aaaa) -> true; + default -> false; + }; + } +} From 736f0ee35113012426caaa3f0072818c925ac3ce Mon Sep 17 00:00:00 2001 From: braxtonzhangio Date: Thu, 13 Aug 2026 06:44:01 +0200 Subject: [PATCH 6/7] test: update when-guard snapshots for #1001 Signed-off-by: braxtonzhangio --- test/unit-test/expressions/_output.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/unit-test/expressions/_output.java b/test/unit-test/expressions/_output.java index 9e0f357d..997300c9 100644 --- a/test/unit-test/expressions/_output.java +++ b/test/unit-test/expressions/_output.java @@ -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 ( From 920ad36d66ce3508c811c5830b9b9b18f7db0b94 Mon Sep 17 00:00:00 2001 From: braxtonzhangio Date: Thu, 13 Aug 2026 06:44:03 +0200 Subject: [PATCH 7/7] test: update when-guard snapshots for #1001 Signed-off-by: braxtonzhangio --- test/unit-test/pattern-matching/_output.java | 35 ++++++++------------ 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/test/unit-test/pattern-matching/_output.java b/test/unit-test/pattern-matching/_output.java index a3190a4f..2c18eca3 100644 --- a/test/unit-test/pattern-matching/_output.java +++ b/test/unit-test/pattern-matching/_output.java @@ -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; @@ -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,