Skip to content

Commit

Permalink
fix: hug when needed for components and fragments (#397)
Browse files Browse the repository at this point in the history
* fix: hug when needed for components and fragments

* fix: consider Component, Fragments and custom elements as whitespace sensitive

* chore: changeset
  • Loading branch information
Princesseuh committed Dec 27, 2023
1 parent 3d596c1 commit e97406a
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-parrots-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'prettier-plugin-astro': minor
---

Fix plugin sometimes including significant whitespace inside components, fragments and custom elements
2 changes: 1 addition & 1 deletion src/printer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export function print(path: AstPath, opts: ParserOptions, print: printFn): Doc {
}
if (!hugEnd && lastChild && isTextNode(lastChild)) {
if (isInlineElement(path, opts, node) && !didSetEndSeparator) {
noHugSeparatorEnd = softline;
noHugSeparatorEnd = line;
}
trimTextNodeRight(lastChild);
}
Expand Down
17 changes: 5 additions & 12 deletions src/printer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,15 @@ export const dotReplace = 'ωP_';
export const interrogationReplace = 'ΔP_';

export function isInlineElement(path: AstPath, opts: ParserOptions, node: anyNode): boolean {
return node && node.type === 'element' && !isBlockElement(node, opts) && !isPreTagContent(path);
return node && isTagLikeNode(node) && !isBlockElement(node, opts) && !isPreTagContent(path);
}

export function isBlockElement(node: anyNode, opts: ParserOptions): boolean {
return (
(node &&
node.type === 'element' &&
opts.htmlWhitespaceSensitivity !== 'strict' &&
(opts.htmlWhitespaceSensitivity === 'ignore' ||
blockElements.includes(node.name as TagName))) ||
node.type === 'component' ||
node.type === 'fragment'
node &&
node.type === 'element' &&
opts.htmlWhitespaceSensitivity !== 'strict' &&
(opts.htmlWhitespaceSensitivity === 'ignore' || blockElements.includes(node.name as TagName))
);
}

Expand Down Expand Up @@ -127,10 +124,6 @@ export function shouldHugStart(node: anyNode, opts: ParserOptions): boolean {
return false;
}

if (node.type === 'fragment') {
return false;
}

if (!isNodeWithChildren(node)) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/basic/html-custom-elements/output.astro
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<custom-element></custom-element>
<custom-element> </custom-element>
4 changes: 1 addition & 3 deletions test/fixtures/markdown/embedded-in-markdown/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ For best results, you should only have one `<style>` tag per-Astro component. Th
}
</style>
</head>
<body>
...
</body>
<body> ...</body>
</html>
```

Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/other/fragment/input.astro
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@
Hello world!</>
<Fragment>
<span>lorem</span></Fragment>
<>Hello world!
</>
<Fragment><span>lorem</span>
</Fragment>
</body>
</html>
8 changes: 4 additions & 4 deletions test/fixtures/other/fragment/output.astro
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<title>Document</title>
</head>
<body>
<>Hello world!</>
<Fragment>
<span>lorem</span>
</Fragment>
<> Hello world!</>
<Fragment> <span>lorem</span></Fragment>
<>Hello world! </>
<Fragment><span>lorem</span> </Fragment>
</body>
</html>

0 comments on commit e97406a

Please sign in to comment.