From 2511937831d80bfc14624a514bffd75fe6141c44 Mon Sep 17 00:00:00 2001 From: benjaminkincses <4175097+benjaminkincses@users.noreply.github.com> Date: Tue, 1 Dec 2026 19:04:16 +0100 Subject: [PATCH] Do not duplicate the closing tag when a file ends in inline content --- src/printer.mjs | 14 +++++++++++++- .../__snapshots__/jsfmt.spec.mjs.snap | 19 +++++++++++++++++++ tests/short-open-tag/jsfmt.spec.mjs | 1 + tests/short-open-tag/short-open-tag.php | 3 +++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 tests/short-open-tag/__snapshots__/jsfmt.spec.mjs.snap create mode 100644 tests/short-open-tag/jsfmt.spec.mjs create mode 100644 tests/short-open-tag/short-open-tag.php diff --git a/src/printer.mjs b/src/printer.mjs index a9f06c170..af8285a2c 100644 --- a/src/printer.mjs +++ b/src/printer.mjs @@ -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); diff --git a/tests/short-open-tag/__snapshots__/jsfmt.spec.mjs.snap b/tests/short-open-tag/__snapshots__/jsfmt.spec.mjs.snap new file mode 100644 index 000000000..0dfb1dcaf --- /dev/null +++ b/tests/short-open-tag/__snapshots__/jsfmt.spec.mjs.snap @@ -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===================================== + + +================================================================================ +`; diff --git a/tests/short-open-tag/jsfmt.spec.mjs b/tests/short-open-tag/jsfmt.spec.mjs new file mode 100644 index 000000000..8829c872f --- /dev/null +++ b/tests/short-open-tag/jsfmt.spec.mjs @@ -0,0 +1 @@ +run_spec(import.meta, ["php"]); diff --git a/tests/short-open-tag/short-open-tag.php b/tests/short-open-tag/short-open-tag.php new file mode 100644 index 000000000..179e2dada --- /dev/null +++ b/tests/short-open-tag/short-open-tag.php @@ -0,0 +1,3 @@ +