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. |
|
@acharron-hl php-parser v3.7.0 has just been released 👍 |
| function k(#[L] int $m): callable | ||
| { | ||
| return #[N, O] #[P] fn(#[Q] int $r) => $r * 2; | ||
| } //Testing T |
There was a problem hiding this comment.
You can see that something is messed up with comments. Why did this comment change its position?
Do you know whether it's a fault of the PHP parser or your PR?
There was a problem hiding this comment.
This is from the changes I made to comment/attribute formatting (Item 1 in my snapshot changes listed in the PR description).
So this is the raw unformatted code
}
// Testing S
#[S]
//Testing S-T
#[T] //Testing TThis is the old snapshot
} //Testing T ← comment moved here (wrong)
// Testing S
//Testing S-T ← moved above #[S] (wrong)
#[S]
#[T] And my udpated snapshot
} ← no stray comment
// Testing S
#[S]
//Testing S-T ← stays between #[S] and #[T]
#[T] //Testing T ← comment stays with #[T]
private function u()It's kind of hard to tell in the git diff that's for sure. One of the negatives of such a huge snapshot is that the input/output are hundreds of lines apart when looking at the snapshot.
|
@jorgsowa So it seems we're held up on if the project should preserve comments in between visibility modifiers or not. If you want I can implement it. Anything to get our growing list of enums out of our |
|
Sorry, for delay. Looks good. Thanks for contribution |
Fixes #2293
Fixes #2486
Dependency update note
This PR relies on a new version of php-parser: 3.7.0.
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.