From ff2e10f4628d5590ac1f929872fdc4f4cbcbb344 Mon Sep 17 00:00:00 2001 From: Antoine Apollis <4954029+apollisa@users.noreply.github.com> Date: Fri, 2 May 2025 11:54:42 +0200 Subject: [PATCH] fix: attribute PSR compatibility --- src/printer.mjs | 23 +++---- .../__snapshots__/jsfmt.spec.mjs.snap | 36 +++++------ .../__snapshots__/jsfmt.spec.mjs.snap | 62 ++++++++++--------- tests/attributes/attributes.php | 6 ++ 4 files changed, 65 insertions(+), 62 deletions(-) diff --git a/src/printer.mjs b/src/printer.mjs index cec72d0f7..bb386ce2c 100644 --- a/src/printer.mjs +++ b/src/printer.mjs @@ -1170,30 +1170,25 @@ function printAttrs(path, options, print, { inline = false } = {}) { return []; } path.each(() => { - const attrGroup = ["#["]; + const attrGroup = []; if (!inline && allAttrs.length > 0) { allAttrs.push(hardline); } - attrGroup.push(softline); - path.each(() => { + path.each((_, index) => { 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)); + if (index === 0) { + attrGroup.push("#["); + } else { + attrGroup.push(ifBreak([line, "#["], ", ")); + } + attrGroup.push(group(attrStmt), ifBreak("]")); }, "attrs"); allAttrs.push( - group([ - indent(attrGroup), - ifBreak(shouldPrintComma(options, "8.0") ? "," : ""), - softline, - "]", - inline ? ifBreak(softline, " ") : "", - ]) + group([attrGroup, ifBreak("", "]"), inline ? ifBreak(softline, " ") : ""]) ); }, "attrGroups"); if (allAttrs.length === 0) { diff --git a/tests/attributes-trail-comma/__snapshots__/jsfmt.spec.mjs.snap b/tests/attributes-trail-comma/__snapshots__/jsfmt.spec.mjs.snap index 89c78ae16..711e88c90 100644 --- a/tests/attributes-trail-comma/__snapshots__/jsfmt.spec.mjs.snap +++ b/tests/attributes-trail-comma/__snapshots__/jsfmt.spec.mjs.snap @@ -26,28 +26,24 @@ final class ORM =====================================output=====================================