Skip to content

Commit

Permalink
Add support for formatting spread attributes (#320)
Browse files Browse the repository at this point in the history
* feat(embed): Add support for formatting spread attributes

* test(embed): Add test for formatting spread attributes

* chore: changeset
  • Loading branch information
Princesseuh committed Jan 26, 2023
1 parent c724082 commit 18cd321
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-needles-cheer.md
@@ -0,0 +1,5 @@
---
'prettier-plugin-astro': minor
---

Add support for formatting spread attributes
9 changes: 9 additions & 0 deletions src/printer/embed.ts
Expand Up @@ -93,6 +93,15 @@ export function embed(
return [line, name, '=', '{', attrNodeValue, '}'];
}

if (node.type === 'attribute' && node.kind === 'spread') {
const spreadContent = wrapParserTryCatch(textToDoc, forceIntoExpression(node.name), {
...opts,
parser: expressionParser,
});

return [line, '{...', spreadContent, '}'];
}

// Frontmatter
if (node.type === 'frontmatter') {
const frontmatterContent = wrapParserTryCatch(textToDoc, node.value, {
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/other/spread-attributes/input.astro
@@ -0,0 +1,5 @@
<div {...{datatype: ""}}></div>

<div {...myObject}></div>

<div {...get({ thing: 'hey'})}></div>
5 changes: 5 additions & 0 deletions test/fixtures/other/spread-attributes/output.astro
@@ -0,0 +1,5 @@
<div {...{ datatype: "" }}></div>

<div {...myObject}></div>

<div {...get({ thing: "hey" })}></div>
2 changes: 2 additions & 0 deletions test/tests/other.test.ts
Expand Up @@ -111,3 +111,5 @@ test(
);

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

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

0 comments on commit 18cd321

Please sign in to comment.