Skip to content

fix: keep trailing comment on the last parameter - #2560

Open
benjaminkincses wants to merge 1 commit into
prettier:mainfrom
benjaminkincses:fix/1745-trailing-parameter-comment
Open

benjaminkincses wants to merge 1 commit into
prettier:mainfrom
benjaminkincses:fix/1745-trailing-parameter-comment

Conversation

@benjaminkincses

Copy link
Copy Markdown
Contributor

Closes #1745

A line comment after the last parameter of a function or method, with the closing paren on the next line, was moved into the body:

Input:

class MyClass {
  public static function from(
    int $value1, // comment works
    int $value2 // comment gets moved down
    ) {

  }
}

Output before:

class MyClass
{
    public static function from(
        int $value1, // comment works
        int $value2,
    ) {
        // comment gets moved down
    }
}

handleLastFunctionArgComments sent any comment sitting before the body block into the body. It now attaches a comment that is on the same line as the last parameter, with ) after it, as a trailing comment of that parameter. Own-line comments before the paren keep the old behavior.

New fixture tests/comments/trailing-parameter-comment.php covers a method, a method with a return type, and a plain function.

Verified: npx jest passes, 104 suites and 601 tests, one new snapshot and no existing snapshots changed. npx eslint src/comments.mjs passes and the changed file passes the repo prettier check. Formatting the fixed output a second time gives the same output.

A line comment after the last parameter of a function or method, with
the closing paren on the next line, was moved into the body. Attach it
as a trailing comment of the parameter instead.
@benjaminkincses
benjaminkincses marked this pull request as ready for review November 26, 2026 14:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant