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 @@ +