Skip to content

Commit 29a935d

Browse files
mreidenclaude
andcommitted
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 <noreply@anthropic.com>
1 parent 269374b commit 29a935d

3 files changed

Lines changed: 77 additions & 0 deletions

File tree

‎src/comments.mjs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,10 +938,15 @@ function getCommentChildNodes(node) {
938938

939939
if (node.attrGroups && node.attrGroups.length > 0) {
940940
if (node.kind === "method" || node.kind === "function") {
941+
// Keep every child (in source order): dropping `body` makes comments
942+
// inside the body resolve against the return type, dropping `name`
943+
// makes a comment after the attribute look like it precedes the body.
941944
return [
942945
...node.attrGroups,
946+
...(node.name && typeof node.name === "object" ? [node.name] : []),
943947
...node.arguments,
944948
...(node.type ? [node.type] : []),
949+
...(node.body ? [node.body] : []),
945950
];
946951
}
947952

‎tests/attributes/__snapshots__/jsfmt.spec.mjs.snap‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,55 @@ class ParamCommentFunctionAnnotation
250250
251251
================================================================================
252252
`;
253+
254+
exports[`attributes-body-comments.php 1`] = `
255+
====================================options=====================================
256+
parsers: ["php"]
257+
printWidth: 80
258+
| printWidth
259+
=====================================input======================================
260+
<?php
261+
262+
class A
263+
{
264+
#[Override]
265+
public function typed(): array
266+
{
267+
// leading comment in the body of an attributed method
268+
$vals = [];
269+
270+
// between statements
271+
return $vals;
272+
}
273+
274+
#[Override] // trailing the attribute
275+
public function untyped()
276+
{
277+
// only a comment
278+
}
279+
}
280+
281+
=====================================output=====================================
282+
<?php
283+
284+
class A
285+
{
286+
#[Override]
287+
public function typed(): array
288+
{
289+
// leading comment in the body of an attributed method
290+
$vals = [];
291+
292+
// between statements
293+
return $vals;
294+
}
295+
296+
#[Override] // trailing the attribute
297+
public function untyped()
298+
{
299+
// only a comment
300+
}
301+
}
302+
303+
================================================================================
304+
`;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
class A
4+
{
5+
#[Override]
6+
public function typed(): array
7+
{
8+
// leading comment in the body of an attributed method
9+
$vals = [];
10+
11+
// between statements
12+
return $vals;
13+
}
14+
15+
#[Override] // trailing the attribute
16+
public function untyped()
17+
{
18+
// only a comment
19+
}
20+
}

0 commit comments

Comments
 (0)