Skip to content

Commit 569b5a5

Browse files
authored
fix(#1785) workaround for extra end tag appearing when first line line has comment (#1764)
1 parent c9d80db commit 569b5a5

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/printer.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,14 +1049,20 @@ function printLines(path, options, print, childrenAttribute = "children") {
10491049
: isProgramLikeNode(node) && isFirstNode && node.kind !== "namespace"
10501050
? ""
10511051
: concat([beforeCloseTagInlineNode, "?>"]);
1052+
1053+
const nextV = path.getNode(index + 1);
1054+
const skipLastComment = nextV && nextV.children && nextV.children.length;
1055+
10521056
const afterInline =
10531057
childNode.comments && childNode.comments.length
10541058
? concat([
10551059
openTag,
10561060
hardline,
1057-
comments.printComments(childNode.comments, options),
1061+
skipLastComment
1062+
? comments.printComments(childNode.comments, options)
1063+
: "",
10581064
hardline,
1059-
"?>",
1065+
skipLastComment ? "?>" : "",
10601066
])
10611067
: isProgramLikeNode(node) && isLastNode
10621068
? ""

tests/comments/__snapshots__/jsfmt.spec.js.snap

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4641,6 +4641,29 @@ $a = 1; ?>
46414641
================================================================================
46424642
`;
46434643
4644+
exports[`inline9.php 1`] = `
4645+
====================================options=====================================
4646+
parsers: ["php"]
4647+
printWidth: 80
4648+
| printWidth
4649+
=====================================input======================================
4650+
<?php //FOO
4651+
echo "foo";?>
4652+
4653+
<?php //BAR
4654+
echo "bar";?>
4655+
4656+
=====================================output=====================================
4657+
<?php //FOO
4658+
echo "foo"; ?>
4659+
4660+
<?php //BAR
4661+
4662+
echo "bar"; ?>
4663+
4664+
================================================================================
4665+
`;
4666+
46444667
exports[`interface.php 1`] = `
46454668
====================================options=====================================
46464669
parsers: ["php"]

tests/comments/inline9.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php //FOO
2+
echo "foo";?>
3+
4+
<?php //BAR
5+
echo "bar";?>

0 commit comments

Comments
 (0)