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
5 changes: 5 additions & 0 deletions .changeset/text-node-white-space-pre-comment.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
'"': "'",
"'": '"',
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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: /
Original file line number Diff line number Diff line change
@@ -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: /
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { test } from 'vitest';
import { assertFormattedEqualsFixed } from '../test-helpers';

test('Unit: text-node-whitespace-pre-comment', async () => {
await assertFormattedEqualsFixed(__dirname);
});
Loading