Skip to content

Commit

Permalink
Merge branch 'main' into fix/break-children
Browse files Browse the repository at this point in the history
  • Loading branch information
Princesseuh authored Dec 27, 2023
2 parents abef6ef + 4354afd commit 5db1e33
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 25 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
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ export default {

To customize formatting behavior, see the [Configuration](#configuration) section below.

## Using in VS Code
## Formatting with the VS Code Prettier extension directly

> **Note**
> If you're using [Astro's VS Code extension](https://marketplace.visualstudio.com/items?itemName=astro-build.astro-vscode), Prettier and this plugin are already included. Only follow the guide below to format using Prettier's official extension.
[The Astro VS Code extension](https://marketplace.visualstudio.com/items?itemName=astro-build.astro-vscode) uses Prettier and this plugin (`prettier-plugin-astro`) to format your code. You will only need to install the VS Code Prettier extension separately for formatting if:

First install the [VS Code Prettier extension](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode) and add the following settings to your VS Code configuration:
- You are not using Astro's VS Code extension.
- You want to use features of the Prettier extension that not supported by Astro's own VS Code extension, such as the toolbar panel showing Prettier's status.

Install the [VS Code Prettier extension](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode) and add the following settings to your VS Code configuration:

```json
{
Expand Down Expand Up @@ -92,7 +95,7 @@ See [CONTRIBUTING.md](./CONTRIBUTING.md) for instructions on how to set up your

## Sponsors

Astro is generously supported by Netlify, Storyblok, and several other amazing organizations.
Astro is free, open source software made possible by these wonderful sponsors.

[❤️ Sponsor Astro! ❤️](https://github.com/withastro/.github/blob/main/FUNDING.md)

Expand Down
2 changes: 1 addition & 1 deletion src/printer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,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 @@ -32,7 +32,7 @@ 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 isBreakChildrenElement(node: anyNode): boolean {
Expand All @@ -41,13 +41,10 @@ export function isBreakChildrenElement(node: anyNode): boolean {

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 @@ -136,10 +133,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 5db1e33

Please sign in to comment.