Skip to content

Commit b7926d3

Browse files
committed
fix: break empty switch/rule, try/catch/finally, and if blocks
1 parent daefe4c commit b7926d3

File tree

11 files changed

+301
-47
lines changed

11 files changed

+301
-47
lines changed

packages/prettier-plugin-java/src/comments.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ export function handleLineComment(
9999
handleIfStatementComments,
100100
handleJumpStatementComments,
101101
handleLabeledStatementComments,
102-
handleNameComments
102+
handleNameComments,
103+
handleTryStatementComments
103104
].some(fn => fn(commentNode, options));
104105
}
105106

@@ -247,6 +248,37 @@ function handleNameComments(commentNode: JavaComment) {
247248
return false;
248249
}
249250

251+
function handleTryStatementComments(commentNode: JavaComment) {
252+
const { enclosingNode, followingNode } = commentNode;
253+
if (
254+
enclosingNode &&
255+
["catches", "tryStatement"].includes(enclosingNode.name) &&
256+
followingNode &&
257+
isNonTerminal(followingNode)
258+
) {
259+
const block = (
260+
followingNode.name === "catches"
261+
? followingNode.children.catchClause[0]
262+
: followingNode.name === "catchClause" ||
263+
followingNode.name === "finally"
264+
? followingNode
265+
: null
266+
)?.children.block[0];
267+
if (!block) {
268+
return false;
269+
}
270+
const blockStatement =
271+
block.children.blockStatements?.[0].children.blockStatement[0];
272+
if (blockStatement) {
273+
util.addLeadingComment(blockStatement, commentNode);
274+
} else {
275+
util.addDanglingComment(block, commentNode, undefined);
276+
}
277+
return true;
278+
}
279+
return false;
280+
}
281+
250282
function isBinaryOperator(node?: JavaNode) {
251283
return (
252284
node !== undefined &&

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

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -263,18 +263,52 @@ export function printArrayInitializer<
263263
}
264264

265265
export function printBlock(path: AstPath<JavaNonTerminal>, contents: Doc[]) {
266-
if (!contents.length) {
267-
const danglingComments = printDanglingComments(path);
268-
return danglingComments.length
269-
? ["{", indent([hardline, ...danglingComments]), hardline, "}"]
270-
: "{}";
266+
if (contents.length) {
267+
return group([
268+
"{",
269+
indent([hardline, ...join(hardline, contents)]),
270+
hardline,
271+
"}"
272+
]);
271273
}
272-
return group([
273-
"{",
274-
indent([hardline, ...join(hardline, contents)]),
275-
hardline,
276-
"}"
277-
]);
274+
const danglingComments = printDanglingComments(path);
275+
if (danglingComments.length) {
276+
return ["{", indent([hardline, ...danglingComments]), hardline, "}"];
277+
}
278+
const parent = path.grandparent;
279+
const grandparent = path.getNode(4);
280+
const greatGrandparent = path.getNode(6);
281+
return (grandparent?.name === "catches" &&
282+
grandparent.children.catchClause.length === 1 &&
283+
(greatGrandparent?.name === "tryStatement" ||
284+
greatGrandparent?.name === "tryWithResourcesStatement") &&
285+
!greatGrandparent.children.finally) ||
286+
(greatGrandparent &&
287+
[
288+
"basicForStatement",
289+
"doStatement",
290+
"enhancedForStatement",
291+
"whileStatement"
292+
].includes(greatGrandparent.name)) ||
293+
[
294+
"annotationInterfaceBody",
295+
"classBody",
296+
"constructorBody",
297+
"enumBody",
298+
"interfaceBody",
299+
"moduleDeclaration",
300+
"recordBody"
301+
].includes(path.node.name) ||
302+
(parent &&
303+
[
304+
"instanceInitializer",
305+
"lambdaBody",
306+
"methodBody",
307+
"staticInitializer",
308+
"synchronizedStatement"
309+
].includes(parent.name))
310+
? "{}"
311+
: ["{", hardline, "}"];
278312
}
279313

280314
export function printName(

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ void parentheses() {
170170
var sizeIndex = ((index - 1) >>> level) & MASK;
171171
var from = offset > left ? 0 : (left - offset) >> level;
172172
var to = (right - offset) >> (level + 1);
173-
if (rawIndex < 1 << (list._level + SHIFT)) {}
173+
if (rawIndex < 1 << (list._level + SHIFT)) {
174+
}
174175
var res = size < SIZE ? 0 : ((size - 1) >>> SHIFT) << SHIFT;
175176
sign = (1 - 2 * b[3]) >> 7;
176177
exponent = ((b[3] << 1) & 0xff) | (b[2] >> (7 - 127));
@@ -213,12 +214,14 @@ void parentheses() {
213214
(aaaaaaaaaa + bbbbbbbbbb == cccccccccc + dddddddddd &&
214215
eeeeeeeeee + ffffffffff == gggggggggg + hhhhhhhhhh) ||
215216
iiiiiiiiii
216-
) {}
217+
) {
218+
}
217219

218220
if (
219221
(((((a * b + c) << d < e == f) & g) ^ h) | i && j) ||
220222
(k && l | (m ^ (n & (o != p > q >> (r - s / t)))))
221-
) {}
223+
) {
224+
}
222225

223226
if (
224227
(aaaaaaaaaa + bbbbbbbbbb == cccccccccc + dddddddddd &&
@@ -227,7 +230,8 @@ void parentheses() {
227230
mmmmmmmmmm + nnnnnnnnnn == oooooooooo + pppppppppp) ||
228231
(qqqqqqqqqq + rrrrrrrrrr == ssssssssss + tttttttttt &&
229232
uuuuuuuuuu + vvvvvvvvvv == wwwwwwwwww + xxxxxxxxxxx)
230-
) {}
233+
) {
234+
}
231235
}
232236

233237
void instanceOf() {

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ void parentheses() {
163163
var sizeIndex = ((index - 1) >>> level) & MASK;
164164
var from = offset > left ? 0 : (left - offset) >> level;
165165
var to = (right - offset) >> (level + 1);
166-
if (rawIndex < 1 << (list._level + SHIFT)) {}
166+
if (rawIndex < 1 << (list._level + SHIFT)) {
167+
}
167168
var res = size < SIZE ? 0 : ((size - 1) >>> SHIFT) << SHIFT;
168169
sign = (1 - 2 * b[3]) >> 7;
169170
exponent = ((b[3] << 1) & 0xff) | (b[2] >> (7 - 127));
@@ -206,12 +207,14 @@ void parentheses() {
206207
(aaaaaaaaaa + bbbbbbbbbb == cccccccccc + dddddddddd
207208
&& eeeeeeeeee + ffffffffff == gggggggggg + hhhhhhhhhh)
208209
|| iiiiiiiiii
209-
) {}
210+
) {
211+
}
210212

211213
if (
212214
(((((a * b + c) << d < e == f) & g) ^ h) | i && j)
213215
|| (k && l | (m ^ (n & (o != p > q >> (r - s / t)))))
214-
) {}
216+
) {
217+
}
215218

216219
if (
217220
(aaaaaaaaaa + bbbbbbbbbb == cccccccccc + dddddddddd
@@ -220,7 +223,8 @@ void parentheses() {
220223
&& mmmmmmmmmm + nnnnnnnnnn == oooooooooo + pppppppppp)
221224
|| (qqqqqqqqqq + rrrrrrrrrr == ssssssssss + tttttttttt
222225
&& uuuuuuuuuu + vvvvvvvvvv == wwwwwwwwww + xxxxxxxxxxx)
223-
) {}
226+
) {
227+
}
224228
}
225229

226230
void instanceOf() {

packages/prettier-plugin-java/test/unit-test/comments/comments-blocks-and-statements/if-statement/_output.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@ void commentsIfLineComment() {
44
if (
55
// test
66
t
7-
) {}
7+
) {
8+
}
89

910
if (
1011
t // test
11-
) {}
12+
) {
13+
}
1214

13-
if (t) {} // test
15+
if (t) {
16+
} // test
1417

1518
if (
1619
// test
1720
t
18-
) {}
21+
) {
22+
}
1923

2024
if (
2125
true // comment
@@ -25,26 +29,38 @@ void commentsIfLineComment() {
2529
}
2630

2731
void commentsIfBlockComment() {
28-
if (/* test */ t) {}
32+
if (/* test */ t) {
33+
}
2934

30-
if (t /* test */) {}
35+
if (t /* test */) {
36+
}
3137

32-
if (t) /* test */ {}
38+
if (t) /* test */ {
39+
}
3340

34-
if (/* test */ t) {}
41+
if (/* test */ t) {
42+
}
3543
}
3644

3745
void commentsElseLineComment() {
38-
if (t) {}
46+
if (t) {
47+
}
3948
// test
40-
else {}
49+
else {
50+
}
4151

42-
if (t) {} else {} // test
52+
if (t) {
53+
} else {
54+
} // test
4355
}
4456

4557
void commentsElseBlockComment() {
46-
if (t) {} /* test */ else {}
58+
if (t) {
59+
} /* test */ else {
60+
}
4761

48-
if (t) {} else /* test */ {}
62+
if (t) {
63+
} else /* test */ {
64+
}
4965
}
5066
}

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ public void instanceOf() {
7373
}
7474

7575
public void printSimple() {
76-
if (myValue == 42) {}
76+
if (myValue == 42) {
77+
}
7778

7879
if (myValue != 42) {
7980
System.out.println("Why not 42 !");
@@ -92,33 +93,40 @@ public void printIf() {
9293
myValue == 42 ||
9394
(myValue == 42 && myValue == 42 && myValue == 42) ||
9495
(myValue == 42 && myValue == 42)
95-
) {}
96+
) {
97+
}
9698

9799
if (
98100
(myValue != 42 && 42 / 42) ||
99101
(myValue & 42 && myValue > 42) ||
100102
(myValue < 42 && myValue == 42)
101-
) {}
103+
) {
104+
}
102105

103-
if (myValue != 42 && myValue == 42) {}
106+
if (myValue != 42 && myValue == 42) {
107+
}
104108
}
105109

106110
public void printSwitch() {
107111
switch (
108112
myValue == 42 ||
109113
(myValue == 42 && myValue == 42 && myValue == 42) ||
110114
(myValue == 42 && myValue == 42)
111-
) {}
115+
) {
116+
}
112117

113118
switch (
114119
(myValue != 42 && 42 / 42) ||
115120
(myValue & 42 && myValue > 42) ||
116121
(myValue < 42 && myValue == 42)
117-
) {}
122+
) {
123+
}
118124

119-
switch (myValue != 42) {}
125+
switch (myValue != 42) {
126+
}
120127

121-
switch (myValue != 42 && myValue == 42) {}
128+
switch (myValue != 42 && myValue == 42) {
129+
}
122130
}
123131

124132
public void printWhile() {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,14 @@ void switchRulesWithComments() {
185185
throw new RuntimeException();
186186
}
187187
}
188+
189+
void emptyBlocks() {
190+
switch (a) {}
191+
switch (a) {
192+
case 1: {}
193+
}
194+
switch (a) {
195+
case 1 -> {}
196+
}
197+
}
188198
}

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ public String shouldWrapEvenForSmallSwitchCases() {
5555

5656
void switchCaseWithBlock1() {
5757
switch (a) {
58-
case 0: {}
59-
default: {}
58+
case 0: {
59+
}
60+
default: {
61+
}
6062
}
6163
}
6264

@@ -233,4 +235,17 @@ void switchRulesWithComments() {
233235
throw new RuntimeException();
234236
}
235237
}
238+
239+
void emptyBlocks() {
240+
switch (a) {
241+
}
242+
switch (a) {
243+
case 1: {
244+
}
245+
}
246+
switch (a) {
247+
case 1 -> {
248+
}
249+
}
250+
}
236251
}

0 commit comments

Comments
 (0)