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
41 changes: 41 additions & 0 deletions src/parser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
50 changes: 50 additions & 0 deletions tests/attributes/__snapshots__/jsfmt.spec.mjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,53 @@ enum Suit: string

================================================================================
`;

exports[`classconstant.php 1`] = `
====================================options=====================================
parsers: ["php"]
printWidth: 80
| printWidth
=====================================input======================================
<?php
class QuoteSearch extends Model
{
#[Deprecated(message: 'use \\App\\Enums\\MoveType')]
public const TYPES = [
'IMPORT' => 'import',
'EXPORT' => 'export',
];

protected $attributes = [
'search_job_completed' => false,
];

#[Foo]
public const OTHER = 1;

public function bar()
{
}
}

=====================================output=====================================
<?php
class QuoteSearch extends Model
{
#[Deprecated(message: "use \\App\\Enums\\MoveType")]
public const TYPES = [
"IMPORT" => "import",
"EXPORT" => "export",
];

protected $attributes = [
"search_job_completed" => false,
];

#[Foo]
public const OTHER = 1;

public function bar() {}
}

================================================================================
`;
20 changes: 20 additions & 0 deletions tests/attributes/classconstant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
class QuoteSearch extends Model
{
#[Deprecated(message: 'use \App\Enums\MoveType')]
public const TYPES = [
'IMPORT' => 'import',
'EXPORT' => 'export',
];

protected $attributes = [
'search_job_completed' => false,
];

#[Foo]
public const OTHER = 1;

public function bar()
{
}
}
Loading