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

Commit 50aee93

Browse files
authored
Merge pull request #159 from Shopify/fix/158-line-breaks-btn-attribtues
Maintain at most 1 newline between tag attributes
2 parents 2bc7004 + eca6768 commit 50aee93

File tree

5 files changed

+82
-5
lines changed

5 files changed

+82
-5
lines changed

src/printer/print/tag.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
last,
2929
first,
3030
isPrettierIgnoreAttributeNode,
31+
hasMoreThanOneNewLineBetweenNodes,
3132
} from '~/printer/utils';
3233

3334
const {
@@ -271,10 +272,22 @@ function printAttributes(
271272

272273
const prettierIgnoreAttributes = isPrettierIgnoreAttributeNode(node.prev);
273274

274-
const printedAttributes = path.map(
275-
(attr) => print(attr, { trailingSpaceGroupId: attrGroupId }),
276-
'attributes',
277-
);
275+
const printedAttributes = path.map((attr) => {
276+
const attrNode = attr.getValue();
277+
let extraNewline: Doc = '';
278+
if (
279+
attrNode.prev &&
280+
hasMoreThanOneNewLineBetweenNodes(
281+
attrNode.source,
282+
attrNode.prev,
283+
attrNode,
284+
)
285+
) {
286+
extraNewline = hardline;
287+
}
288+
const printed = print(attr, { trailingSpaceGroupId: attrGroupId });
289+
return [extraNewline, printed];
290+
}, 'attributes');
278291

279292
const forceBreakAttrContent = node.source
280293
.slice(node.blockStartPosition.start, last(node.attributes).position.end)

src/printer/utils/string.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { LiquidAstPath, LiquidHtmlNode, LiquidParserOptions } from '~/types';
1+
import {
2+
LiquidAstPath,
3+
LiquidHtmlNode,
4+
LiquidParserOptions,
5+
Position,
6+
} from '~/types';
27

38
export function isWhitespace(source: string, loc: number): boolean {
49
if (loc < 0 || loc >= source.length) return false;
@@ -49,3 +54,14 @@ export function hasLineBreakInRange(
4954
const indexOfNewLine = source.indexOf('\n', locStart);
5055
return 0 <= indexOfNewLine && indexOfNewLine < locEnd;
5156
}
57+
58+
export function hasMoreThanOneNewLineBetweenNodes(
59+
source: string,
60+
prev: { position: Position } | undefined,
61+
next: { position: Position } | undefined,
62+
): boolean {
63+
if (!prev || !next) return false;
64+
const between = source.slice(prev.position.end, next.position.start);
65+
const count = between.match(/\n/g)?.length || 0;
66+
return count > 1;
67+
}

test/issue-158/fixed.liquid

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
It should maintain at most 1 newline between attributes, and none at the end
2+
<label class="block relative">
3+
<input
4+
class="absolute inset-0 opacity-0 pointer-events-none peer"
5+
type="radio"
6+
name="id"
7+
8+
id="variant-{{ variant.id }}"
9+
value="{{ variant.id }}"
10+
required
11+
12+
{% unless variant.available %}
13+
disabled
14+
{% endunless %}
15+
16+
{% if product.selected_variant == variant %}
17+
checked
18+
{% endif %}
19+
>
20+
</label>

test/issue-158/index.liquid

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
It should maintain at most 1 newline between attributes, and none at the end
2+
<label class="block relative">
3+
<input
4+
class="absolute inset-0 opacity-0 pointer-events-none peer"
5+
type="radio"
6+
name="id"
7+
8+
9+
id="variant-{{ variant.id }}"
10+
value="{{ variant.id }}"
11+
required
12+
13+
{% unless variant.available %}
14+
disabled
15+
{% endunless %}
16+
17+
{% if product.selected_variant == variant %}
18+
checked
19+
{% endif %}
20+
21+
>
22+
</label>

test/issue-158/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)