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====================================== +