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; } 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() {} +} + +================================================================================ +`; 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() + { + } +}