Repository navigation
Conversation
…tags - php-parser ends the body of a short-form `if` (`if (): ... else: ... endif;`) where the whole statement ends, so it overlaps the alternate and comments inside the `else:`/`elseif:` branch were printed twice (and once more on every run). The body now ends at its last child. - A comment right after an open tag that follows inline HTML (`<div><?php // comment`) was attached as a trailing comment of the inline node and printed twice. It is now a leading comment of the next statement. Fixes prettier#1705 Fixes prettier#2495 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1705
Fixes #2495
Comments in templates that mix PHP and inline HTML were duplicated, and the copies grew on every run (1 → 2 → 4 → 8), which is painful with format-on-save. Two separate causes:
1. Short-form
ifwithelse:/elseif:php-parser ends the body of a short-form
ifwhere the whole statement ends, so the body overlaps the alternate:Any comment inside the
else:/elseif:branch is then inside both blocks. It ended up as a dangling comment of theif, printed twice and moved beforeelse:.parse()now ends the body at its last child, and the comment stays where it was written.Before (one run):
After: unchanged.
2. Comment right after an open tag that follows inline HTML
<div> <?php // comment $a = 1; ?> </div>This became a trailing comment of the
inlinenode.printLines()printsinline.commentsitself and prettier prints it again, so it was duplicated.handleInlineCommentsnow makes it a leading comment of the next statement. This is where the plugin already puts an own-line comment in the same spot (<?php\n// comment\n$a = 1;is formatted as<?php // comment\n$a = 1;), so the second run is stable too.Tests
tests/comments/inline-short-form-if.php,inline-short-form-elseif.php,inline-open-tag-comment.php. Onmainthey print every comment twice on the first run.comments/inline9.php: the blank line between<?php //BARandecho "bar"goes away and the statement keeps the block indentation.yarn test,AST_COMPARE=1 yarn test:nodeandyarn lintpass.#,//,/* */and/** */, at different positions insideif:/else:/elseif:/foreach:/plain HTML) with 3 runs each. All are stable now; onmain, 35 of them duplicated.Not covered
#1228 (
?>\n<?phpwith nothing between) is a different issue. It comes from the___PSEUDO_INLINE_PLACEHOLDER___workaround inparse(): node locations then refer to the text with the placeholder, not to the original text. Comments after such a placeholder can be misclassified (for example, an own-line comment printed at the end of the previous line). This PR does not touch that.🤖 Generated with Claude Code