Skip to content

Commit a449dfe

Browse files
mreidenclaude
andauthored
fix: attach comments inside the body of an attributed function (#2517)
`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 396a609 commit a449dfe

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
@@ -243,6 +243,58 @@ class ParamCommentFunctionAnnotation
243243
================================================================================
244244
`;
245245
246+
exports[`attributes-body-comments.php 1`] = `
247+
====================================options=====================================
248+
parsers: ["php"]
249+
printWidth: 80
250+
| printWidth
251+
=====================================input======================================
252+
<?php
253+
254+
class A
255+
{
256+
#[Override]
257+
public function typed(): array
258+
{
259+
// leading comment in the body of an attributed method
260+
$vals = [];
261+
262+
// between statements
263+
return $vals;
264+
}
265+
266+
#[Override] // trailing the attribute
267+
public function untyped()
268+
{
269+
// only a comment
270+
}
271+
}
272+
273+
=====================================output=====================================
274+
<?php
275+
276+
class A
277+
{
278+
#[Override]
279+
public function typed(): array
280+
{
281+
// leading comment in the body of an attributed method
282+
$vals = [];
283+
284+
// between statements
285+
return $vals;
286+
}
287+
288+
#[Override] // trailing the attribute
289+
public function untyped()
290+
{
291+
// only a comment
292+
}
293+
}
294+
295+
================================================================================
296+
`;
297+
246298
exports[`attributes-per-cs.php 1`] = `
247299
====================================options=====================================
248300
parsers: ["php"]
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)