From 8dadebb73bab7a1126292a4f0a8e79ba01b453fe Mon Sep 17 00:00:00 2001 From: andershagbard Date: Tue, 18 Aug 2026 10:53:51 +0200 Subject: [PATCH] Honor the `white-space: pre` comment hint for plain text nodes `printTextNode` always reflowed text as an HTML paragraph, collapsing single newlines into spaces. That's correct for real HTML text, but it silently ignored the documented `{% # white-space: pre %}` comment hint, so there was no way to opt a bare text node out of it. This mangled files with meaningful line breaks but no HTML tags, such as a templates/robots.txt.liquid override. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01F9pHQHyQvE1bP26XUkfmSj --- .../text-node-white-space-pre-comment.md | 5 ++++ .../src/printer/printer-liquid-html.ts | 23 ++++++++++++++++++- .../fixed.liquid | 5 ++++ .../index.liquid | 5 ++++ .../index.spec.ts | 6 +++++ 5 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 .changeset/text-node-white-space-pre-comment.md create mode 100644 packages/prettier-plugin-liquid/src/test/text-node-whitespace-pre-comment/fixed.liquid create mode 100644 packages/prettier-plugin-liquid/src/test/text-node-whitespace-pre-comment/index.liquid create mode 100644 packages/prettier-plugin-liquid/src/test/text-node-whitespace-pre-comment/index.spec.ts diff --git a/.changeset/text-node-white-space-pre-comment.md b/.changeset/text-node-white-space-pre-comment.md new file mode 100644 index 000000000..18314cd79 --- /dev/null +++ b/.changeset/text-node-white-space-pre-comment.md @@ -0,0 +1,5 @@ +--- +'@shopify/prettier-plugin-liquid': patch +--- + +Fix `{% # white-space: pre %}` comment hint being ignored for plain text nodes, which caused files with meaningful line breaks but no HTML tags (e.g. a `robots.txt.liquid` template) to have their lines incorrectly joined together diff --git a/packages/prettier-plugin-liquid/src/printer/printer-liquid-html.ts b/packages/prettier-plugin-liquid/src/printer/printer-liquid-html.ts index 1f61701e0..9d844ca1c 100644 --- a/packages/prettier-plugin-liquid/src/printer/printer-liquid-html.ts +++ b/packages/prettier-plugin-liquid/src/printer/printer-liquid-html.ts @@ -55,11 +55,24 @@ import { printLiquidDocPrompt, } from './print/liquid'; import { printClosingTagSuffix, printOpeningTagPrefix } from './print/tag'; -import { bodyLines, hasLineBreakInRange, isEmpty, isTextLikeNode, reindent } from './utils'; +import { + bodyLines, + hasLineBreakInRange, + isEmpty, + isPreLikeNode, + isTextLikeNode, + reindent, +} from './utils'; const { builders, utils } = doc; const { fill, group, hardline, dedentToRoot, indent, join, line, softline } = builders; +// `replaceEndOfLine` exists at runtime on both prettier 2 and prettier 3's +// `doc.utils`, but is missing from prettier 2's type definitions. +const { replaceEndOfLine } = utils as typeof utils & { + replaceEndOfLine: (doc: Doc, replacement?: Doc) => Doc; +}; + const oppositeQuotes = { '"': "'", "'": '"', @@ -161,6 +174,14 @@ function printTextNode( if (node.value.match(/^\s*$/)) return ''; const text = node.value; + if (isPreLikeNode(node)) { + return [ + printOpeningTagPrefix(node, options), + replaceEndOfLine(text), + printClosingTagSuffix(node, options), + ]; + } + const paragraphs = text .split(/(\r?\n){2,}/) .filter(Boolean) // removes empty paragraphs (trailingWhitespace) diff --git a/packages/prettier-plugin-liquid/src/test/text-node-whitespace-pre-comment/fixed.liquid b/packages/prettier-plugin-liquid/src/test/text-node-whitespace-pre-comment/fixed.liquid new file mode 100644 index 000000000..258eaf46e --- /dev/null +++ b/packages/prettier-plugin-liquid/src/test/text-node-whitespace-pre-comment/fixed.liquid @@ -0,0 +1,5 @@ +It should not reflow plain text preceded by a `{% # white-space: pre %}` comment +{% # white-space: pre %} +User-agent: GPTBot +User-agent: ClaudeBot +Allow: / diff --git a/packages/prettier-plugin-liquid/src/test/text-node-whitespace-pre-comment/index.liquid b/packages/prettier-plugin-liquid/src/test/text-node-whitespace-pre-comment/index.liquid new file mode 100644 index 000000000..258eaf46e --- /dev/null +++ b/packages/prettier-plugin-liquid/src/test/text-node-whitespace-pre-comment/index.liquid @@ -0,0 +1,5 @@ +It should not reflow plain text preceded by a `{% # white-space: pre %}` comment +{% # white-space: pre %} +User-agent: GPTBot +User-agent: ClaudeBot +Allow: / diff --git a/packages/prettier-plugin-liquid/src/test/text-node-whitespace-pre-comment/index.spec.ts b/packages/prettier-plugin-liquid/src/test/text-node-whitespace-pre-comment/index.spec.ts new file mode 100644 index 000000000..6186c0143 --- /dev/null +++ b/packages/prettier-plugin-liquid/src/test/text-node-whitespace-pre-comment/index.spec.ts @@ -0,0 +1,6 @@ +import { test } from 'vitest'; +import { assertFormattedEqualsFixed } from '../test-helpers'; + +test('Unit: text-node-whitespace-pre-comment', async () => { + await assertFormattedEqualsFixed(__dirname); +});