prettier / prettier/plugin-php

Invalid output caused by combo of if statement, inline node, and comment

Open
#517 12 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Input:

<?php
if (true) {
    ?>inline<?php
    // comment
}

Output:

<?php
if (true) { ?>inline<?php
    // comment
    // comment
    ?>}

Note the extra closing tag that has been added, which makes the code invalid, and that the comment is duplicated, which is unexpected.

If you comment out this line, it doesn't print out the closing tag that breaks the code, but it also removes the closing tag from the test file inline3.php.

If you comment out the line above, prettier throws the following error, I think about inline3.php again.

Comment "// test" was not printed. Please report this error!

I've tried to debug, but the printing system is quite large. I'll still keep looking, but might as well put it out to the community too!

Problem aslo with

<!DOCTYPE html>
<html lang="de">
<head>
    <?php /* ups */
    if (1==1)
    {
        echo 'FOO';
    } ?>
</head>
<body>
</body>
</html>

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 in src/printer.js around lines 1362-1363 and reproduce the provided PHP cases. Compare the output with tests/comments/inline3.php, especially the inline closing-tag behavior. Done means the formatter preserves the required closing tag without adding an invalid extra tag or duplicating comments, while the existing inline3.php expectation remains valid.

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
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.