Prettier duplicates a comment and inserts it somewhere else

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

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
php
Domain
tooling

Research direction

Start by reproducing the issue from the supplied Playground links and compare the formatter output with the input, especially around the PHP comments near else and endif. Trace the plugin-php comment-handling path; done means comments are neither duplicated nor moved when the surrounding PHP block is present or removed.

Written by the indexing model from the issue text.

Description

@prettier/plugin-php v0.25.0
Playground link

Input:

<?php if (have_posts()): ?>

  <p>Content if have posts</p>

  <?php
  /* My comment */
  ?>

  <?php
  // Comment in else
  // Another comment
  // Comment number three
  else: ?>

  <?php if (true): ?>

    <p>This is always true</p>

  <?php endif;
  // end true check
  ?>

<?php endif;
// end have_posts() check
?>

Output:

<?php if (have_posts()): ?>

  <p>Content if have posts</p>

  <?php
  /* My comment */
  ?>

  <?php
  // Comment in else
  // Another comment
  // Comment number three
  // end true check

  else: ?>

  <?php if (true): ?>

    <p>This is always true</p>

  <?php endif;
  // end true check
  ?>

<?php endif;
// end have_posts() check
?>

Notice that the //end true check comment is inserted next to the comments near the else statement.

However, a weirder things happen when I delete the entire /* My comment */ PHP block:

@prettier/plugin-php v0.25.0
Playground link

Input:

<?php if (have_posts()): ?>

  <p>Content if have posts</p>

  <?php
  // Comment in else
  // Another comment
  // Comment number three
  else: ?>

  <?php if (true): ?>

    <p>This is always true</p>

  <?php endif;
  // end true check
  ?>

<?php endif;
// end have_posts() check
?>

Output:

<?php if (have_posts()): ?>

  <p>Content if have posts</p>

  <?php
  // Comment in else
  // Another comment
  // Comment number three
  // end true check
  // Comment in else
  // Another comment
  // Comment number three
  // end true check
  else: ?>

  <?php if (true): ?>

    <p>This is always true</p>

  <?php endif;
  // end true check
  ?>

<?php endif;
// end have_posts() check
?>

Multiple comments get duplicated

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

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.

More from prettier/plugin-php

All issues in prettier/plugin-php

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.