Plugin-php: Formatting twice changes result of mathematical expression

Created on 7 May 2020  路  5Comments  路  Source: prettier/plugin-php

@prettier/plugin-php v0.14.0
Playground link

Input:

<?php

// The first time formatted the braces are removed (fine).
$number = 5;
$result = (2 ** $number) - 1;


// When taking the result form the first formatting and pasting it,
// Prettier adds braces in the wrong place, changing the result (bad).
$number = 5;
$result = 2 ** $number - 1;

Output:

<?php

// The first time formatted the braces are removed (fine).
$number = 5;
$result = 2 ** $number - 1;

// When taking the result form the first formatting and pasting it,
// Prettier adds braces in the wrong place, changing the result (bad).
$number = 5;
$result = 2 ** ($number - 1);

As written in the comments above, Prettier changes the result when formatting an initial piece of code twice or more times.

Adding // prettier-ignore a line before works temporarily if (!) the change is noticed.

As this issue might have very serious implications, a quick fix would be appreciated highly.

If you need some more information or if I can be of assistance, please let me know.

bug high-pri upstream

All 5 comments

Thanks for the bug report!

No problem, thanks for Prettier for PHP :-)

I took a closer look - this turns out to be an issue with operator precedence in the parser.

We just released v0.14.2 which fixes this :tada:

Awesome, it works like a charm. Thanks a lot @czosel!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexander-akait picture alexander-akait  路  3Comments

czosel picture czosel  路  6Comments

loilo picture loilo  路  3Comments

aboyton picture aboyton  路  4Comments

adamroyle picture adamroyle  路  6Comments