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

Parens incorrectly removed when cloning the result of an expression

Open deefour opened this issue 3 years ago • 2 comments

Prettier 2.7.1

PHP Plugin 0.19.1

# Options (if any):
# our .prettierrc
{
    "phpVersion": "8.1",
    "printWidth": 80,
    "tabWidth": 4,
    "useTabs": false,
    "singleQuote": true,
    "trailingCommaPHP": true,
    "braceStyle": "psr-2",
    "requirePragma": false,
    "insertPragma": false
}

Input:

$questions = clone ($panel->primaryViewer()?->questions() ?? new Questions());

Output:

$questions = clone $panel->primaryViewer()?->questions() ?? new Questions();

Expected behavior:

The outermost parens are being removed by prettier, affecting behavior. Given

$questions = clone ($panel->primaryViewer()?->questions() ?? new Questions());

if $panel->primaryViewer() is null, because prettier drops the outermost parens, it ends up leaving PHP to evaluate clone null - something I'm actively guarding against with the null coalesce operator.

I expect the parens to remain in place, resulting in the ?? new Questions() to be cloned.

deefour avatar Oct 31 '22 15:10 deefour

👀 curious if there is any update/thoughts here. @czosel ?

deefour avatar Nov 19 '22 22:11 deefour

This definitely looks like a bug, thanks for reporting it @deefour!

Reproduction in playground

czosel avatar Nov 20 '22 09:11 czosel