Skip to content

Commit 9f1817d

Browse files
committed
fix: break nested ternaries together
1 parent 675eb4d commit 9f1817d

File tree

5 files changed

+42
-11
lines changed

5 files changed

+42
-11
lines changed

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,21 @@ export default {
116116

117117
conditionalExpression(path, print) {
118118
const binaryExpression = call(path, print, "binaryExpression");
119+
if (!path.node.children.QuestionMark) {
120+
return binaryExpression;
121+
}
119122
const expressions = map(path, print, "expression");
120-
return path.node.children.QuestionMark
121-
? group(
122-
indent(
123-
join(line, [
124-
binaryExpression,
125-
["? ", expressions[0]],
126-
[": ", expressions[1]]
127-
])
128-
)
129-
)
130-
: binaryExpression;
123+
const contents = indent(
124+
join(line, [
125+
binaryExpression,
126+
["? ", expressions[0]],
127+
[": ", expressions[1]]
128+
])
129+
);
130+
const isNestedTernary =
131+
(path.getNode(4) as JavaNonTerminal | null)?.name ===
132+
"conditionalExpression";
133+
return isNestedTernary ? contents : group(contents);
131134
},
132135

133136
binaryExpression(path, print, options) {

packages/prettier-plugin-java/test/unit-test/binary_expressions/operator-position-end/_input.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public int ternaryOperationThatShouldNotBreak() {
3232
return b ? b : c;
3333
}
3434

35+
void nestedTernary() {
36+
aaaaaaaaaa ? bbbbbbbbbb : cccccccccc ? dddddddddd : eeeeeeeeee ? ffffffffff : gggggggggg;
37+
}
38+
3539
public boolean binaryOperationWithComments() {
3640
boolean a = one || two >> 1 // one
3741
// two

packages/prettier-plugin-java/test/unit-test/binary_expressions/operator-position-end/_output.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ public int ternaryOperationThatShouldNotBreak() {
4949
return b ? b : c;
5050
}
5151

52+
void nestedTernary() {
53+
aaaaaaaaaa
54+
? bbbbbbbbbb
55+
: cccccccccc
56+
? dddddddddd
57+
: eeeeeeeeee
58+
? ffffffffff
59+
: gggggggggg;
60+
}
61+
5262
public boolean binaryOperationWithComments() {
5363
boolean a =
5464
one ||

packages/prettier-plugin-java/test/unit-test/binary_expressions/operator-position-start/_input.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public int ternaryOperationThatShouldNotBreak() {
3232
return b ? b : c;
3333
}
3434

35+
void nestedTernary() {
36+
aaaaaaaaaa ? bbbbbbbbbb : cccccccccc ? dddddddddd : eeeeeeeeee ? ffffffffff : gggggggggg;
37+
}
38+
3539
public boolean binaryOperationWithComments() {
3640
boolean a = one || two >> 1 // one
3741
// two

packages/prettier-plugin-java/test/unit-test/binary_expressions/operator-position-start/_output.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ public int ternaryOperationThatShouldNotBreak() {
4949
return b ? b : c;
5050
}
5151

52+
void nestedTernary() {
53+
aaaaaaaaaa
54+
? bbbbbbbbbb
55+
: cccccccccc
56+
? dddddddddd
57+
: eeeeeeeeee
58+
? ffffffffff
59+
: gggggggggg;
60+
}
61+
5262
public boolean binaryOperationWithComments() {
5363
boolean a =
5464
one

0 commit comments

Comments
 (0)