Skip to content

Commit 983a09d

Browse files
jtkieselclementdessoude
authored andcommitted
fix: break between non-block if/else statements
1 parent 6f587d7 commit 983a09d

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

packages/prettier-plugin-java/src/printers/blocks-and-statements.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter {
203203

204204
const elseOnSameLine =
205205
hasTrailingLineComments(ctx.statement[0]) ||
206-
hasLeadingLineComments(ctx.Else[0])
206+
hasLeadingLineComments(ctx.Else[0]) ||
207+
!ctx.statement[0].children.statementWithoutTrailingSubstatement?.[0]
208+
.children.block
207209
? hardline
208210
: " ";
209211

packages/prettier-plugin-java/test/unit-test/empty_statement/_output.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,27 @@ public void forEachWithEmptyStatement(List<String> list) {
3737
}
3838

3939
public void ifElseWithEmptyStatements() {
40-
if (test); else {
40+
if (test);
41+
else {
4142
System.out.println("one");
4243
}
4344

4445
if (test) {
4546
System.out.println("two");
4647
} else;
4748

48-
if (test); else;
49+
if (test);
50+
else;
4951
}
5052

5153
public void ifElseWithEmptyStatementsWithComments() {
52-
if (test) /*test*/; else {
54+
if (test) /*test*/;
55+
else {
5356
System.out.println("one");
5457
}
5558

56-
if (test); /*test*/else {
59+
if (test);
60+
/*test*/else {
5761
System.out.println("one");
5862
}
5963

@@ -65,9 +69,11 @@ public void ifElseWithEmptyStatementsWithComments() {
6569
System.out.println("two");
6670
} else;/*test*/
6771

68-
if (test); /*test*/else;/*test*/
72+
if (test);
73+
/*test*/else;/*test*/
6974

70-
if (test) /*test*/; else /*test*/;
75+
if (test) /*test*/;
76+
else /*test*/;
7177
}
7278

7379
public void simpleWhileWithEmptyStatement(boolean one) {

packages/prettier-plugin-java/test/unit-test/sealed/_output.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,9 @@ default Shape rotate(double angle) {
4646
}
4747

4848
default String areaMessage() {
49-
if (this instanceof Circle) return "Circle: " + area(); else if (
50-
this instanceof Rectangle
51-
) return "Rectangle: " + area(); else if (
52-
this instanceof RightTriangle
53-
) return "Triangle: " + area();
49+
if (this instanceof Circle) return "Circle: " + area();
50+
else if (this instanceof Rectangle) return "Rectangle: " + area();
51+
else if (this instanceof RightTriangle) return "Triangle: " + area();
5452
// :(
5553
throw new IllegalArgumentException();
5654
}

0 commit comments

Comments
 (0)