Skip to content

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

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

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

Conversation

@benjaminkincses

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.

@benjaminkincses
benjaminkincses 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

1 participant