Skip to content

Commit a23e3aa

Browse files
committed
Merge branch 'GH-90/bracket-same-line' into master
Close GH-90 Fix missing implementation for option `bracketSameLine` for html tags. If you don't have this option on your prettier configuration you need to add it to retain your current code style. Add this configuration to retain current code style. ```yaml bracketSameLine: true ``` Otherwise closing bracket for opening tag will be printed on new line. __Previous default__ ```twig <a href="/some/url"> text </a> ``` __Current default__ ```twig <a href="/some/url" > text </a> ```
2 parents 877c774 + 8293679 commit a23e3aa

17 files changed

+188
-18
lines changed

CHANGELOG.md

+66
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,72 @@
22

33
## unreleased
44

5+
### Bugfixes
6+
- Properly implement `bracketSameLine` options for html element tag
7+
8+
__Input__
9+
```twig
10+
<iframe class=""
11+
src="https://www.google.com/maps/embed"
12+
frameborder="0"
13+
allowfullscreen></iframe>
14+
15+
<img src="/public/logo.png" alt="some description that should be enough to break this into multiline" class="block bg-white border radius-lg"/>
16+
<img src="/public/logo.png" class="block bg-white border radius-lg"/>
17+
<a href="/homepage" class="block bg-white border radius-lg" aria-label="some text label">some text</a>
18+
19+
<br />
20+
```
21+
22+
__bracketSameLine: `true`__
23+
```twig
24+
<iframe
25+
class=""
26+
src="https://www.google.com/maps/embed"
27+
frameborder="0"
28+
allowfullscreen></iframe>
29+
30+
<img
31+
src="/public/logo.png"
32+
alt="some description that should be enough to break this into multiline"
33+
class="block bg-white border radius-lg" />
34+
<img src="/public/logo.png" class="block bg-white border radius-lg" />
35+
<a
36+
href="/homepage"
37+
class="block bg-white border radius-lg"
38+
aria-label="some text label">
39+
some text
40+
</a>
41+
42+
<br />
43+
```
44+
45+
__bracketSameLine: `false`__
46+
```twig
47+
<iframe
48+
class=""
49+
src="https://www.google.com/maps/embed"
50+
frameborder="0"
51+
allowfullscreen
52+
></iframe>
53+
54+
<img
55+
src="/public/logo.png"
56+
alt="some description that should be enough to break this into multiline"
57+
class="block bg-white border radius-lg"
58+
/>
59+
<img src="/public/logo.png" class="block bg-white border radius-lg" />
60+
<a
61+
href="/homepage"
62+
class="block bg-white border radius-lg"
63+
aria-label="some text label"
64+
>
65+
some text
66+
</a>
67+
68+
<br />
69+
```
70+
571
### Internals
672
- Add option to add prefix/suffix for test snapshot output. This will allow to reuse single input file to produce several snapshot output with different configuration
773

src/print/Element.js

+27-7
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,46 @@ import {
99

1010
const { group, line, hardline, softline, indent, join } = doc.builders;
1111

12-
const printOpeningTag = (node, path, print) => {
12+
const printOpeningTag = (node, path, print, options) => {
13+
const groupId = Symbol("opening-tag");
14+
1315
const opener = "<" + node.name;
1416
const printedAttributes = printSeparatedList(path, print, "", "attributes");
15-
const openingTagEnd = node.selfClosing ? " />" : ">";
16-
const hasAttributes = node.attributes && node.attributes.length > 0;
1717

18+
const openingTagEnd = [];
19+
if (node.selfClosing) {
20+
if (!options.bracketSameLine) {
21+
openingTagEnd.push(line);
22+
} else {
23+
openingTagEnd.push(" ");
24+
}
25+
openingTagEnd.push("/>");
26+
} else {
27+
if (!options.bracketSameLine) {
28+
openingTagEnd.push(softline);
29+
}
30+
openingTagEnd.push(">");
31+
}
32+
33+
const hasAttributes = node.attributes && node.attributes.length > 0;
1834
if (hasAttributes) {
19-
return [opener, indent([line, printedAttributes]), openingTagEnd];
35+
return group(
36+
[opener, indent([line, printedAttributes]), openingTagEnd],
37+
{ id: groupId }
38+
);
2039
}
21-
return [opener, openingTagEnd];
40+
41+
return group([opener, openingTagEnd], { id: groupId });
2242
};
2343

2444
const printSeparatedList = (path, print, separator, attrName) => {
2545
return join([separator, line], path.map(print, attrName));
2646
};
2747

28-
const p = (node, path, print) => {
48+
const p = (node, path, print, options) => {
2949
// Set a flag in case attributes contain, e.g., a FilterExpression
3050
node[EXPRESSION_NEEDED] = true;
31-
const openingGroup = group(printOpeningTag(node, path, print));
51+
const openingGroup = group(printOpeningTag(node, path, print, options));
3252
node[EXPRESSION_NEEDED] = false;
3353
node[STRING_NEEDS_QUOTES] = false;
3454

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{% for foo in range(1, category) %}
22
<span
33
key="{{ foo }}"
4-
class="qtp-item__star icon-ic icon-icn_star--white {{ foo }}">
4+
class="qtp-item__star icon-ic icon-icn_star--white {{ foo }}"
5+
>
56
{% include './Star.twig' only %}
67
</span>
78
{% endfor %}

tests/ControlStructures/__snapshots__/if.snap.twig

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
(css.logoWider): useWiderItems
1414
}|classes }}"
1515
srcABC="{{ partner.logoUrl }}"
16-
alt="{{ partner.name }}" />
16+
alt="{{ partner.name }}"
17+
/>
1718
{%- elseif partnerName %}
1819
<b class="{{ css.name }}">{{ partnerName }}</b>
1920
{% elseif partnerImg -%}

tests/Element/__snapshots__/attributes.snap.twig

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
ref: intersectionObserver|default,
77
class: 'hotel-item item-order__list-item js_co_item',
88
'data-co_alt_htl': isAlternative ? '1'
9-
}|attrs }}>
9+
}|attrs }}
10+
>
1011
Test
1112
</fantasy>
1213

tests/Element/__snapshots__/manyAttributes.snap.twig

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
attr5="five"
77
attr6="six"
88
attr7="seven"
9-
attr8="eight">
9+
attr8="eight"
10+
>
1011
Text
1112
</span>

tests/Element/__snapshots__/selfClosing.snap.twig

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
attr5="five"
99
attr6="six"
1010
attr7="seven"
11-
attr8="eight" />
11+
attr8="eight"
12+
/>
1213

1314
<input type="text" name="user" />
1415
<input type="text" name="password" />

tests/Expressions/__snapshots__/mappingExpression.snap.twig

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,5 @@
6868
theme
6969
})
7070
})
71-
}}></div>
71+
}}
72+
></div>

tests/Expressions/__snapshots__/objectExpression.snap.twig

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<h1
1111
class="{{ {
1212
('hero__title--' ~ (locale|lower)): locale in ['CN', 'JP', 'DE', 'RU']
13-
}|classes }}">
13+
}|classes }}"
14+
>
1415
Heading
1516
</h1>

tests/Failing/__snapshots__/controversial.snap.twig

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
<section
1111
class="{{ {
1212
base: css.prices
13-
} | classes }}"></section>
13+
} | classes }}"
14+
></section>
1415

1516
<!-- This is what happens if we reduce indentation depth here:
1617
"as" and object keys at same indentation level

tests/Failing/__snapshots__/failing.snap.twig

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
class="rat-chart__bar-content rat-color--{{ valueIndex }}"
4343
{{ {
4444
style: width
45-
} | attrs }}>
45+
} | attrs }}
46+
>
4647

4748
</span>
4849
</span>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<iframe
2+
class=""
3+
src="https://www.google.com/maps/embed"
4+
frameborder="0"
5+
allowfullscreen
6+
></iframe>
7+
8+
<img
9+
src="/public/logo.png"
10+
alt="some description that should be enough to break this into multiline"
11+
class="block bg-white border radius-lg"
12+
/>
13+
<img src="/public/logo.png" class="block bg-white border radius-lg" />
14+
<a
15+
href="/homepage"
16+
class="block bg-white border radius-lg"
17+
aria-label="some text label"
18+
>
19+
some text
20+
</a>
21+
22+
<br />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<iframe
2+
class=""
3+
src="https://www.google.com/maps/embed"
4+
frameborder="0"
5+
allowfullscreen></iframe>
6+
7+
<img
8+
src="/public/logo.png"
9+
alt="some description that should be enough to break this into multiline"
10+
class="block bg-white border radius-lg" />
11+
<img src="/public/logo.png" class="block bg-white border radius-lg" />
12+
<a
13+
href="/homepage"
14+
class="block bg-white border radius-lg"
15+
aria-label="some text label">
16+
some text
17+
</a>
18+
19+
<br />

tests/Options/bracket_same_line.twig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<iframe class=""
2+
src="https://www.google.com/maps/embed"
3+
frameborder="0"
4+
allowfullscreen></iframe>
5+
6+
<img src="/public/logo.png" alt="some description that should be enough to break this into multiline" class="block bg-white border radius-lg"/>
7+
<img src="/public/logo.png" class="block bg-white border radius-lg"/>
8+
<a href="/homepage" class="block bg-white border radius-lg" aria-label="some text label">some text</a>
9+
10+
<br />

tests/Options/jsfmt.spec.js

+22
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,26 @@ describe("Options", () => {
2929
});
3030
await expect(actual).toMatchFileSnapshot(snapshotFile);
3131
});
32+
33+
it("bracket same line - enabled", async () => {
34+
const { actual, snapshotFile } = await run_spec(import.meta.url, {
35+
source: "bracket_same_line.twig",
36+
suffix: "_enabled",
37+
formatOptions: {
38+
bracketSameLine: true
39+
}
40+
});
41+
await expect(actual).toMatchFileSnapshot(snapshotFile);
42+
});
43+
44+
it("bracket same line - disabled", async () => {
45+
const { actual, snapshotFile } = await run_spec(import.meta.url, {
46+
source: "bracket_same_line.twig",
47+
suffix: "_disabled",
48+
formatOptions: {
49+
bracketSameLine: false
50+
}
51+
});
52+
await expect(actual).toMatchFileSnapshot(snapshotFile);
53+
});
3254
});

tests/Statements/__snapshots__/macro.snap.twig

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
type="{{ type }}"
1818
name="{{ name }}"
1919
value="{{ value|e }}"
20-
size="{{ size }}" />
20+
size="{{ size }}"
21+
/>
2122
{% endmacro %}
2223

2324
{%- macro wrapped_input(name, value, type, size) %}

tests/smoke-test.twig

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
<meta charset="UTF-8" />
55
<meta
66
name="viewport"
7-
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />
7+
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
8+
/>
89
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
910
<title>
1011
Document

0 commit comments

Comments
 (0)