Skip to content

Commit ffac8cd

Browse files
committed
fix: indent ternary binary expressions in return statements
1 parent 93e174b commit ffac8cd

File tree

5 files changed

+116
-2
lines changed

5 files changed

+116
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ export default {
126126
if (!path.node.children.QuestionMark) {
127127
return isInParentheses ? binaryExpression : group(binaryExpression);
128128
}
129+
const isInReturn = grandparentNodeName === "returnStatement";
130+
const prefix = group(
131+
isInReturn ? indent(binaryExpression) : binaryExpression
132+
);
129133
const [consequent, alternate] = map(path, print, "expression");
130134
const suffix = [
131135
line,
@@ -139,9 +143,9 @@ export default {
139143
? suffix
140144
: align(Math.max(0, options.tabWidth - 2), suffix);
141145
if (isNestedTernary) {
142-
return [group(binaryExpression), alignedSuffix];
146+
return [prefix, alignedSuffix];
143147
}
144-
const parts = [group(binaryExpression), indent(alignedSuffix)];
148+
const parts = [prefix, indent(alignedSuffix)];
145149
return isInParentheses ? parts : group(parts);
146150
},
147151

packages/prettier-plugin-java/test/unit-test/conditional-expression/spaces/_input.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ int ternaryOperationThatShouldBreak2() {
1010
return thisIsAVeryLongInteger ? thisIsAnotherVeryLongOne : thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne;
1111
}
1212

13+
void ternaryOperationThatShouldBreak3() {
14+
aaaaaaaaaa && bbbbbbbbbb && cccccccccc && dddddddddd && eeeeeeeeee && ffffffffff ? gggggggggg : hhhhhhhhhh;
15+
var v = aaaaaaaaaa && bbbbbbbbbb && cccccccccc && dddddddddd && eeeeeeeeee && ffffffffff ? gggggggggg : hhhhhhhhhh;
16+
v = aaaaaaaaaa && bbbbbbbbbb && cccccccccc && dddddddddd && eeeeeeeeee && ffffffffff ? gggggggggg : hhhhhhhhhh;
17+
f(aaaaaaaaaa && bbbbbbbbbb && cccccccccc && dddddddddd && eeeeeeeeee && ffffffffff ? gggggggggg : hhhhhhhhhh);
18+
return aaaaaaaaaa && bbbbbbbbbb && cccccccccc && dddddddddd && eeeeeeeeee && ffffffffff ? gggggggggg : hhhhhhhhhh;
19+
}
20+
1321
int ternaryOperationThatShouldNotBreak() {
1422
int a = b ? b : c;
1523
return b ? b : c;

packages/prettier-plugin-java/test/unit-test/conditional-expression/spaces/_output.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,53 @@ int ternaryOperationThatShouldBreak2() {
1919
: thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne;
2020
}
2121

22+
void ternaryOperationThatShouldBreak3() {
23+
aaaaaaaaaa &&
24+
bbbbbbbbbb &&
25+
cccccccccc &&
26+
dddddddddd &&
27+
eeeeeeeeee &&
28+
ffffffffff
29+
? gggggggggg
30+
: hhhhhhhhhh;
31+
var v =
32+
aaaaaaaaaa &&
33+
bbbbbbbbbb &&
34+
cccccccccc &&
35+
dddddddddd &&
36+
eeeeeeeeee &&
37+
ffffffffff
38+
? gggggggggg
39+
: hhhhhhhhhh;
40+
v =
41+
aaaaaaaaaa &&
42+
bbbbbbbbbb &&
43+
cccccccccc &&
44+
dddddddddd &&
45+
eeeeeeeeee &&
46+
ffffffffff
47+
? gggggggggg
48+
: hhhhhhhhhh;
49+
f(
50+
aaaaaaaaaa &&
51+
bbbbbbbbbb &&
52+
cccccccccc &&
53+
dddddddddd &&
54+
eeeeeeeeee &&
55+
ffffffffff
56+
? gggggggggg
57+
: hhhhhhhhhh
58+
);
59+
return aaaaaaaaaa &&
60+
bbbbbbbbbb &&
61+
cccccccccc &&
62+
dddddddddd &&
63+
eeeeeeeeee &&
64+
ffffffffff
65+
? gggggggggg
66+
: hhhhhhhhhh;
67+
}
68+
2269
int ternaryOperationThatShouldNotBreak() {
2370
int a = b ? b : c;
2471
return b ? b : c;

packages/prettier-plugin-java/test/unit-test/conditional-expression/tabs/_input.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ int ternaryOperationThatShouldBreak2() {
1010
return thisIsAVeryLongInteger ? thisIsAnotherVeryLongOne : thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne;
1111
}
1212

13+
void ternaryOperationThatShouldBreak3() {
14+
aaaaaaaaaa && bbbbbbbbbb && cccccccccc && dddddddddd && eeeeeeeeee && ffffffffff ? gggggggggg : hhhhhhhhhh;
15+
var v = aaaaaaaaaa && bbbbbbbbbb && cccccccccc && dddddddddd && eeeeeeeeee && ffffffffff ? gggggggggg : hhhhhhhhhh;
16+
v = aaaaaaaaaa && bbbbbbbbbb && cccccccccc && dddddddddd && eeeeeeeeee && ffffffffff ? gggggggggg : hhhhhhhhhh;
17+
f(aaaaaaaaaa && bbbbbbbbbb && cccccccccc && dddddddddd && eeeeeeeeee && ffffffffff ? gggggggggg : hhhhhhhhhh);
18+
return aaaaaaaaaa && bbbbbbbbbb && cccccccccc && dddddddddd && eeeeeeeeee && ffffffffff ? gggggggggg : hhhhhhhhhh;
19+
}
20+
1321
int ternaryOperationThatShouldNotBreak() {
1422
int a = b ? b : c;
1523
return b ? b : c;

packages/prettier-plugin-java/test/unit-test/conditional-expression/tabs/_output.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,53 @@ int ternaryOperationThatShouldBreak2() {
1818
: thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne;
1919
}
2020

21+
void ternaryOperationThatShouldBreak3() {
22+
aaaaaaaaaa &&
23+
bbbbbbbbbb &&
24+
cccccccccc &&
25+
dddddddddd &&
26+
eeeeeeeeee &&
27+
ffffffffff
28+
? gggggggggg
29+
: hhhhhhhhhh;
30+
var v =
31+
aaaaaaaaaa &&
32+
bbbbbbbbbb &&
33+
cccccccccc &&
34+
dddddddddd &&
35+
eeeeeeeeee &&
36+
ffffffffff
37+
? gggggggggg
38+
: hhhhhhhhhh;
39+
v =
40+
aaaaaaaaaa &&
41+
bbbbbbbbbb &&
42+
cccccccccc &&
43+
dddddddddd &&
44+
eeeeeeeeee &&
45+
ffffffffff
46+
? gggggggggg
47+
: hhhhhhhhhh;
48+
f(
49+
aaaaaaaaaa &&
50+
bbbbbbbbbb &&
51+
cccccccccc &&
52+
dddddddddd &&
53+
eeeeeeeeee &&
54+
ffffffffff
55+
? gggggggggg
56+
: hhhhhhhhhh
57+
);
58+
return aaaaaaaaaa &&
59+
bbbbbbbbbb &&
60+
cccccccccc &&
61+
dddddddddd &&
62+
eeeeeeeeee &&
63+
ffffffffff
64+
? gggggggggg
65+
: hhhhhhhhhh;
66+
}
67+
2168
int ternaryOperationThatShouldNotBreak() {
2269
int a = b ? b : c;
2370
return b ? b : c;

0 commit comments

Comments
 (0)