Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 BUG: Astro VS code extension fails to format code if multiple elements inside expression aren't wrapped in fragment #429

Open
Trombach opened this issue Sep 11, 2024 · 3 comments
Labels
needs triage Issue needs to be triaged

Comments

@Trombach
Copy link

Trombach commented Sep 11, 2024

Describe the Bug

The Astro extensions formatter seems to expect elements inside expressions to be wrapped in a single element just like JSX. The code however compiles fine and displays the correct elements without using a wrapper element.

For example, this compiles and displays fine

---
import Card from "something"

const cards = ["a", "b"];
---

{cards.map(card => (<Card 				
	href="https://docs.astro.build/"
	title={card}
	body={card}/>
	<Card 				
	href="https://docs.astro.build/"
	title={card}
	body={card}/>))}

However, the extension fails to format this and gives the following error message.

SyntaxError: Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>? 

Wrapping the cards components in a fragment results in the correct formatting.

Steps to Reproduce

  1. Clone repository and open linked file in VS Code
  2. open the command panel, use "Format document with..." and chose "Astro"
  3. check output panel for error from Astro extension
  4. wrap card components in file in fragment
  5. use "Format document with..." again
  6. file is now correctly formatted

Link to Minimal Reproducible Example

https://github.com/Trombach/withastro-astro-fmqmba/blob/main/src/pages/formatting.astro

@github-actions github-actions bot added the needs triage Issue needs to be triaged label Sep 11, 2024
@Princesseuh Princesseuh transferred this issue from withastro/language-tools Sep 11, 2024
@c1sar
Copy link

c1sar commented Oct 21, 2024

image
this also happened to me

@claraberendsen
Copy link

claraberendsen commented Nov 11, 2024

+1 to this I see it with the following versions of prettier and astro.

 prettier:
        specifier: ^3.3.3
        version: 3.3.3
      prettier-plugin-astro:
        specifier: ^0.14.1
        version: 0.14.1

@fvieira
Copy link

fvieira commented Dec 7, 2024

I'm having this error too, here's a trivially simple test case.

This formats properly:

{true && <div></div><div></div>}

This fails with the error mentioned above:

{true && <div></div> <div></div>}

and so does this:

{
  true && (
    <div />
    <div />
  )
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

4 participants