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(); }