Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
],
"dependencies": {
"linguist-languages": "^8.0.0",
"php-parser": "^3.4.0"
"php-parser": "https://github.com/glayzzle/php-parser.git#0b3dbe8833965ae14915e722a1b974e3616df78c"
},
"devDependencies": {
"@babel/preset-env": "^7.27.2",
Expand Down Expand Up @@ -82,5 +82,6 @@
"include": [
"src/**"
]
}
},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
46 changes: 46 additions & 0 deletions src/comments.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function handleOwnLineComment(comment, text, options) {
options
) ||
handleTryComments(enclosingNode, followingNode, comment) ||
handleClassMemberStatementComments(enclosingNode, followingNode, comment) ||
handleClassComments(enclosingNode, followingNode, comment) ||
handleFunctionParameter(
text,
Expand Down Expand Up @@ -201,6 +202,7 @@ function handleRemainingComment(comment, text, options) {
handleGoto(enclosingNode, comment) ||
handleHalt(precedingNode, enclosingNode, followingNode, comment) ||
handleBreakAndContinueStatementComments(enclosingNode, comment) ||
handleClassMemberStatementComments(enclosingNode, followingNode, comment) ||
handleInlineComments(
enclosingNode,
precedingNode,
Expand Down Expand Up @@ -433,6 +435,32 @@ function handleTraitUseComments(enclosingNode, followingNode, comment) {
return false;
}

function handleClassMemberStatementComments(
enclosingNode,
followingNode,
comment
) {
if (
enclosingNode &&
enclosingNode.kind === "propertystatement" &&
enclosingNode.properties?.includes(followingNode)
) {
addLeadingComment(enclosingNode, comment);
return true;
}

if (
enclosingNode &&
enclosingNode.kind === "classconstant" &&
enclosingNode.constants?.includes(followingNode)
) {
addLeadingComment(enclosingNode, comment);
return true;
}

return false;
}

function handleClassComments(enclosingNode, followingNode, comment) {
if (
enclosingNode &&
Expand Down Expand Up @@ -907,6 +935,24 @@ function getCommentChildNodes(node) {
node.what.__parent_new_arguments = [...node.arguments];
return [node.what];
}

if (node.attrGroups && node.attrGroups.length > 0) {
if (node.kind === "method" || node.kind === "function") {
return [
...node.attrGroups,
...node.arguments,
...(node.type ? [node.type] : []),
];
}

if (node.kind === "classconstant") {
return [...node.attrGroups, ...node.constants];
}

if (node.kind === "enumcase") {
return node.attrGroups;
}
}
}

function canAttachComment(node) {
Expand Down
261 changes: 135 additions & 126 deletions src/printer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1199,31 +1199,16 @@ function printAttrs(path, options, print, { inline = false } = {}) {
if (!path.node.attrGroups) {
return [];
}
path.each(() => {
const attrGroup = ["#["];
path.each((attrGroupPath) => {
if (!inline && allAttrs.length > 0) {
allAttrs.push(hardline);
}
attrGroup.push(softline);
path.each(() => {
const attrNode = path.node;
if (attrGroup.length > 2) {
attrGroup.push(",", line);
}
const attrStmt = [attrNode.name];
if (attrNode.args.length > 0) {
attrStmt.push(printArgumentsList(path, options, print, "args"));
}
attrGroup.push(group(attrStmt));
}, "attrs");
allAttrs.push(
group([
indent(attrGroup),
ifBreak(shouldPrintComma(options, 8.0) ? "," : ""),
softline,
"]",
inline ? ifBreak(softline, " ") : "",
])
printAllComments(
attrGroupPath,
() => printAttrGroup(attrGroupPath, options, print, { inline }),
options
)
);
}, "attrGroups");
if (allAttrs.length === 0) {
Expand All @@ -1232,6 +1217,134 @@ function printAttrs(path, options, print, { inline = false } = {}) {
return [...allAttrs, inline ? "" : hardline];
}

function printAttrGroup(path, options, print, { inline = false } = {}) {
const attrGroup = ["#["];
attrGroup.push(softline);
path.each(() => {
const attrNode = path.node;
if (attrGroup.length > 2) {
attrGroup.push(",", line);
}
const attrStmt = [attrNode.name];
if (attrNode.args.length > 0) {
attrStmt.push(printArgumentsList(path, options, print, "args"));
}
attrGroup.push(group(attrStmt));
}, "attrs");
return group([
indent(attrGroup),
ifBreak(shouldPrintComma(options, 8.0) ? "," : ""),
softline,
"]",
inline ? ifBreak(softline, " ") : "",
]);
}

function printFunction(path, options, print) {
const { node } = path;
const declAttrs = printAttrs(path, options, print, {
inline: node.kind === "closure",
});
const declaration = [];

if (node.isFinal) {
declaration.push("final ");
}

if (node.isAbstract) {
declaration.push("abstract ");
}

if (node.visibility) {
declaration.push(node.visibility, " ");
}

if (node.isStatic) {
declaration.push("static ");
}

declaration.push("function ");

if (node.byref) {
declaration.push("&");
}

if (node.name) {
declaration.push(print("name"));
}

declaration.push(printArgumentsList(path, options, print));

if (node.uses && node.uses.length > 0) {
declaration.push(
group([" use ", printArgumentsList(path, options, print, "uses")])
);
}

if (node.type) {
declaration.push([
": ",
hasDanglingComments(node.type)
? [
path.call(() => printDanglingComments(path, options, true), "type"),
" ",
]
: "",
node.nullable ? "?" : "",
print("type"),
]);
}

const printedDeclaration = declaration;

if (!node.body) {
return [...declAttrs, printedDeclaration];
}

const printedBody = [
"{",
indent([hasEmptyBody(path) ? "" : hardline, print("body")]),
hasEmptyBody(path) ? "" : hardline,
"}",
];

const isClosure = node.kind === "closure";
if (isClosure) {
return [...declAttrs, printedDeclaration, " ", printedBody];
}

if (node.arguments.length === 0) {
return [
...declAttrs,
printedDeclaration,
shouldPrintHardlineForOpenBrace(options) && !hasEmptyBody(path)
? hardline
: " ",
printedBody,
];
}

const willBreakDeclaration = declaration.some(willBreak);

if (willBreakDeclaration) {
return [...declAttrs, printedDeclaration, " ", printedBody];
}

return [
...declAttrs,
conditionalGroup([
[
printedDeclaration,
shouldPrintHardlineForOpenBrace(options) && !hasEmptyBody(path)
? hardline
: " ",
printedBody,
],
[printedDeclaration, " ", printedBody],
]),
];
}

function printClass(path, options, print) {
const { node } = path;
const isAnonymousClass = node.kind === "class" && node.isAnonymous;
Expand Down Expand Up @@ -1353,111 +1466,6 @@ function printClass(path, options, print) {
return [printedDeclaration, printedBody];
}

function printFunction(path, options, print) {
const { node } = path;
const declAttrs = printAttrs(path, options, print, {
inline: node.kind === "closure",
});
const declaration = [];

if (node.isFinal) {
declaration.push("final ");
}

if (node.isAbstract) {
declaration.push("abstract ");
}

if (node.visibility) {
declaration.push(node.visibility, " ");
}

if (node.isStatic) {
declaration.push("static ");
}

declaration.push("function ");

if (node.byref) {
declaration.push("&");
}

if (node.name) {
declaration.push(print("name"));
}

declaration.push(printArgumentsList(path, options, print));

if (node.uses && node.uses.length > 0) {
declaration.push(
group([" use ", printArgumentsList(path, options, print, "uses")])
);
}

if (node.type) {
declaration.push([
": ",
hasDanglingComments(node.type)
? [
path.call(() => printDanglingComments(path, options, true), "type"),
" ",
]
: "",
node.nullable ? "?" : "",
print("type"),
]);
}

const printedDeclaration = declaration;

if (!node.body) {
return [...declAttrs, printedDeclaration];
}

const printedBody = [
"{",
indent([hasEmptyBody(path) ? "" : hardline, print("body")]),
hasEmptyBody(path) ? "" : hardline,
"}",
];

const isClosure = node.kind === "closure";
if (isClosure) {
return [...declAttrs, printedDeclaration, " ", printedBody];
}

if (node.arguments.length === 0) {
return [
...declAttrs,
printedDeclaration,
shouldPrintHardlineForOpenBrace(options) && !hasEmptyBody(path)
? hardline
: " ",
printedBody,
];
}

const willBreakDeclaration = declaration.some(willBreak);

if (willBreakDeclaration) {
return [...declAttrs, printedDeclaration, " ", printedBody];
}

return [
...declAttrs,
conditionalGroup([
[
printedDeclaration,
shouldPrintHardlineForOpenBrace(options) && !hasEmptyBody(path)
? hardline
: " ",
printedBody,
],
[printedDeclaration, " ", printedBody],
]),
];
}

function printBodyControlStructure(
path,
options,
Expand Down Expand Up @@ -2909,6 +2917,7 @@ function printNode(path, options, print) {

case "enumcase":
return group([
...printAttrs(path, options, print),
"case ",
print("name"),
node.value
Expand Down
6 changes: 3 additions & 3 deletions tests/attributes/__snapshots__/jsfmt.spec.mjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ class D
function k(#[L] int $m): callable
{
return #[N, O] #[P] fn(#[Q] int $r) => $r * 2;
} //Testing T
}

// Testing S
//Testing S-T
#[S]
#[T]
//Testing S-T
#[T] //Testing T
private function u()
{
return #[V] function () {
Expand Down
Loading