Skip to content

Commit b1b9e7c

Browse files
jtkieselclementdessoude
authored andcommitted
fix(printer): break and indent binary expression with cast properly
1 parent 20da8ea commit b1b9e7c

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

packages/prettier-plugin-java/src/printers/classes.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,17 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
345345
ctx.variableInitializer![0].children.expression![0].children
346346
.ternaryExpression !== undefined
347347
) {
348-
const firstPrimary =
348+
const unaryExpressions =
349349
ctx.variableInitializer![0].children.expression![0].children
350350
.ternaryExpression[0].children.binaryExpression[0].children
351-
.unaryExpression[0].children.primary[0];
351+
.unaryExpression;
352+
const firstPrimary = unaryExpressions[0].children.primary[0];
352353

353354
// Cast Expression
354355
if (
355356
firstPrimary.children.primaryPrefix[0].children.castExpression !==
356-
undefined
357+
undefined &&
358+
unaryExpressions.length === 1
357359
) {
358360
const groupId = Symbol("assignment");
359361
return group([

packages/prettier-plugin-java/test/unit-test/binary_expressions/_input.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,8 @@ public void method() {
5858
new Foo(stuff, thing, "auaaaaaaaaa some very long stuff", "some more").bar(10);
5959
foo(stuff, thing, "some very longuuuuuuuuuuuuuu stuff", "some more").bar(10);
6060
}
61+
62+
public void binaryExpressionWithCast() {
63+
double availability = (double) successfulCount / (successfulCount + failureCount);
64+
}
6165
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,9 @@ public void method() {
8282
foo(stuff, thing, "some very longuuuuuuuuuuuuuu stuff", "some more")
8383
.bar(10);
8484
}
85+
86+
public void binaryExpressionWithCast() {
87+
double availability =
88+
(double) successfulCount / (successfulCount + failureCount);
89+
}
8590
}

0 commit comments

Comments
 (0)