prettier / prettier/plugin-php

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

Open
#2,019 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
PHP
Stars
1.9k
Forks
139
PR merge metrics
No merged PRs in 30d

Description

@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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Playground example and compare the first formatting pass with the second pass. Reproduce the nested foreach, for, try/catch, and related cases, then verify that formatting produces no extra semicolons and remains unchanged when run again.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.