Skip to content

Commit

Permalink
fix: format JSX expressions with 3+ roots (#392)
Browse files Browse the repository at this point in the history
* fix: format JSX expressions with 3+ roots

* chore: changeset
  • Loading branch information
Princesseuh committed Dec 26, 2023
1 parent bc62d2f commit b4b0918
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-days-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'prettier-plugin-astro': patch
---

Fix not being able to format expressions with more than 2 roots
12 changes: 12 additions & 0 deletions src/printer/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,18 @@ function makeNodeJSXCompatible<T>(node: any): T {
return result;
}

// If we have a previous children, and it's an element AND
// we have a next children, and it's also an element. Add the current children to the bundle
if (
previousChildren &&
isTagLikeNode(previousChildren) &&
nextChildren &&
isTagLikeNode(nextChildren)
) {
childBundle[childBundleIndex].push(child);
return result;
}

// If we have elements in our bundle, and there's no next children, or it's a text node
// Create a fake parent, and add all the previous encountered elements as children of it
if (
Expand Down
27 changes: 27 additions & 0 deletions test/fixtures/other/expression-multiple-roots-stress/input.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
Promise.all([
[].map((something) => {
return [
<div></div>,
<div></div>,
<div></div>,
<div></div>
]
}),
[].map((something) => {
return <div></div><div></div><div></div><div></div>
}),
[].map((something) => {
return <div></div><div></div><div></div><div></div> + <div></div><div></div><div></div><div></div>
}),
[].map((something) => {
return <div></div><div></div><div></div><div></div> + {something: <div></div><div></div><div></div><div></div>}
}),

<div></div><div></div><div></div><div></div>
])
}

{
<div></div><div></div><div></div>
}
76 changes: 76 additions & 0 deletions test/fixtures/other/expression-multiple-roots-stress/output.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
Promise.all([
[].map((something) => {
return [<div />, <div />, <div />, <div />];
}),
[].map((something) => {
return (
<>
<div />
<div />
<div />
<div />
</>
);
}),
[].map((something) => {
return (
(
<>
<div />
<div />
<div />
<div />
</>
) +
(
<>
<div />
<div />
<div />
<div />
</>
)
);
}),
[].map((something) => {
return (
(
<>
<div />
<div />
<div />
<div />
</>
) +
{
something: (
<>
<div />
<div />
<div />
<div />
</>
),
}
);
}),

<>
<div />
<div />
<div />
<div />
</>,
])
}

{
(
<>
<div />
<div />
<div />
</>
)
}
6 changes: 6 additions & 0 deletions test/tests/other.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ test(
'other/expression-multiple-roots'
);

test(
'Can format expressions who have multi-roots returns - extreme cases',
files,
'other/expression-multiple-roots-stress'
);

test('Can ignore self-closing elements', files, 'other/ignore-self-close');

test('can format spread attributes', files, 'other/spread-attributes');

0 comments on commit b4b0918

Please sign in to comment.