Skip to content

Fix eslint-plugin-astro <script> parsing errors#787

Closed
smnbbrv wants to merge 1 commit intoprettier:mainfrom
smnbbrv:main
Closed

Fix eslint-plugin-astro <script> parsing errors#787
smnbbrv wants to merge 1 commit intoprettier:mainfrom
smnbbrv:main

Conversation

@smnbbrv
Copy link
Copy Markdown

@smnbbrv smnbbrv commented Apr 14, 2026

Summary

Fix eslint-plugin-prettier failing on .astro files containing <script> blocks

Problem

When using eslint-plugin-prettier with eslint-plugin-astro, linting .astro files with <script> blocks fails with:

Parsing error: Unexpected token, expected "}"  prettier/prettier

Related: ota-meshi/eslint-plugin-astro#132

Root cause

eslint-plugin-astro extracts <script> blocks as virtual .js files (e.g., Component.astro/1.js). When eslint-plugin-prettier processes these:

  1. prettier.getFileInfo() is called with the real .astro path, inferring "astro" as the parser
  2. Since the virtual filepath differs from the on-disk filepath, the code enters the parserBlocklist check
  3. "astro" was missing from the blocklist, so the plugin tried to format the bare JS/TS fragment with parser: "astro"
  4. The astro parser chokes on standalone code containing braces (object literals, function bodies, if blocks) and TypeScript syntax (interface, type, generics)

This affects all <script> variants: regular <script>, <script is:inline>, <script is:raw>, and <script define:vars={...}>.

Fix

Add 'astro' to the parserBlocklist, matching the existing behavior for vue, svelte, html, etc. The extracted script blocks are skipped since prettier will format the full .astro file anyway.

Test plan

Added integration tests covering all four Astro script types:

  • Component.astro — regular <script> with TypeScript (interface, as casts)
  • Inline.astro<script is:inline> with object literals
  • Raw.astro<script is:raw> with function bodies
  • DefineVars.astro<script define:vars={...}> with if blocks

Tests re-enable prettier/prettier on the extracted virtual files (since eslint-plugin-astro's flat/base config disables it by default) to verify the blocklist is actually exercised. Removing 'astro' from the blocklist causes all four fixtures to fail with Parsing error: Unexpected token, expected "}".

New dev dependencies

  • eslint-plugin-astro — provides the processor that extracts <script> blocks as virtual files
  • prettier-plugin-astro — required for prettier.getFileInfo() to infer "astro" as the parser for .astro files
  • @typescript-eslint/parser — parses TypeScript in extracted virtual files so ESLint doesn't fail before the prettier rule runs
  • typescript — peer dependency of @typescript-eslint/parser

Thank you for maintaining the plugin!

Summary by CodeRabbit

  • New Features

    • Added Astro file formatting support and extended linting to cover Astro component files.
  • Tests

    • Added test fixtures covering multiple Astro component script styles.
  • Chores

    • Updated formatter configuration and development dependencies to enable Astro tooling.
    • Added a changeset entry documenting the release-level update.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 14, 2026

🦋 Changeset detected

Latest commit: 87e612b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
eslint-plugin-prettier Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 14, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 4f686a33-5dfd-4ba4-8fcd-d5f707cae59b

📥 Commits

Reviewing files that changed from the base of the PR and between 33fd75a and 87e612b.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (9)
  • .changeset/clever-heads-march.md
  • .prettierrc
  • package.json
  • test/fixtures/eslint-plugin-astro/Component.astro
  • test/fixtures/eslint-plugin-astro/DefineVars.astro
  • test/fixtures/eslint-plugin-astro/Inline.astro
  • test/fixtures/eslint-plugin-astro/Raw.astro
  • test/prettier.mjs
  • worker.mjs
✅ Files skipped from review due to trivial changes (6)
  • test/fixtures/eslint-plugin-astro/DefineVars.astro
  • test/fixtures/eslint-plugin-astro/Raw.astro
  • test/fixtures/eslint-plugin-astro/Inline.astro
  • .prettierrc
  • test/fixtures/eslint-plugin-astro/Component.astro
  • .changeset/clever-heads-march.md
🚧 Files skipped from review as they are similar to previous changes (2)
  • worker.mjs
  • package.json

📝 Walkthrough

Walkthrough

Adds Astro support to the toolchain by registering prettier-plugin-astro and eslint-plugin-astro, adding TypeScript tooling, introducing Astro test fixtures, integrating the Astro ESLint flat config into tests, and skipping Prettier formatting for files detected with the astro parser in the worker.

Changes

Cohort / File(s) Summary
Prettier config
\.prettierrc
Added "prettier-plugin-astro" to Prettier plugins.
Package manifest
package\.json
Added devDependencies: eslint-plugin-astro, prettier-plugin-astro, @typescript-eslint/parser, and typescript.
Test fixtures
test/fixtures/eslint-plugin-astro/*.astro
Added four Astro fixtures: Component.astro, DefineVars.astro, Inline.astro, Raw.astro demonstrating frontmatter, define:vars, inline and raw scripts.
Test harness
test/prettier\.mjs
Imported eslint-plugin-astro flat config, remapped .astro globs to fixture path, added a test run for eslint-plugin-astro/*.astro, and enforced prettier/prettier for embedded script files via targeted overrides.
Worker
worker\.mjs
Extended parserBlocklist to include 'astro' so files inferred as the astro parser are skipped from Prettier formatting.
Changeset
.changeset/clever-heads-march\.md
Added a changeset noting fix for eslint-plugin-astro <script> parsing errors.

Sequence Diagram(s)

sequenceDiagram
  participant Dev as Developer (commit)
  participant CI as Test Runner (test/prettier.mjs)
  participant ESLint as eslint-plugin-astro
  participant Prettier as Prettier
  participant Worker as worker.mjs

  Dev->>CI: push changes (configs, fixtures, deps)
  CI->>ESLint: load flat/base config + astro overrides
  CI->>Prettier: call getFileInfo for files
  Prettier->>Worker: return inferredParser = "astro"
  Worker->>Prettier: skip format (parserBlocklist contains "astro")
  CI->>ESLint: lint .astro fixtures using eslint-plugin-astro rules
  ESLint-->>CI: report diagnostics (prettier/prettier enforced for embedded scripts)
  CI-->>Dev: test results
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • fisker

Poem

🐰 I hopped in with plugins bright and new,
Astro snippets, fixtures — a cozy view,
Prettier learns to skip when Astro's found,
ESLint hops round scripts without a bound,
Cheers from a rabbit — linting now feels true! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix eslint-plugin-astro <script> parsing errors' directly and specifically summarizes the main change: resolving parsing failures in <script> blocks within Astro files.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@smnbbrv smnbbrv closed this by deleting the head repository Apr 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant