Conversation
`getCommentChildNodes()` returns an explicit child list for functions
and methods that carry attributes, but it left out `name` and `body`.
Any comment inside the body of such a function was then resolved
against the return type instead of the body: with a return type it
crashed in `handleFunctionParameter` ("Cannot read properties of
undefined (reading 'kind')"), and without one the comment was hoisted
out of the body.
Return every child in source order. Including `name` keeps a comment
trailing the attribute (`#[T] // comment`) attached to the attribute
rather than being pushed into the body by
`handleLastFunctionArgComments`, so existing snapshots are unchanged.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On
main(not in 0.25.0), any comment inside the body of a function or method that carries an attribute breaks formatting:Without a return type it does not crash; the comments are instead hoisted out of the body and printed between the signature and
{.Cause
getCommentChildNodes()returns an explicit child list for attributed functions/methods (attrGroups,arguments,type) and omitsnameandbody. Prettier therefore never descends into the body: a body comment getsprecedingNode = type,followingNode = undefined, andhandleFunctionParameterdereferences the missing following node.Fix
Return every child in source order. Including
namematters too: with onlybodyadded, a comment trailing the attribute (#[T] // comment) sees the body as its following node andhandleLastFunctionArgCommentspushes it into the body, changing the existingtests/attributessnapshot. Withnamepresent that comment stays on the attribute, so existing snapshots are unchanged.Tests
tests/attributes/attributes-body-comments.php: comments at the start of, and between statements in, an attributed method with and without a return type, plus a comment trailing the attribute. Full suite passes withAST_COMPARE=1.🤖 Generated with Claude Code