Skip to content

Prettier fails / misparses <script> when conditionally rendered in .astro template expressions #454

@rutvikkaturi

Description

@rutvikkaturi

Astro Info

Astro                    v5.17.1
Vite                     v6.4.1
Node                     v22.14.0
System                   macOS (arm64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             astro-sitemap (v1.0.0)
                         astro-robots-txt (v1.0.0)

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

When a <script> tag is placed inside a conditional expression in an .astro file (ternary or &&), Prettier fails to format the file and throws a parsing error. The code compiles and runs correctly in both dev and build; the issue appears to be in Prettier parsing. This might be related to <script> being treated as a special Astro tag and not a normal JSX/HTML node when nested under expression containers.

Reproduction

  1. Create a new Astro project
  2. Install and enable prettier + prettier-plugin-astro
  3. Add the following to a .astro file
  4. Run Prettier on the file (Format Document / CLI)
---
const view = 'Gallery';
---

{view === 'Gallery' ? (
  <script>
    for (let index = 0; index < 10; index++) {
      console.log(index);
    }
  </script>
) : null}

{view === 'Gallery' &&
  (<script>
    for (let index = 0; index < 10; index++) {
      console.log(index);
    }
  </script>)
}

What's the expected result?

Prettier formats the file without errors, and preserves the script contents (or formats them consistently).

Link to Minimal Reproducible Example

Participation

  • I am willing to submit a pull request for this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageIssue needs to be triaged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions