From 91f95fad36e4ab05860df6c692b8140fb9865ace Mon Sep 17 00:00:00 2001 From: benjaminkincses <4175097+benjaminkincses@users.noreply.github.com> Date: Thu, 26 Nov 2026 13:55:00 +0100 Subject: [PATCH] fix: keep trailing comment on the last parameter 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. --- src/comments.mjs | 19 ++++++ .../__snapshots__/jsfmt.spec.mjs.snap | 62 +++++++++++++++++++ tests/comments/trailing-parameter-comment.php | 25 ++++++++ 3 files changed, 106 insertions(+) create mode 100644 tests/comments/trailing-parameter-comment.php diff --git a/src/comments.mjs b/src/comments.mjs index bc1b8a643..2869e0f0d 100644 --- a/src/comments.mjs +++ b/src/comments.mjs @@ -296,6 +296,25 @@ function handleLastFunctionArgComments( return true; } + // A comment on the same line as the last parameter, with the closing + // paren after it, belongs to that parameter. Without this it falls + // through to the body branch below and lands inside the body. + // https://github.com/prettier/plugin-php/issues/1745 + if ( + precedingNode && + enclosingNode && + (enclosingNode.kind === "function" || enclosingNode.kind === "method") && + enclosingNode.arguments && + enclosingNode.arguments.length > 0 && + enclosingNode.arguments[enclosingNode.arguments.length - 1] === + precedingNode && + nextCharacter === ")" && + !hasNewlineInRange(text, locEnd(precedingNode), locStart(comment)) + ) { + addTrailingComment(precedingNode, comment); + return true; + } + if ( enclosingNode && (enclosingNode.kind === "function" || enclosingNode.kind === "method") && diff --git a/tests/comments/__snapshots__/jsfmt.spec.mjs.snap b/tests/comments/__snapshots__/jsfmt.spec.mjs.snap index 5adfe5f8c..a382968f3 100644 --- a/tests/comments/__snapshots__/jsfmt.spec.mjs.snap +++ b/tests/comments/__snapshots__/jsfmt.spec.mjs.snap @@ -6937,6 +6937,68 @@ throw /* Comment */ new Foo() /* Comment */; // Comment ================================================================================ `; +exports[`trailing-parameter-comment.php 1`] = ` +====================================options===================================== +parsers: ["php"] +printWidth: 80 + | printWidth +=====================================input====================================== +