Skip to content
This repository was archived by the owner on Jun 20, 2024. It is now read-only.

Commit fd7c3d7

Browse files
authored
Merge pull request #145 from Shopify/fix/issue-144
Fix trailing whitespace after opening raw tag breaking formatting
2 parents 1af0619 + 6a1ba40 commit fd7c3d7

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

src/printer/printer-liquid-html.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,13 @@ function printNode(
254254
}
255255

256256
const lines = bodyLines(node.value);
257-
const shouldSkipFirstLine =
258-
!node.source[node.position.start].match(/\r|\n/);
259-
return lines.length > 0 && lines[0].trim() !== ''
257+
258+
const rawFirstLineIsntIndented = !!node.value
259+
.split(/\r?\n/)[0]
260+
?.match(/\S/);
261+
const shouldSkipFirstLine = rawFirstLineIsntIndented;
262+
263+
return lines.length > 0 && lines.find((line) => line.trim() !== '')
260264
? join(hardline, reindent(lines, shouldSkipFirstLine))
261265
: softline;
262266
}

src/printer/utils/string.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const trimEnd = (x: string) => x.trimEnd();
1010

1111
export function bodyLines(str: string): string[] {
1212
return str
13-
.replace(/^\n*|\s*$/g, '') // only want the meat
13+
.replace(/^(?: |\t)*(\r?\n)*|\s*$/g, '') // only want the meat
1414
.split(/\r?\n/);
1515
}
1616

test/issue-144/fixed.liquid

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
it should format as expected when there's whitespace after the opening comment
2+
{% comment %}
3+
This is a comment
4+
{% endcomment %}
5+
<p>hello, world!</p>

test/issue-144/index.liquid

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
it should format as expected when there's whitespace after the opening comment
2+
{% comment %}
3+
This is a comment
4+
{% endcomment %}
5+
<p>hello, world!</p>

test/issue-144/index.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { assertFormattedEqualsFixed } from '../test-helpers';
2+
import * as path from 'path';
3+
4+
describe(`Unit: ${path.basename(__dirname)}`, () => {
5+
assertFormattedEqualsFixed(__dirname);
6+
});

0 commit comments

Comments
 (0)