Skip to content

Commit 071a365

Browse files
committed
Update php-parser, fix unary snapshot tests, and make multiple fixes for attribute formatting.
1 parent 7ea62f3 commit 071a365

9 files changed

Lines changed: 260 additions & 142 deletions

File tree

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
],
2828
"dependencies": {
2929
"linguist-languages": "^8.0.0",
30-
"php-parser": "^3.4.0"
30+
"php-parser": "https://github.com/glayzzle/php-parser.git#0b3dbe8833965ae14915e722a1b974e3616df78c"
3131
},
3232
"devDependencies": {
3333
"@babel/preset-env": "^7.27.2",
@@ -82,5 +82,6 @@
8282
"include": [
8383
"src/**"
8484
]
85-
}
85+
},
86+
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
8687
}

src/comments.mjs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ function handleOwnLineComment(comment, text, options) {
6262
options
6363
) ||
6464
handleTryComments(enclosingNode, followingNode, comment) ||
65+
handleClassMemberStatementComments(enclosingNode, followingNode, comment) ||
6566
handleClassComments(enclosingNode, followingNode, comment) ||
6667
handleFunctionParameter(
6768
text,
@@ -201,6 +202,7 @@ function handleRemainingComment(comment, text, options) {
201202
handleGoto(enclosingNode, comment) ||
202203
handleHalt(precedingNode, enclosingNode, followingNode, comment) ||
203204
handleBreakAndContinueStatementComments(enclosingNode, comment) ||
205+
handleClassMemberStatementComments(enclosingNode, followingNode, comment) ||
204206
handleInlineComments(
205207
enclosingNode,
206208
precedingNode,
@@ -433,6 +435,32 @@ function handleTraitUseComments(enclosingNode, followingNode, comment) {
433435
return false;
434436
}
435437

438+
function handleClassMemberStatementComments(
439+
enclosingNode,
440+
followingNode,
441+
comment
442+
) {
443+
if (
444+
enclosingNode &&
445+
enclosingNode.kind === "propertystatement" &&
446+
enclosingNode.properties?.includes(followingNode)
447+
) {
448+
addLeadingComment(enclosingNode, comment);
449+
return true;
450+
}
451+
452+
if (
453+
enclosingNode &&
454+
enclosingNode.kind === "classconstant" &&
455+
enclosingNode.constants?.includes(followingNode)
456+
) {
457+
addLeadingComment(enclosingNode, comment);
458+
return true;
459+
}
460+
461+
return false;
462+
}
463+
436464
function handleClassComments(enclosingNode, followingNode, comment) {
437465
if (
438466
enclosingNode &&
@@ -907,6 +935,24 @@ function getCommentChildNodes(node) {
907935
node.what.__parent_new_arguments = [...node.arguments];
908936
return [node.what];
909937
}
938+
939+
if (node.attrGroups && node.attrGroups.length > 0) {
940+
if (node.kind === "method" || node.kind === "function") {
941+
return [
942+
...node.attrGroups,
943+
...node.arguments,
944+
...(node.type ? [node.type] : []),
945+
];
946+
}
947+
948+
if (node.kind === "classconstant") {
949+
return [...node.attrGroups, ...node.constants];
950+
}
951+
952+
if (node.kind === "enumcase") {
953+
return node.attrGroups;
954+
}
955+
}
910956
}
911957

912958
function canAttachComment(node) {

src/printer.mjs

Lines changed: 135 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
12351348
function 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-
14611469
function 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

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,12 @@ class D
134134
function k(#[L] int $m): callable
135135
{
136136
return #[N, O] #[P] fn(#[Q] int $r) => $r * 2;
137-
} //Testing T
137+
}
138138
139139
// Testing S
140-
//Testing S-T
141140
#[S]
142-
#[T]
141+
//Testing S-T
142+
#[T] //Testing T
143143
private function u()
144144
{
145145
return #[V] function () {

0 commit comments

Comments
 (0)