Skip to content

Commit

Permalink
fix(embed): Replace all instances of invalid characters inside expres…
Browse files Browse the repository at this point in the history
…sions (#378)

* fix(embed): Replace all instances of invalid characters inside expressions

* chore: changeset
  • Loading branch information
Princesseuh authored Oct 25, 2023
1 parent 40a58f1 commit 0188f04
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/selfish-worms-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'prettier-plugin-astro': patch
---

Fix attributes with multiple invalid JSX characters in their key inside expressions causing the plugin to throw an error
12 changes: 6 additions & 6 deletions src/printer/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ export const embed = ((path: AstPath, options: Options) => {
// Create a Doc without the things we had to add to make the expression compatible with Babel
const astroDoc = mapDoc(content, (doc) => {
if (typeof doc === 'string') {
doc = doc.replace(openingBracketReplace, '{');
doc = doc.replace(closingBracketReplace, '}');
doc = doc.replace(atSignReplace, '@');
doc = doc.replace(dotReplace, '.');
doc = doc.replaceAll(openingBracketReplace, '{');
doc = doc.replaceAll(closingBracketReplace, '}');
doc = doc.replaceAll(atSignReplace, '@');
doc = doc.replaceAll(dotReplace, '.');
}

return doc;
Expand Down Expand Up @@ -269,11 +269,11 @@ function makeNodeJSXCompatible<T>(node: any): T {
}

if (attr.name.includes('@')) {
attr.name = attr.name.replace('@', atSignReplace);
attr.name = attr.name.replaceAll('@', atSignReplace);
}

if (attr.name.includes('.')) {
attr.name = attr.name.replace('.', dotReplace);
attr.name = attr.name.replaceAll('.', dotReplace);
}

return attr;
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/other/non-jsx-compatible-characters/input.astro
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@

</div>
}

{true && <div x-on:click.stop.prevent="console.log('yay');">



blah</div>}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{(<div @do={0}>Astro!</div>)}

{(<div do.do={0}>Astro!</div>)}

{true && <div x-on:click.stop.prevent="console.log('yay');">blah</div>}
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "ES2019",
"module": "ES2020",
"target": "ES2022",
"module": "ES2022",
"moduleResolution": "bundler",
"removeComments": true,
"esModuleInterop": true,
Expand Down

0 comments on commit 0188f04

Please sign in to comment.