prettier / prettier/plugin-php

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.

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

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

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

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.