Skip to content

Commit c380d32

Browse files
authored
Format custom elements & components as block elements with htmlWhitespaceSensitivity: ignore (#448)
1 parent 49456da commit c380d32

File tree

6 files changed

+49
-2
lines changed

6 files changed

+49
-2
lines changed

.changeset/slimy-plums-learn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"prettier-plugin-astro": patch
3+
---
4+
5+
Format custom elements & components as block elements with htmlWhitespaceSensitivity: ignore

src/printer/utils.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,20 @@ export function isInlineElement(path: AstPath, opts: ParserOptions, node: anyNod
3131
}
3232

3333
export function isBlockElement(node: anyNode, opts: ParserOptions): boolean {
34+
if (!node) {
35+
return false;
36+
}
37+
38+
// All tags (element, custom-element, component, fragment) are considered
39+
// block elements when htmlWhitespaceSensitivity is set to "ignore".
40+
if (opts.htmlWhitespaceSensitivity === 'ignore') {
41+
return true;
42+
}
43+
3444
return (
35-
node &&
3645
node.type === 'element' &&
3746
opts.htmlWhitespaceSensitivity !== 'strict' &&
38-
(opts.htmlWhitespaceSensitivity === 'ignore' || blockElements.includes(node.name as TagName))
47+
blockElements.includes(node.name as TagName)
3948
);
4049
}
4150

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
import Link from "@/components/link.astro";
3+
---
4+
5+
<ul>
6+
<li><a href="https://github.com/withastro/prettier-plugin-astro">Prettier Plugin for Astro (using HTML element)</a></li>
7+
<li><Link href="https://github.com/withastro/prettier-plugin-astro">Prettier Plugin for Astro (using component)</Link></li>
8+
</ul>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"bracketSameLine": true,
3+
"htmlWhitespaceSensitivity": "ignore"
4+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
import Link from "@/components/link.astro";
3+
---
4+
5+
<ul>
6+
<li>
7+
<a href="https://github.com/withastro/prettier-plugin-astro">
8+
Prettier Plugin for Astro (using HTML element)
9+
</a>
10+
</li>
11+
<li>
12+
<Link href="https://github.com/withastro/prettier-plugin-astro">
13+
Prettier Plugin for Astro (using component)
14+
</Link>
15+
</li>
16+
</ul>

test/tests/options.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ test(
185185
files,
186186
'options/option-html-whitespace-sensitivity-ignore',
187187
);
188+
test(
189+
'Can format components with prettier "htmlWhitespaceSensitivity: ignore" option',
190+
files,
191+
'options/option-html-whitespace-sensitivity-ignore-component',
192+
);
188193

189194
// https://prettier.io/docs/en/options.html#single-attribute-per-line
190195
test(

0 commit comments

Comments
 (0)