Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/comments.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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") &&
Expand Down
62 changes: 62 additions & 0 deletions tests/comments/__snapshots__/jsfmt.spec.mjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6937,6 +6937,68 @@ throw /* Comment */ new Foo() /* Comment */; // Comment
================================================================================
`;

exports[`trailing-parameter-comment.php 1`] = `
====================================options=====================================
parsers: ["php"]
printWidth: 80
| printWidth
=====================================input======================================
<?php

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

public function withReturnType(
int $value1,
int $value2 // trailing comment
): int {
return $value1 + $value2;
}
}

function plainFunction(
int $value1,
int $value2 // trailing comment
) {
return $value1 + $value2;
}

=====================================output=====================================
<?php

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

public function withReturnType(
int $value1,
int $value2, // trailing comment
): int {
return $value1 + $value2;
}
}

function plainFunction(
int $value1,
int $value2, // trailing comment
) {
return $value1 + $value2;
}

================================================================================
`;

exports[`trait.php 1`] = `
====================================options=====================================
parsers: ["php"]
Expand Down
25 changes: 25 additions & 0 deletions tests/comments/trailing-parameter-comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

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

public function withReturnType(
int $value1,
int $value2 // trailing comment
): int {
return $value1 + $value2;
}
}

function plainFunction(
int $value1,
int $value2 // trailing comment
) {
return $value1 + $value2;
}
Loading