Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/printer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,19 @@ function printLines(path, options, print, childrenAttribute = "children") {

parts.push(wrappedParts);

const hasEndTag = /\?>\n?$/.test(options.originalText);
// When the file ends with an inline node, a trailing `?>` in the
// original text is part of that node's raw text (no PHP block was
// opened, e.g. a short open tag, or the close tag sits in trailing
// HTML). It is printed with the node, so appending another one here
// would duplicate it on every format.
// https://github.com/prettier/plugin-php/issues/1689
const lastChild = getLast(node.children);
const hasEndTag =
!(
lastChild &&
lastChild.kind === "inline" &&
/\?>\s*$/.test(lastChild.raw)
) && /\?>\n?$/.test(options.originalText);

if (hasEndTag) {
const lastNode = getLast(node.children);
Expand Down
19 changes: 19 additions & 0 deletions tests/short-open-tag/__snapshots__/jsfmt.spec.mjs.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`short-open-tag.php 1`] = `
====================================options=====================================
parsers: ["php"]
printWidth: 80
| printWidth
=====================================input======================================
<?

?>

=====================================output=====================================
<?

?>

================================================================================
`;
1 change: 1 addition & 0 deletions tests/short-open-tag/jsfmt.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run_spec(import.meta, ["php"]);
3 changes: 3 additions & 0 deletions tests/short-open-tag/short-open-tag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?

?>
Loading