Skip to content

Commit 4d145ba

Browse files
committed
Fix else-if test structure and improve formatting stability.
- Update else-if test to use testSampleWithOptions pattern - Rename test files to follow expected naming convention - Fix empty block formatting in next-line brace style - Remove extra hardline in printBlock for empty blocks - Improve instance initializer formatting consistency
1 parent 4a8fbbb commit 4d145ba

7 files changed

Lines changed: 41 additions & 160 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,9 @@ export default {
302302

303303
exceptionType: printSingle,
304304
methodBody: printSingle,
305-
instanceInitializer: printSingle,
305+
instanceInitializer(path, print) {
306+
return call(path, print, "block");
307+
},
306308

307309
staticInitializer(path, print, options) {
308310
return formatWithBraces("static", call(path, print, "block"), options);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ export function printBlock(
286286
hardline,
287287
"}"
288288
]
289-
: [hardline, "{", hardline, "}"];
289+
: [hardline, "{", "}"];
290290
}
291291
return danglingComments.length
292292
? ["{", indent([hardline, ...danglingComments]), hardline, "}"]

packages/prettier-plugin-java/test/unit-test/braces-next-line/_output.java

Lines changed: 30 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -282,114 +282,93 @@ public void synchronizedMethod()
282282
class HelperMethods
283283
{
284284
void riskyOperation() throws Exception
285-
{
286-
}
285+
{}
287286

288287
void handleError(Exception e)
289-
{
290-
}
288+
{}
291289

292290
void handleIOError(IOException e)
293-
{
294-
}
291+
{}
295292

296293
void handleSQLError(SQLException e)
297-
{
298-
}
294+
{}
299295

300296
void cleanup()
301-
{
302-
}
297+
{}
303298

304299
void readFile(FileInputStream fis)
305-
{
306-
}
300+
{}
307301

308302
void processString(String s)
309-
{
310-
}
303+
{}
311304
}
312305

313306
// Test empty blocks for all constructs
314307
class EmptyBlocksTest
315308
{
316309
// Empty class declaration
317310
static class EmptyClass
318-
{
319-
}
311+
{}
320312

321313
// Empty interface declaration
322314
interface EmptyInterface
323-
{
324-
}
315+
{}
325316

326317
// Empty enum
327318
enum EmptyEnum
328-
{
329-
}
319+
{}
330320

331321
// Empty method
332322
void emptyMethod()
333-
{
334-
}
323+
{}
335324

336325
// Empty constructor
337326
EmptyBlocksTest()
338-
{
339-
}
327+
{}
340328

341329
// Empty static initializer
342330
static
343-
{
344-
}
331+
{}
345332

346333
// Empty instance initializer
347334

348-
{
349-
}
335+
{}
350336

351337
// Empty blocks in statements
352338
void testEmptyStatementBlocks()
353339
{
354340
// Empty if
355341
if (true)
356-
{
357-
}
342+
{}
358343

359344
// Empty else
360345
if (false)
361346
{
362347
System.out.println("not empty");
363348
}
364349
else
365-
{
366-
}
350+
{}
367351

368352
// Empty while
369353
while (false)
370-
{
371-
}
354+
{}
372355

373356
// Empty do-while
374357
do
375-
{
376-
}
358+
{}
377359
while (false);
378360

379361
// Empty for
380362
for (int i = 0; i < 0; i++)
381-
{
382-
}
363+
{}
383364

384365
// Empty enhanced for
385366
for (String s : new String[0])
386-
{
387-
}
367+
{}
388368

389369
// Empty try
390370
try
391-
{
392-
}
371+
{}
393372
catch (Exception e)
394373
{
395374
e.printStackTrace();
@@ -401,50 +380,41 @@ void testEmptyStatementBlocks()
401380
throw new Exception();
402381
}
403382
catch (Exception e)
404-
{
405-
}
383+
{}
406384

407385
// Empty finally
408386
try
409387
{
410388
System.out.println("try");
411389
}
412390
finally
413-
{
414-
}
391+
{}
415392

416393
// Empty try-catch-finally
417394
try
418-
{
419-
}
395+
{}
420396
catch (Exception e)
421-
{
422-
}
397+
{}
423398
finally
424-
{
425-
}
399+
{}
426400

427401
// Empty synchronized
428402
synchronized (this)
429-
{
430-
}
403+
{}
431404

432405
// Empty switch
433406
switch (1)
434-
{
435-
}
407+
{}
436408

437409
// Empty anonymous class
438410
Runnable r = new Runnable()
439411
{
440412
public void run()
441-
{
442-
}
413+
{}
443414
};
444415

445416
// Empty lambda block
446417
Runnable lambda = () ->
447-
{
448-
};
418+
{};
449419
}
450420
}

packages/prettier-plugin-java/test/unit-test/else-if/else-if-input.java renamed to packages/prettier-plugin-java/test/unit-test/else-if/_input.java

File renamed without changes.

packages/prettier-plugin-java/test/unit-test/else-if/else-if-output.java renamed to packages/prettier-plugin-java/test/unit-test/else-if/_output.java

File renamed without changes.

packages/prettier-plugin-java/test/unit-test/else-if/else-if-output-next-line.java

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
import { testSample } from "../../test-utils.js";
1+
import path from "path";
2+
import url from "url";
3+
import { testSampleWithOptions } from "../../test-utils.js";
24

3-
describe("else-if formatting", () => {
4-
// With default braceStyle (same-line)
5-
testSample(__dirname, "else-if-input", {
6-
prettierOptions: {
7-
braceStyle: "same-line",
8-
},
9-
});
5+
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
106

11-
// With next-line braceStyle
12-
testSample(__dirname, "else-if-input", {
13-
prettierOptions: {
14-
braceStyle: "next-line",
15-
},
16-
expectedSampleSuffix: "-next-line",
7+
describe("else-if formatting", () => {
8+
testSampleWithOptions({
9+
testFolder: __dirname,
1710
});
1811
});

0 commit comments

Comments
 (0)