@@ -1199,31 +1199,16 @@ function printAttrs(path, options, print, { inline = false } = {}) {
11991199 if ( ! path . node . attrGroups ) {
12001200 return [ ] ;
12011201 }
1202- path . each ( ( ) => {
1203- const attrGroup = [ "#[" ] ;
1202+ path . each ( ( attrGroupPath ) => {
12041203 if ( ! inline && allAttrs . length > 0 ) {
12051204 allAttrs . push ( hardline ) ;
12061205 }
1207- attrGroup . push ( softline ) ;
1208- path . each ( ( ) => {
1209- const attrNode = path . node ;
1210- if ( attrGroup . length > 2 ) {
1211- attrGroup . push ( "," , line ) ;
1212- }
1213- const attrStmt = [ attrNode . name ] ;
1214- if ( attrNode . args . length > 0 ) {
1215- attrStmt . push ( printArgumentsList ( path , options , print , "args" ) ) ;
1216- }
1217- attrGroup . push ( group ( attrStmt ) ) ;
1218- } , "attrs" ) ;
12191206 allAttrs . push (
1220- group ( [
1221- indent ( attrGroup ) ,
1222- ifBreak ( shouldPrintComma ( options , 8.0 ) ? "," : "" ) ,
1223- softline ,
1224- "]" ,
1225- inline ? ifBreak ( softline , " " ) : "" ,
1226- ] )
1207+ printAllComments (
1208+ attrGroupPath ,
1209+ ( ) => printAttrGroup ( attrGroupPath , options , print , { inline } ) ,
1210+ options
1211+ )
12271212 ) ;
12281213 } , "attrGroups" ) ;
12291214 if ( allAttrs . length === 0 ) {
@@ -1232,6 +1217,134 @@ function printAttrs(path, options, print, { inline = false } = {}) {
12321217 return [ ...allAttrs , inline ? "" : hardline ] ;
12331218}
12341219
1220+ function printAttrGroup ( path , options , print , { inline = false } = { } ) {
1221+ const attrGroup = [ "#[" ] ;
1222+ attrGroup . push ( softline ) ;
1223+ path . each ( ( ) => {
1224+ const attrNode = path . node ;
1225+ if ( attrGroup . length > 2 ) {
1226+ attrGroup . push ( "," , line ) ;
1227+ }
1228+ const attrStmt = [ attrNode . name ] ;
1229+ if ( attrNode . args . length > 0 ) {
1230+ attrStmt . push ( printArgumentsList ( path , options , print , "args" ) ) ;
1231+ }
1232+ attrGroup . push ( group ( attrStmt ) ) ;
1233+ } , "attrs" ) ;
1234+ return group ( [
1235+ indent ( attrGroup ) ,
1236+ ifBreak ( shouldPrintComma ( options , 8.0 ) ? "," : "" ) ,
1237+ softline ,
1238+ "]" ,
1239+ inline ? ifBreak ( softline , " " ) : "" ,
1240+ ] ) ;
1241+ }
1242+
1243+ function printFunction ( path , options , print ) {
1244+ const { node } = path ;
1245+ const declAttrs = printAttrs ( path , options , print , {
1246+ inline : node . kind === "closure" ,
1247+ } ) ;
1248+ const declaration = [ ] ;
1249+
1250+ if ( node . isFinal ) {
1251+ declaration . push ( "final " ) ;
1252+ }
1253+
1254+ if ( node . isAbstract ) {
1255+ declaration . push ( "abstract " ) ;
1256+ }
1257+
1258+ if ( node . visibility ) {
1259+ declaration . push ( node . visibility , " " ) ;
1260+ }
1261+
1262+ if ( node . isStatic ) {
1263+ declaration . push ( "static " ) ;
1264+ }
1265+
1266+ declaration . push ( "function " ) ;
1267+
1268+ if ( node . byref ) {
1269+ declaration . push ( "&" ) ;
1270+ }
1271+
1272+ if ( node . name ) {
1273+ declaration . push ( print ( "name" ) ) ;
1274+ }
1275+
1276+ declaration . push ( printArgumentsList ( path , options , print ) ) ;
1277+
1278+ if ( node . uses && node . uses . length > 0 ) {
1279+ declaration . push (
1280+ group ( [ " use " , printArgumentsList ( path , options , print , "uses" ) ] )
1281+ ) ;
1282+ }
1283+
1284+ if ( node . type ) {
1285+ declaration . push ( [
1286+ ": " ,
1287+ hasDanglingComments ( node . type )
1288+ ? [
1289+ path . call ( ( ) => printDanglingComments ( path , options , true ) , "type" ) ,
1290+ " " ,
1291+ ]
1292+ : "" ,
1293+ node . nullable ? "?" : "" ,
1294+ print ( "type" ) ,
1295+ ] ) ;
1296+ }
1297+
1298+ const printedDeclaration = declaration ;
1299+
1300+ if ( ! node . body ) {
1301+ return [ ...declAttrs , printedDeclaration ] ;
1302+ }
1303+
1304+ const printedBody = [
1305+ "{" ,
1306+ indent ( [ hasEmptyBody ( path ) ? "" : hardline , print ( "body" ) ] ) ,
1307+ hasEmptyBody ( path ) ? "" : hardline ,
1308+ "}" ,
1309+ ] ;
1310+
1311+ const isClosure = node . kind === "closure" ;
1312+ if ( isClosure ) {
1313+ return [ ...declAttrs , printedDeclaration , " " , printedBody ] ;
1314+ }
1315+
1316+ if ( node . arguments . length === 0 ) {
1317+ return [
1318+ ...declAttrs ,
1319+ printedDeclaration ,
1320+ shouldPrintHardlineForOpenBrace ( options ) && ! hasEmptyBody ( path )
1321+ ? hardline
1322+ : " " ,
1323+ printedBody ,
1324+ ] ;
1325+ }
1326+
1327+ const willBreakDeclaration = declaration . some ( willBreak ) ;
1328+
1329+ if ( willBreakDeclaration ) {
1330+ return [ ...declAttrs , printedDeclaration , " " , printedBody ] ;
1331+ }
1332+
1333+ return [
1334+ ...declAttrs ,
1335+ conditionalGroup ( [
1336+ [
1337+ printedDeclaration ,
1338+ shouldPrintHardlineForOpenBrace ( options ) && ! hasEmptyBody ( path )
1339+ ? hardline
1340+ : " " ,
1341+ printedBody ,
1342+ ] ,
1343+ [ printedDeclaration , " " , printedBody ] ,
1344+ ] ) ,
1345+ ] ;
1346+ }
1347+
12351348function printClass ( path , options , print ) {
12361349 const { node } = path ;
12371350 const isAnonymousClass = node . kind === "class" && node . isAnonymous ;
@@ -1353,111 +1466,6 @@ function printClass(path, options, print) {
13531466 return [ printedDeclaration , printedBody ] ;
13541467}
13551468
1356- function printFunction ( path , options , print ) {
1357- const { node } = path ;
1358- const declAttrs = printAttrs ( path , options , print , {
1359- inline : node . kind === "closure" ,
1360- } ) ;
1361- const declaration = [ ] ;
1362-
1363- if ( node . isFinal ) {
1364- declaration . push ( "final " ) ;
1365- }
1366-
1367- if ( node . isAbstract ) {
1368- declaration . push ( "abstract " ) ;
1369- }
1370-
1371- if ( node . visibility ) {
1372- declaration . push ( node . visibility , " " ) ;
1373- }
1374-
1375- if ( node . isStatic ) {
1376- declaration . push ( "static " ) ;
1377- }
1378-
1379- declaration . push ( "function " ) ;
1380-
1381- if ( node . byref ) {
1382- declaration . push ( "&" ) ;
1383- }
1384-
1385- if ( node . name ) {
1386- declaration . push ( print ( "name" ) ) ;
1387- }
1388-
1389- declaration . push ( printArgumentsList ( path , options , print ) ) ;
1390-
1391- if ( node . uses && node . uses . length > 0 ) {
1392- declaration . push (
1393- group ( [ " use " , printArgumentsList ( path , options , print , "uses" ) ] )
1394- ) ;
1395- }
1396-
1397- if ( node . type ) {
1398- declaration . push ( [
1399- ": " ,
1400- hasDanglingComments ( node . type )
1401- ? [
1402- path . call ( ( ) => printDanglingComments ( path , options , true ) , "type" ) ,
1403- " " ,
1404- ]
1405- : "" ,
1406- node . nullable ? "?" : "" ,
1407- print ( "type" ) ,
1408- ] ) ;
1409- }
1410-
1411- const printedDeclaration = declaration ;
1412-
1413- if ( ! node . body ) {
1414- return [ ...declAttrs , printedDeclaration ] ;
1415- }
1416-
1417- const printedBody = [
1418- "{" ,
1419- indent ( [ hasEmptyBody ( path ) ? "" : hardline , print ( "body" ) ] ) ,
1420- hasEmptyBody ( path ) ? "" : hardline ,
1421- "}" ,
1422- ] ;
1423-
1424- const isClosure = node . kind === "closure" ;
1425- if ( isClosure ) {
1426- return [ ...declAttrs , printedDeclaration , " " , printedBody ] ;
1427- }
1428-
1429- if ( node . arguments . length === 0 ) {
1430- return [
1431- ...declAttrs ,
1432- printedDeclaration ,
1433- shouldPrintHardlineForOpenBrace ( options ) && ! hasEmptyBody ( path )
1434- ? hardline
1435- : " " ,
1436- printedBody ,
1437- ] ;
1438- }
1439-
1440- const willBreakDeclaration = declaration . some ( willBreak ) ;
1441-
1442- if ( willBreakDeclaration ) {
1443- return [ ...declAttrs , printedDeclaration , " " , printedBody ] ;
1444- }
1445-
1446- return [
1447- ...declAttrs ,
1448- conditionalGroup ( [
1449- [
1450- printedDeclaration ,
1451- shouldPrintHardlineForOpenBrace ( options ) && ! hasEmptyBody ( path )
1452- ? hardline
1453- : " " ,
1454- printedBody ,
1455- ] ,
1456- [ printedDeclaration , " " , printedBody ] ,
1457- ] ) ,
1458- ] ;
1459- }
1460-
14611469function printBodyControlStructure (
14621470 path ,
14631471 options ,
@@ -2909,6 +2917,7 @@ function printNode(path, options, print) {
29092917
29102918 case "enumcase" :
29112919 return group ( [
2920+ ...printAttrs ( path , options , print ) ,
29122921 "case " ,
29132922 print ( "name" ) ,
29142923 node . value
0 commit comments