From dfa961bd31b12da5194f88736d0ad350a95972ba Mon Sep 17 00:00:00 2001 From: benjaminkincses <4175097+benjaminkincses@users.noreply.github.com> Date: Wed, 25 Nov 2026 20:20:06 +0100 Subject: [PATCH 1/3] fix: stop class constant attributes leaking onto the next member --- src/parser.mjs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/parser.mjs b/src/parser.mjs index 8df49870d..7d9d90fd3 100644 --- a/src/parser.mjs +++ b/src/parser.mjs @@ -61,6 +61,47 @@ function parse(text, opts) { } }); + // php-parser does not clear the attribute groups of a class constant, + // so the next class member reuses the same attrgroup object and the + // printer emits the attribute twice. Drop every attrgroup occurrence + // after the first one, ordered by source position. Two attribute + // groups written in the source are always distinct objects, so a + // repeated object can only come from this parser bug. + const attrGroupUses = []; + const collectAttrGroups = (node) => { + if (!node || typeof node !== "object") { + return; + } + if (Array.isArray(node)) { + node.forEach(collectAttrGroups); + return; + } + if (Array.isArray(node.attrGroups)) { + for (const attrGroup of node.attrGroups) { + attrGroupUses.push({ owner: node, attrGroup }); + } + } + for (const value of Object.values(node)) { + collectAttrGroups(value); + } + }; + collectAttrGroups(ast); + attrGroupUses.sort( + (a, b) => + (a.attrGroup.loc?.start?.offset ?? 0) - + (b.attrGroup.loc?.start?.offset ?? 0) + ); + const seenAttrGroups = new Set(); + for (const { owner, attrGroup } of attrGroupUses) { + if (seenAttrGroups.has(attrGroup)) { + owner.attrGroups = owner.attrGroups.filter( + (group) => group !== attrGroup + ); + } else { + seenAttrGroups.add(attrGroup); + } + } + return ast; } From 444c0fd425bffb94a368665e55c4f389188e6cf8 Mon Sep 17 00:00:00 2001 From: benjaminkincses <4175097+benjaminkincses@users.noreply.github.com> Date: Wed, 25 Nov 2026 20:20:11 +0100 Subject: [PATCH 2/3] test: class constant attribute followed by other members --- tests/attributes/classconstant.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/attributes/classconstant.php diff --git a/tests/attributes/classconstant.php b/tests/attributes/classconstant.php new file mode 100644 index 000000000..e9a70f3e5 --- /dev/null +++ b/tests/attributes/classconstant.php @@ -0,0 +1,20 @@ + 'import', + 'EXPORT' => 'export', + ]; + + protected $attributes = [ + 'search_job_completed' => false, + ]; + + #[Foo] + public const OTHER = 1; + + public function bar() + { + } +} From bcd41954f40329dfa0ab192577b1547c4d6091a8 Mon Sep 17 00:00:00 2001 From: benjaminkincses <4175097+benjaminkincses@users.noreply.github.com> Date: Wed, 25 Nov 2026 20:20:16 +0100 Subject: [PATCH 3/3] test: update snapshots --- .../__snapshots__/jsfmt.spec.mjs.snap | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/attributes/__snapshots__/jsfmt.spec.mjs.snap b/tests/attributes/__snapshots__/jsfmt.spec.mjs.snap index 70f7baf89..613223052 100644 --- a/tests/attributes/__snapshots__/jsfmt.spec.mjs.snap +++ b/tests/attributes/__snapshots__/jsfmt.spec.mjs.snap @@ -392,3 +392,53 @@ enum Suit: string ================================================================================ `; + +exports[`classconstant.php 1`] = ` +====================================options===================================== +parsers: ["php"] +printWidth: 80 + | printWidth +=====================================input====================================== + 'import', + 'EXPORT' => 'export', + ]; + + protected $attributes = [ + 'search_job_completed' => false, + ]; + + #[Foo] + public const OTHER = 1; + + public function bar() + { + } +} + +=====================================output===================================== + "import", + "EXPORT" => "export", + ]; + + protected $attributes = [ + "search_job_completed" => false, + ]; + + #[Foo] + public const OTHER = 1; + + public function bar() {} +} + +================================================================================ +`;