From 393f8e6a3386a4e627414d4e0bbba7319047c88b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Marques?= Date: Fri, 23 Oct 2026 09:04:29 -0300 Subject: [PATCH] fix: don't duplicate comments in short-form if blocks and after open 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 (`
--- src/comments.mjs | 10 ++ src/parser.mjs | 36 ++++++ .../__snapshots__/jsfmt.spec.mjs.snap | 103 +++++++++++++++++- tests/comments/inline-open-tag-comment.php | 9 ++ tests/comments/inline-short-form-elseif.php | 11 ++ tests/comments/inline-short-form-if.php | 12 ++ 6 files changed, 179 insertions(+), 2 deletions(-) create mode 100644 tests/comments/inline-open-tag-comment.php create mode 100644 tests/comments/inline-short-form-elseif.php create mode 100644 tests/comments/inline-short-form-if.php diff --git a/src/comments.mjs b/src/comments.mjs index bc1b8a643..cc1b61524 100644 --- a/src/comments.mjs +++ b/src/comments.mjs @@ -691,6 +691,16 @@ function handleInlineComments( ) { addDanglingComment(precedingNode, comment); return true; + } else if ( + precedingNode && + precedingNode.kind === "inline" && + followingNode + ) { + // A comment right after the open tag (`?>... + +
+ + + + + +=====================================output===================================== +
+ +
+ + + + + +================================================================================ +`; + +exports[`inline-short-form-elseif.php 1`] = ` +====================================options===================================== +parsers: ["php"] +printWidth: 80 + | printWidth +=====================================input====================================== + +

a

+ + + +

c

+ + +=====================================output===================================== + +

a

+ + + +

c

+ + +================================================================================ +`; + +exports[`inline-short-form-if.php 1`] = ` +====================================options===================================== +parsers: ["php"] +printWidth: 80 + | printWidth +=====================================input====================================== + +

a

+ + + + +=====================================output===================================== + +

a

+ + + + +================================================================================ +`; + exports[`inline2.php 1`] = ` ====================================options===================================== parsers: ["php"] @@ -4627,8 +4727,7 @@ printWidth: 80 echo "foo"; ?> + echo "bar"; ?> ================================================================================ `; diff --git a/tests/comments/inline-open-tag-comment.php b/tests/comments/inline-open-tag-comment.php new file mode 100644 index 000000000..1935c680b --- /dev/null +++ b/tests/comments/inline-open-tag-comment.php @@ -0,0 +1,9 @@ +
+ +
+ + + + diff --git a/tests/comments/inline-short-form-elseif.php b/tests/comments/inline-short-form-elseif.php new file mode 100644 index 000000000..d18b87732 --- /dev/null +++ b/tests/comments/inline-short-form-elseif.php @@ -0,0 +1,11 @@ + +

a

+ + + +

c

+ diff --git a/tests/comments/inline-short-form-if.php b/tests/comments/inline-short-form-if.php new file mode 100644 index 000000000..50bcb2c4e --- /dev/null +++ b/tests/comments/inline-short-form-if.php @@ -0,0 +1,12 @@ + +

a

+ + +