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); +});