From b8089e0086811c21136790fb4b7c6a89716f0144 Mon Sep 17 00:00:00 2001 From: Mark Reidenbach Date: Mon, 21 Sep 2026 22:00:53 -0500 Subject: [PATCH] fix: attach comments inside the body of an attributed function `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 --- src/comments.mjs | 5 ++ .../__snapshots__/jsfmt.spec.mjs.snap | 52 +++++++++++++++++++ tests/attributes/attributes-body-comments.php | 20 +++++++ 3 files changed, 77 insertions(+) create mode 100644 tests/attributes/attributes-body-comments.php diff --git a/src/comments.mjs b/src/comments.mjs index 0e868ac1c..94cedc3f3 100644 --- a/src/comments.mjs +++ b/src/comments.mjs @@ -938,10 +938,15 @@ function getCommentChildNodes(node) { if (node.attrGroups && node.attrGroups.length > 0) { if (node.kind === "method" || node.kind === "function") { + // Keep every child (in source order): dropping `body` makes comments + // inside the body resolve against the return type, dropping `name` + // makes a comment after the attribute look like it precedes the body. return [ ...node.attrGroups, + ...(node.name && typeof node.name === "object" ? [node.name] : []), ...node.arguments, ...(node.type ? [node.type] : []), + ...(node.body ? [node.body] : []), ]; } diff --git a/tests/attributes/__snapshots__/jsfmt.spec.mjs.snap b/tests/attributes/__snapshots__/jsfmt.spec.mjs.snap index 381408616..70f7baf89 100644 --- a/tests/attributes/__snapshots__/jsfmt.spec.mjs.snap +++ b/tests/attributes/__snapshots__/jsfmt.spec.mjs.snap @@ -243,6 +243,58 @@ class ParamCommentFunctionAnnotation ================================================================================ `; +exports[`attributes-body-comments.php 1`] = ` +====================================options===================================== +parsers: ["php"] +printWidth: 80 + | printWidth +=====================================input====================================== +