Skip to content

Commit 7e8f79e

Browse files
authored
Keep braces inline on empty classes and functions (#2336)
* Keep braces inline on empty classes and functions * Handle anonymous classes and static functions * Update snapshots
1 parent 46b7ba9 commit 7e8f79e

File tree

20 files changed

+371
-1007
lines changed

20 files changed

+371
-1007
lines changed

src/printer.mjs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,25 +1297,26 @@ function printClass(path, options, print) {
12971297
}
12981298
}
12991299

1300+
const hasEmptyClassBody =
1301+
node.body && node.body.length === 0 && !hasDanglingComments(node);
1302+
13001303
const printedDeclaration = group([
13011304
group(declaration),
1302-
shouldPrintHardlineForOpenBrace(options)
1305+
shouldPrintHardlineForOpenBrace(options) && !hasEmptyClassBody
13031306
? isAnonymousClass
13041307
? line
13051308
: hardline
13061309
: " ",
13071310
]);
13081311

1309-
const hasEmptyClassBody =
1310-
node.body && node.body.length === 0 && !hasDanglingComments(node);
13111312
const printedBody = [
13121313
"{",
13131314
indent([
13141315
hasEmptyClassBody ? "" : hardline,
13151316
printStatements(path, options, print, "body"),
13161317
]),
13171318
printDanglingComments(path, options, true),
1318-
isAnonymousClass && hasEmptyClassBody ? softline : hardline,
1319+
hasEmptyClassBody ? "" : hardline,
13191320
"}",
13201321
];
13211322

@@ -1383,14 +1384,14 @@ function printFunction(path, options, print) {
13831384
return [...declAttrs, printedDeclaration];
13841385
}
13851386

1386-
const isClosure = node.kind === "closure";
13871387
const printedBody = [
13881388
"{",
13891389
indent([hasEmptyBody(path) ? "" : hardline, print("body")]),
1390-
isClosure && hasEmptyBody(path) ? "" : hardline,
1390+
hasEmptyBody(path) ? "" : hardline,
13911391
"}",
13921392
];
13931393

1394+
const isClosure = node.kind === "closure";
13941395
if (isClosure) {
13951396
return [...declAttrs, printedDeclaration, " ", printedBody];
13961397
}
@@ -1399,7 +1400,9 @@ function printFunction(path, options, print) {
13991400
return [
14001401
...declAttrs,
14011402
printedDeclaration,
1402-
shouldPrintHardlineForOpenBrace(options) ? hardline : " ",
1403+
shouldPrintHardlineForOpenBrace(options) && !hasEmptyBody(path)
1404+
? hardline
1405+
: " ",
14031406
printedBody,
14041407
];
14051408
}
@@ -1415,7 +1418,9 @@ function printFunction(path, options, print) {
14151418
conditionalGroup([
14161419
[
14171420
printedDeclaration,
1418-
shouldPrintHardlineForOpenBrace(options) ? hardline : " ",
1421+
shouldPrintHardlineForOpenBrace(options) && !hasEmptyBody(path)
1422+
? hardline
1423+
: " ",
14191424
printedBody,
14201425
],
14211426
[printedDeclaration, " ", printedBody],

tests/attributes/__snapshots__/jsfmt.spec.mjs.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,7 @@ final class DomainEventMessage
181181
}
182182
183183
#[Attr1(Attr1::FOO | Attr1::BAR), Attr2(-20 * 5 + 10)]
184-
class A
185-
{
186-
}
184+
class A {}
187185
188186
class ValueModel
189187
{

0 commit comments

Comments
 (0)