Skip to content

🐛 BUG: Conditional inline script with triangle bracket causes syntax error with prettier@3.7.0 and up #452

@angelikatyborska

Description

@angelikatyborska

Describe the Bug

Hi! Let me start by saying that I was not sure if this problem should be reported here or in Prettier's issue tracker. I decided to report it here because I only experience it in Astro files and Prettier might reject it on the grounds of not supporting Astro files. If the issue is on Prettier's side and not this project's, I hope you can help me figure out exactly what to report to Prettier.

After upgrading from prettier 3.6.2 to 3.7.0, the following Astro code started throwing a syntax error when running prettier:

{ includeScript &&
  <script is:inline>
    var x = 5 > 7 ? 'not possible' : 'ok';
  </script>
}

The error is:

SyntaxError: Unexpected token `>`. Did you mean `&gt;` or `{'>'}`?

The error only appears if the inline script tag is inside of {}. This code produces no error:

<script is:inline>
  var x = 5 > 7 ? "not possible" : "ok";
</script>

The error persists in the newest Prettier version (3.7.4).

Steps to Reproduce

  1. Check out my reproduction repository (a basic Astro Starlight project) https://github.com/angelikatyborska/astro-prettier-inline-script

  2. Install deps (npm install)

  3. Run prettier (npm run prettier)

  4. There are unexpected syntax errors:

    $ npm run prettier                 
    
    > tested-trappist@0.0.1 prettier
    > prettier --write .
    
    .prettierrc.json 8ms (unchanged)
    .vscode/extensions.json 1ms (unchanged)
    .vscode/launch.json 1ms (unchanged)
    astro.config.mjs 6ms (unchanged)
    package-lock.json 125ms (unchanged)
    package.json 0ms (unchanged)
    README.md 15ms (unchanged)
    src/components/MyComponent.astro
    [error] src/components/MyComponent.astro: SyntaxError: Unexpected token `>`. Did you mean `&gt;` or `{'>'}`? (3:15)
    [error]   1 |  includeScript &&
    [error]   2 |   <script is:inline>
    [error] > 3 |     var x = 5 > 7 ? 'not possible' : 'ok';
    [error]     |               ^
    [error]   4 |   </script>
    [error]   5 |
    src/content.config.ts 16ms (unchanged)
    src/content/docs/guides/example.md 8ms (unchanged)
    src/content/docs/index.mdx
    [error] src/content/docs/index.mdx: SyntaxError: Unexpected token (3:67)
    [error]   1 | <$><CardGrid stagger>
    [error]   2 |   <Card title="Update content" icon="pencil">
    [error] > 3 |     Edit `src/content/docs/index.mdx` to see this page change.</$>
    [error]     |                                                                   ^
    src/content/docs/reference/example.md 2ms (unchanged)
    tsconfig.json 1ms (unchanged)
    

    The second error, in the mdx file, is interesting. If I get rid of the first error, the second error disappears too. It might be unrelated, or it might be a clue?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions