Skip to content

fix: attributes leaking from class constants and lost on classes declared in a block - #1309

Open
maoxor wants to merge 2 commits into
glayzzle:mainfrom
maoxor:fix-leaking-and-dropped-attributes
Open

maoxor wants to merge 2 commits into
glayzzle:mainfrom
maoxor:fix-leaking-and-dropped-attributes

Conversation

@maoxor

@maoxor maoxor commented Oct 29, 2026

Copy link
Copy Markdown

Two attribute bugs that show up as wrong output in @prettier/plugin-php:

Attributes of a class constant leak into the next member. read_class_body resets attrs after enum cases, methods and properties, but not after constants, so in

class A
{
    #[Deprecated]
    public const B = 1;

    public function f() {}
}

f() also gets #[Deprecated], and Prettier prints it there. nette/utils hits this in Strings::checkEncoding(). This is what prettier/plugin-php#2436 reports; #1225 fixed the location but not the leak. The existing location snapshot had recorded it on $bar, so that snapshot changes too.

Attributes on a class or interface declared in a block are dropped. read_inner_statement reads them but doesn't pass them to read_class_declaration_statement / read_interface_declaration_statement, and it doesn't accept readonly class at all (syntax error). Symfony's polyfill stubs are written like this:

if (\PHP_VERSION_ID < 80400) {
    #[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_FUNCTION | Attribute::TARGET_CLASS_CONSTANT)]
    final class Deprecated

and Prettier currently removes the #[Attribute] line.

Each fix is its own commit with a test in attributes.test.js; both tests fail without the change. CI=1 yarn jest passes (1570 tests, coverage thresholds included). I also formatted nette/utils Strings.php and the six Symfony polyfill stubs with @prettier/plugin-php 0.26.0 on top of this branch, and the result has the same AST as the input (checked with nikic/php-parser).

read_class_body reset the attribute list after enum cases, methods and properties, but not after constants, so the member following an attributed constant got the same attributes. The location snapshot had recorded the leaked attribute on `$bar`.
read_inner_statement read the attributes but did not pass them on to class and interface declarations, and did not accept readonly classes at all.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant