Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prettier/plugin-php",
"version": "0.25.0",
"name": "@vanilla/prettier-plugin-php",
Comment thread
jorgsowa marked this conversation as resolved.
Outdated
"version": "0.26.0",
Comment thread
jorgsowa marked this conversation as resolved.
Outdated
"description": "Prettier PHP Plugin",
"repository": "prettier/prettier-php",
"author": "Lucas Azzola <@azz>",
Expand All @@ -27,7 +27,7 @@
],
"dependencies": {
"linguist-languages": "^8.0.0",
"php-parser": "^3.4.0"
"php-parser": "^3.7.0"
},
"devDependencies": {
"@babel/preset-env": "^7.27.2",
Expand Down
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) {
Comment thread
jorgsowa marked this conversation as resolved.
Outdated
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
Loading
Loading