Plugin-php: PHP file with // prettier-ignore on method call with comments between params - Error: Comment "// Some comment" was not printed. Please report this error!

Created on 24 Apr 2020  路  9Comments  路  Source: prettier/plugin-php

@prettier/plugin-php v0.14.0
Playground link

Input:

<?php

class PHPClass {


    public function someMethod() {
        // prettier-ignore
        $this->anotherMethod(
            // Some comment 1
            "A param 1",
                // Some comment 2 indented
                "Indented param 2",
                // Some comment 3
                "Indented param 3"
        );

        $a =      "Some value";
    }
}

Output:

Comment "// Some comment 1" was not printed. Please report this error!

Expected output:

<?php

class PHPClass
{
    public function someMethod()
    {
        // prettier-ignore
        $this->anotherMethod(
            // Some comment 1
            "A param 1",
                // Some comment 2 indented
                "Indented param 2",
                // Some comment 3
                "Indented param 3"
        );

        $a = "Some value";
    }
}


bug comments ignore upstream

Most helpful comment

I just took a closer look. The issue occurs because we our parser includes the leading // in a comment's value. This should be no problem because we can (and do) overwrite hasPrettierIgnore accordingly, but prettier core currently calls isNodeIgnoreComment as a escape hatch to avoid this issue - which currently can't be overwritten by plugins.

However, this problem should disappear automatically once https://github.com/prettier/prettier/pull/8777 has been released, which improves the handling of comments inside ignore blocks in prettier core. I was able to confirm this by installing the current master of prettier/prettier.

I'd suggest to leave this issue open until the fix in prettier core has been released (and this issue hopefully just disappears :upside_down_face:)

All 9 comments

Thanks for the bug report @tonix-tuft!

I just ran into what is probably the same issue with comments in a prettier-ignored array (demo):

Input:

<?php

// @prettier-ignore
$data = [
    'a'          => 1,
    // 'b'       => 2,
];

Result: Comment "// 'b' => 2," was not printed. Please report this error!

I just saw it in functions as well. It seems to be any comments inside a prettier-ignored segment.

Any chance this is going to be fixed? Thank you!

@tonix-tuft Currently I don't have very much time to work on this plugin and I'm also not too familiar with the way prettier-ignore is handled, so I can't give any guarantees. I totally agree that this is a serious issue and I'll try to take a look at it soon. If you (or anyone else) would like to contribute you're most welcome and I'd be happy to help you get started.

I just took a closer look. The issue occurs because we our parser includes the leading // in a comment's value. This should be no problem because we can (and do) overwrite hasPrettierIgnore accordingly, but prettier core currently calls isNodeIgnoreComment as a escape hatch to avoid this issue - which currently can't be overwritten by plugins.

However, this problem should disappear automatically once https://github.com/prettier/prettier/pull/8777 has been released, which improves the handling of comments inside ignore blocks in prettier core. I was able to confirm this by installing the current master of prettier/prettier.

I'd suggest to leave this issue open until the fix in prettier core has been released (and this issue hopefully just disappears :upside_down_face:)

OK, thank you!

The issue seems to be resolved since prettier 2.1.0, so I'll go ahead and close this.

Will take a look, thank you!

Was this page helpful?
0 / 5 - 0 ratings