Skip to content

fix: do not insert an extra <?php before a comment after a control structure - #2547

Open
Dextheking1 wants to merge 1 commit into
prettier:mainfrom
Dextheking1:fix/2494-comment-after-if
Open

Dextheking1 wants to merge 1 commit into
prettier:mainfrom
Dextheking1:fix/2494-comment-after-if

Conversation

@Dextheking1

Copy link
Copy Markdown
Contributor

fixes #2494

Input:

<?php if (true): // example comment
  ?>
  <h1>Heading</h1>
<?php endif; ?>

Output before:

<?php if (true):<?php
    // example comment
    ?>

The comment attaches as a leading comment of the inline node in the if body. printLines printed an opening <?php for it, but a block body is already inside an open php region, so the output is a syntax error. The same bug hit while/foreach in alternative syntax and the brace form.

The opening <?php is now only printed when the parent is the program itself, the one case where no tag is open yet. Program-level output is unchanged.

New fixtures in tests/inline-comment-after-if cover if, while and the brace form. Full suite passes (104 suites, 602 tests), no snapshot changes elsewhere. AST_COMPARE=1 passes on the new fixtures.

@Dextheking1
Dextheking1 marked this pull request as ready for review October 23, 2026 11:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prettier inserts a syntax error when there is a comment after an if statement

1 participant