plugin-php icon indicating copy to clipboard operation
plugin-php copied to clipboard

Comment of last argument gets replaced

Open sgruetter opened this issue 4 years ago • 4 comments

The comment of the last argument gets replaced. This also applies to /* block comments */.

@prettier/plugin-php v0.16.3 Playground link

Input:

<?php

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


Output:

<?php

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

sgruetter avatar Jun 02 '21 11:06 sgruetter

Thanks for the bug report @sgruetter!

czosel avatar Jun 14 '21 14:06 czosel

My attempt at a fix for this is in #1764

cseufert avatar Jun 22 '21 12:06 cseufert

@cseufert this still seems to be an issue even with #1764 merged.

czosel avatar Nov 06 '21 19:11 czosel

@czosel Hrmm looks like the parser is doing kinda weird things parsing these.

Looks to me like the trailing comment is not being picked up on the first line correctly in the first place. I suspect it will be quite a breaking change to the parser behaviour, I am not very confident in how the whole comments parsing and handling is done.

        Parameter {
          "attrGroups": Array [],
          "byref": false,
          "flags": 0,
          "kind": "parameter",
          "name": Identifier {
            "kind": "identifier",
            "name": "value1",
          },
          "nullable": false,
          "type": TypeReference {
            "kind": "typereference",
            "name": "int",
            "raw": "int",
          },
          "value": null,
          "variadic": false,
        },
        Parameter {
          "attrGroups": Array [],
          "byref": false,
          "flags": 0,
          "kind": "parameter",
          "leadingComments": Array [
            CommentLine {
              "kind": "commentline",
              "offset": 37,
              "value": "// comment 1
",
            },
          ],
          "name": Identifier {
            "kind": "identifier",
            "name": "value2",
          },
          "nullable": false,
          "trailingComments": Array [
            CommentLine {
              "kind": "commentline",
              "offset": 66,
              "value": "// comment 2
",
            },
          ],
          "type": TypeReference {
            "kind": "typereference",
            "name": "int",
            "raw": "int",
          },
          "value": null,
          "variadic": false,
        },
      ],

That was parsing

    function from(
    int $value1, // comment 1
    int $value2 // comment 2
    ) { ]

cseufert avatar Nov 07 '21 23:11 cseufert