Skip to content

Commit

Permalink
Fix attributes using optional chaining not formatting correctly (#384)
Browse files Browse the repository at this point in the history
* fix(embed): Format shorthand and spread attributes with optional accesors properly

* chore: changeset
  • Loading branch information
Princesseuh committed Nov 8, 2023
1 parent e5dc2ec commit 11b0dc7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/mean-planets-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'prettier-plugin-astro': patch
---

Fix attributes using optional chaining not formatting correctly
19 changes: 14 additions & 5 deletions src/printer/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
closingBracketReplace,
dotReplace,
inferParserByTypeAttribute,
interrogationReplace,
isNodeWithChildren,
isTagLikeNode,
isTextNode,
Expand Down Expand Up @@ -85,6 +86,7 @@ export const embed = ((path: AstPath, options: Options) => {
doc = doc.replaceAll(closingBracketReplace, '}');
doc = doc.replaceAll(atSignReplace, '@');
doc = doc.replaceAll(dotReplace, '.');
doc = doc.replaceAll(interrogationReplace, '?');
}

return doc;
Expand Down Expand Up @@ -268,12 +270,19 @@ function makeNodeJSXCompatible<T>(node: any): T {
attr.name = openingBracketReplace + attr.name + closingBracketReplace;
}

if (attr.name.includes('@')) {
attr.name = attr.name.replaceAll('@', atSignReplace);
}
// For spreads, we don't need to do anything because it should already be JSX compatible
if (attr.kind !== 'spread') {
if (attr.name.includes('@')) {
attr.name = attr.name.replaceAll('@', atSignReplace);
}

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

if (attr.name.includes('?')) {
attr.name = attr.name.replaceAll('?', interrogationReplace);
}
}

return attr;
Expand Down
1 change: 1 addition & 0 deletions src/printer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const openingBracketReplace = '_Pé';
export const closingBracketReplace = 'èP_';
export const atSignReplace = 'ΩP_';
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);
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/other/non-jsx-compatible-characters/input.astro
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,17 @@


blah</div>}

{arr.map(() =>


<button {...object?.data} />)}


{() => <button
{object?.something}></button>}


{
<div something={object?.somethingelse}></div>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@
{(<div do.do={0}>Astro!</div>)}

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

{arr.map(() => <button {...object?.data} />)}

{() => <button {object?.something} />}

{(<div something={object?.somethingelse} />)}

0 comments on commit 11b0dc7

Please sign in to comment.