Skip to content

Prettier duplicates a comment and inserts it somewhere else #2495

Description

@Pawel-Bakalarz

@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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions