Skip to content

Commit de79d60

Browse files
authored
Bug fix: formatting loop when using bracketSameLine: true and htmlWhitespaceSensitivity: ignore (#447)
* Add failing test * Fix * Run pnpm format * Add changeset
1 parent c380d32 commit de79d60

File tree

7 files changed

+33
-2
lines changed

7 files changed

+33
-2
lines changed

.changeset/brown-oranges-tan.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+
Fixes a bug with formatting empty elements with bracketSameLine: true and htmlWhitespaceSensitivity: ignore

src/printer/embed.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,12 @@ export const embed = ((path: AstPath, options: Options) => {
155155

156156
// print
157157
const attributes = path.map(print, 'attributes');
158-
const openingTag = group(['<script', indent(group(attributes)), softline, '>']);
158+
const openingTag = group([
159+
'<script',
160+
indent(group(attributes)),
161+
options.bracketSameLine ? '' : softline,
162+
'>',
163+
]);
159164
return [
160165
openingTag,
161166
indent([isEmpty ? '' : hardline, formattedScript]),

src/printer/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export function print(path: AstPath, opts: ParserOptions, print: printFn): Doc {
177177
isTextNodeStartingWithWhitespace(node.children[0]) &&
178178
!isPreTagContent(path)
179179
? () => line
180-
: () => softline;
180+
: () => (node.children.length > 0 ? softline : '');
181181
} else if (isPreTagContent(path)) {
182182
body = () => printRaw(node);
183183
} else if (isInlineElement(path, opts, node) && !isPreTagContent(path)) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script
2+
is:inline
3+
src="https://fonts.googleapis.com/css2?family=Roboto&display=swap"
4+
crossorigin="anonymous">
5+
</script>
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script
2+
is:inline
3+
src="https://fonts.googleapis.com/css2?family=Roboto&display=swap"
4+
crossorigin="anonymous"></script>

test/tests/options.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ test(
205205
'options/single-attribute-per-line-false',
206206
);
207207

208+
// https://prettier.io/docs/options.html#bracket-line
209+
// https://prettier.io/docs/en/options.html#html-whitespace-sensitivity
210+
test(
211+
'Can format an Astro file with prettier "bracketSameLine: true, htmlWhitespaceSensitivity: ignore" options',
212+
files,
213+
'options/option-bracket-same-line-html-true-whitespace-sensitivity-ignore',
214+
);
215+
208216
// // astro option: astroSortOrder
209217
// test('Can format an Astro file with prettier "astroSortOrder: markup | styles" option', 'option-astro-sort-order-markup-styles');
210218

0 commit comments

Comments
 (0)