From a3604d2108cb1774cf5ac57840e3f2fb5ec86b0e Mon Sep 17 00:00:00 2001 From: mao <118530756+maoxor@users.noreply.github.com> Date: Thu, 29 Oct 2026 18:18:39 +0100 Subject: [PATCH] fix: keep a comment after the last parameter on that parameter When the parameter list ends with a trailing comma, a comment between the last parameter and the comma was attached to the return type, so formatting the output again moved it after `):`. --- src/comments.mjs | 9 +++- .../__snapshots__/jsfmt.spec.mjs.snap | 49 +++++++++++++++++++ tests/comments/last-parameter.php | 18 +++++++ 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 tests/comments/last-parameter.php diff --git a/src/comments.mjs b/src/comments.mjs index bc1b8a643..55ab20144 100644 --- a/src/comments.mjs +++ b/src/comments.mjs @@ -553,10 +553,17 @@ function handleFunction( enclosingNode.body && locStart(comment) > argumentsLocEnd && locEnd(comment) < locStart(enclosingNode.body); - const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex( + let nextCharIndex = getNextNonSpaceNonCommentCharacterIndex( text, locEnd(comment) ); + // skip the trailing comma of the argument list, if any + if (text.charAt(nextCharIndex) === ",") { + nextCharIndex = getNextNonSpaceNonCommentCharacterIndex( + text, + nextCharIndex + 1 + ); + } // we additionally need to check if this isn't a trailing argument comment, // by checking the next character isn't ")" if ( diff --git a/tests/comments/__snapshots__/jsfmt.spec.mjs.snap b/tests/comments/__snapshots__/jsfmt.spec.mjs.snap index 5adfe5f8c..81ecd709c 100644 --- a/tests/comments/__snapshots__/jsfmt.spec.mjs.snap +++ b/tests/comments/__snapshots__/jsfmt.spec.mjs.snap @@ -4763,6 +4763,55 @@ if ( ================================================================================ `; +exports[`last-parameter.php 1`] = ` +====================================options===================================== +parsers: ["php"] +printWidth: 80 + | printWidth +=====================================input====================================== +