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

Prettier adds incorrect semicolon with nested foreach, for, try/catch

Open karptonite opened this issue 2 years ago • 0 comments

@prettier/plugin-php v0.18.8 Playground link

Input:

<?php

foreach( $x as $y)
  foreach ($a as $b) {
    $a = $x;
  }

foreach ($x as $y)
    for ($a = 1; $a < 10; a++) {
        $a = $x;
    };

if( $foo)
  foreach($b as $a)
  {
    $foo = bar;
  }

if($foo)
  try {
  } catch (Exception $e){
  }

Output:

<?php

foreach ($x as $y) {
    foreach ($a as $b) {
        $a = $x;
    };
}

foreach ($x as $y) {
    for ($a = 1; $a < 10; a++) {
        $a = $x;
    };
}

if ($foo) {
    foreach ($b as $a) {
        $foo = bar;
    };
}

if ($foo) {
    try {
    } catch (Exception $e) {
    };
}

I think this shows the issue. The expected output should be the same as the output, without the extra semicolons.

If you run prettier on the same file a second time, it will removed the extra semicolons. I know this affects foreach, for, try/catch, but I'm not sure that is a comprehensive list.

karptonite avatar Jun 23 '22 17:06 karptonite