Skip to content

Update php-parser, fix unary snapshot tests, and make multiple fixes for attribute formatting.#2502

Open
acharron-hl wants to merge 1 commit into
prettier:mainfrom
acharron-hl:fix/unary-parsing-enum-attributes
Open

Update php-parser, fix unary snapshot tests, and make multiple fixes for attribute formatting.#2502
acharron-hl wants to merge 1 commit into
prettier:mainfrom
acharron-hl:fix/unary-parsing-enum-attributes

Conversation

@acharron-hl
Copy link
Copy Markdown

@acharron-hl acharron-hl commented Jun 2, 2026

Fixes #2293
Fixes #2486

Dependency update note

This PR relies on a new / unpublished version of php-parser (which will likely be version 3.6.1 or 3.7.0.

It should not be merged until that version is published. In the meantime, this PR depends on the commit at the tip of the main branch over there (which includes the unary operator fix.

I was initially setting out to just fix the attribute formatting issues, but that required a new version of the php-parser. Unfortunately the currently published 3.6 has a regression around the unary operator reported as #2486 which means we can't just update to that.

Attribute formatting fixes

Fixes #2293

The updated php-parser exposes attribute groups as distinct AST nodes on methods, functions, class constants, and enum cases. Prettier-plugin-php needed matching changes so attributes and their comments format correctly.

Changes

  • getCommentChildNodes — attribute groups are now comment attachment targets on function/method, classconstant, and enumcase, so comments between #[...] blocks attach to the right node instead of being skipped or misplaced.
  • printAttrs / printAttrGroup — each attribute group is printed through printAllComments(), so leading and trailing comments on groups are actually emitted.
  • enumcase printing — attributes on enum cases are now printed before case (new coverage in tests/enum/enum-case-attributes.php).
  • handleClassMemberStatementComments — comments before a property or constant identifier are attached to the enclosing member statement, keeping modifiers like public static together when comments appear between modifiers and the name.

Snapshot impact

Test What changed
tests/attributes/attributes.php Comments between #[S] and #[T] now print in place; //Testing T no longer incorrectly trails the previous method's }.
tests/comments/method.php Dangling comment in empty () stays inside parens; inline comments between static and $foo are all preserved.
tests/enum/enum-case-attributes.php New snapshot for attributed enum cases.

Unary operator / exponentiation fix (upstream parser)

Fixes #2486

This change comes from php-parser, not from new formatting logic in the plugin. PHP gives ** higher precedence than unary + and -, so unparenthesized expressions parse differently from explicitly parenthesized ones:

+$a ** 1   // PHP parses as:  +($a ** 1)
(+$a) ** 1 // PHP parses as:  (+$a) ** 1

The old parser incorrectly parsed +$var ** 2 as (+$var) ** 2. The formatter faithfully reproduced that wrong AST, so both forms printed identically. The updated parser builds the correct tree — unary wrapping exponentiation for the unparenthesized form, and a bin(**) with parenthesizedExpression: true when parens are explicit.

The plugin's existing needs-parens.mjs rules already handle both cases correctly; only the snapshots needed updating.

Snapshot impact

Test Input Old output New output
tests/parens/bin.php $var = +$var ** 2; (+$var) ** 2 +($var ** 2)
tests/parens/unary.php $a = +$a ** 1; (+$a) ** 1 +($a ** 1)

Explicitly parenthesized inputs such as (+$var) ** 2 and (+$a) ** 1 are unchanged.

This is a semantic correction: the old output did not match how PHP actually parses unparenthesized +$x ** n expressions.

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.

Negative expression formats as functionally different code Syntax error when attributes are defined to enum cases

1 participant