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 70e63d888..b9b03dc54 100644 --- a/tests/attributes/__snapshots__/jsfmt.spec.mjs.snap +++ b/tests/attributes/__snapshots__/jsfmt.spec.mjs.snap @@ -250,3 +250,55 @@ class ParamCommentFunctionAnnotation ================================================================================ `; + +exports[`attributes-body-comments.php 1`] = ` +====================================options===================================== +parsers: ["php"] +printWidth: 80 + | printWidth +=====================================input====================================== +