withastro / language-tools

Language tools for Astro
MIT License
269 stars 53 forks source link

🐛 BUG: Formatting Layout components gives unexpected reults #846

Closed jmayergit closed 7 months ago

jmayergit commented 7 months ago

Describe the Bug

Astro extension for VSCode configured with Prettier

Layout structure

<html>
  <head>
    <AstroComponent />
  </head>
  <body>
  </body>
</html>

<script src=""></script>

Above, the Layout component (an Astro component) has another Astro component (AstroComponent) in the head and a script located outside the <html>

I suppose this is invalid html to have script tags located outside the html element however given "Template Syntax" documentation it's expected to be a valid Astro component

Formatting this document will move the script tag inside the html element

If there is no Astro component located in the head then the script and/or style tags located outside the html tag are not moved inside the html tag when formatting, and thus the result is expected

Full Layout component provided in steps to reproduce

Where should we place script tags in Layout components? Will astro still process these?

Steps to Reproduce

  1. pnpm create astro@latest
  2. select "Use blog template" when bootstrapping
  3. cd [randomly-generated-project-name]
  4. open apps/astro-app/src/layouts/main.astro and copy paste Layout component
Layout component ```Astro --- import type { CollectionEntry } from "astro:content"; import BaseHead from "../components/BaseHead.astro"; import Header from "../components/Header.astro"; import Footer from "../components/Footer.astro"; import FormattedDate from "../components/FormattedDate.astro"; type Props = CollectionEntry<"blog">["data"]; const { title, description, pubDate, updatedDate, heroImage } = Astro.props; ---
{ heroImage && ( ) }
{ updatedDate && (
Last updated on
) }

{title}


```
  1. Format document
  2. script tag is moved unexpectedly
diff --git a/src/layouts/BlogPost.astro b/src/layouts/BlogPost.astro
index bcebf6b..b2a1c85 100644
--- a/src/layouts/BlogPost.astro
+++ b/src/layouts/BlogPost.astro
@@ -85,7 +85,5 @@ const { title, description, pubDate, updatedDate, heroImage } = Astro.props;
       </article>
     </main>
     <Footer />
-  </body>
+  </body><script src=""></script>
 </html>
-
-<script src=""></script>
Princesseuh commented 7 months ago

Duplicate of https://github.com/withastro/prettier-plugin-astro/issues/316