Update php-parser, fix unary snapshot tests, and make multiple fixes for attribute formatting.#2502
Conversation
…for attribute formatting.
|
Please don't merge fixes and put into description only relevant information. It's hard to review such PRs.
This is not acceptable solution. |
I'm aware. That's why I noted in the first section
I don't think it's necessarily overly verbose? I'm mostly just explaining which snapshots are updating any why. The unary operator change in particular is a bit finicky and not immediately obvious to me what was wrong with the initial implementation, which is why I wrote up the examples. |
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-parserexposes attribute groups as distinct AST nodes on methods, functions, class constants, and enum cases. I madechanges so attributes and their comments format correctly.Changes
getCommentChildNodes— attribute groups are now comment attachment targets onfunction/method,classconstant, andenumcase, so comments between#[...]blocks attach to the right node instead of being skipped or misplaced.printAttrs/printAttrGroup— each attribute group is printed throughprintAllComments(), so leading and trailing comments on groups are actually emitted.enumcaseprinting — attributes on enum cases are now printed beforecase(new coverage intests/enum/enum-case-attributes.php).handleClassMemberStatementComments— comments before a property or constant identifier are attached to the enclosing member statement, keeping modifiers likepublic statictogether when comments appear between modifiers and the name.Snapshot impact
tests/attributes/attributes.php#[S]and#[T]now print in place;//Testing Tno longer incorrectly trails the previous method's}.tests/comments/method.php()stays inside parens; inline comments betweenstaticand$fooare all preserved.tests/enum/enum-case-attributes.phpUnary 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:The old parser incorrectly parsed
+$var ** 2as(+$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 abin(**)withparenthesizedExpression: truewhen parens are explicit.Only the snapshots needed updating.
Snapshot impact
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) ** 2and(+$a) ** 1are unchanged.This is a semantic correction: the old output did not match how PHP actually parses unparenthesized
+$x ** nexpressions.