From d0ff4d1dedd6dc44db2a0f10ad218d8d9daa4f57 Mon Sep 17 00:00:00 2001 From: Jordan Kiesel Date: Mon, 8 Jan 2024 23:28:01 -0700 Subject: [PATCH] fix: break between non-block if/else statements --- .../src/printers/blocks-and-statements.ts | 4 +++- .../unit-test/empty_statement/_output.java | 18 ++++++++++++------ .../test/unit-test/sealed/_output.java | 8 +++----- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/packages/prettier-plugin-java/src/printers/blocks-and-statements.ts b/packages/prettier-plugin-java/src/printers/blocks-and-statements.ts index 747fceb3..c1da9ef1 100644 --- a/packages/prettier-plugin-java/src/printers/blocks-and-statements.ts +++ b/packages/prettier-plugin-java/src/printers/blocks-and-statements.ts @@ -205,7 +205,9 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter { const elseOnSameLine = hasTrailingLineComments(ctx.statement[0]) || - hasLeadingLineComments(ctx.Else[0]) + hasLeadingLineComments(ctx.Else[0]) || + !ctx.statement[0].children.statementWithoutTrailingSubstatement?.[0] + .children.block ? hardline : " "; diff --git a/packages/prettier-plugin-java/test/unit-test/empty_statement/_output.java b/packages/prettier-plugin-java/test/unit-test/empty_statement/_output.java index f1b81fd0..ccfa8b38 100644 --- a/packages/prettier-plugin-java/test/unit-test/empty_statement/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/empty_statement/_output.java @@ -37,7 +37,8 @@ public void forEachWithEmptyStatement(List list) { } public void ifElseWithEmptyStatements() { - if (test); else { + if (test); + else { System.out.println("one"); } @@ -45,15 +46,18 @@ public void ifElseWithEmptyStatements() { System.out.println("two"); } else; - if (test); else; + if (test); + else; } public void ifElseWithEmptyStatementsWithComments() { - if (test) /*test*/; else { + if (test) /*test*/; + else { System.out.println("one"); } - if (test); /*test*/else { + if (test); + /*test*/else { System.out.println("one"); } @@ -65,9 +69,11 @@ public void ifElseWithEmptyStatementsWithComments() { System.out.println("two"); } else;/*test*/ - if (test); /*test*/else;/*test*/ + if (test); + /*test*/else;/*test*/ - if (test) /*test*/; else /*test*/; + if (test) /*test*/; + else /*test*/; } public void simpleWhileWithEmptyStatement(boolean one) { diff --git a/packages/prettier-plugin-java/test/unit-test/sealed/_output.java b/packages/prettier-plugin-java/test/unit-test/sealed/_output.java index c0d51562..38f9ac3d 100644 --- a/packages/prettier-plugin-java/test/unit-test/sealed/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/sealed/_output.java @@ -46,11 +46,9 @@ default Shape rotate(double angle) { } default String areaMessage() { - if (this instanceof Circle) return "Circle: " + area(); else if ( - this instanceof Rectangle - ) return "Rectangle: " + area(); else if ( - this instanceof RightTriangle - ) return "Triangle: " + area(); + if (this instanceof Circle) return "Circle: " + area(); + else if (this instanceof Rectangle) return "Rectangle: " + area(); + else if (this instanceof RightTriangle) return "Triangle: " + area(); // :( throw new IllegalArgumentException(); }