Repository navigation
feat: parse only the frontmatter and <script> bodies of .astro files - #1
Conversation
Parsing a whole Astro component as TypeScript misreads its markup: a
<script> import after JSX-like markup such as {items.map((i) => <li>{i}</li>)}
was missed, and import-like text in the markup was reported as an import.
Each code section is now parsed on its own, and HTML comments are skipped.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…Script> - A <script> with any attribute besides src is inline in Astro and is not bundled, so its imports are not dependencies. - Comments and scripts are found in one scan, so a <!-- inside a script no longer hides the scripts after it. - Frontmatter without a closing fence runs to the end of the file. - Only lowercase <script> counts; <Script> is a component. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 1Reviewer: Claude Opus 5.5, an independent general-purpose agent that checked each case against
Checked and fine: BOM, CRLF, trailing spaces on fences, empty frontmatter, |
Response to review round 1
|
… script tags A <script src> bundles its file and drops the body, and an uppercase SRC is inline, so a body counts only when the tag has no attributes. A self-closing <script /> no longer runs to the next </script>, <script-foo> is no longer a script, and the closing tag matches in any case. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Review round 2 — Claude Opus 5.5 (independent general-purpose agent), commit be576cc. Verdict: changes. Ground truth this round:
Checked and fine: inline detection for |
|
Response to review round 2
|
|
Review round 3 — Claude Opus 5.5 (the round 2 reviewer, resumed), commit 6af5b90. Verdict: approve.
|
.astrofiles matched bysourcePatternsare now scanned by parsing only their code: the frontmatter and the body of each<script>that Astro bundles. Other files are handled as before, and the defaultsourcePatternsdon't change.Why
The collector parsed a whole Astro component with the TypeScript parser. The markup is not TypeScript, and error recovery gave wrong results:
<script>import that follows JSX-like markup such as{items.map((i) => <li>{i}</li>)}was missed (a false negative, so a missing dependency went unreported).<code>import D from 'pkg'</code>orcall import('pkg'), was reported as an import (a false positive).Change
src/imports/collector.ts, for a file ending in.astro:---and the next---line. Without a closing fence, it runs to the end of the file, as in Astro.<script>in the rest of the file. Comments and scripts are found in one scan, so a<!--inside a script stays script text, and a script inside a comment is skipped. A self-closing<script />has no body,<script-foo>is another element, and the closing tag matches in any case.<script>without attributes is parsed. Withsrc, Astro bundles the file and drops the body; any other attribute (type="module",is:inline,define:vars, an uppercaseSRC, …) makes the script inline, so its imports are not package dependencies.<Script>is a component in Astro, not a script.requirecalls.README.md: thesourcePatternsrow says how to include.astrofiles and what gets parsed.Tests
New tests in
src/__tests__/imports.test.ts, undercollectImports in .astro files:<script>import after JSX-like markup, a backtick and/*in text<script>, including<script >and asrc-only scripttype="module",is:inline,define:vars) are ignoredsrcscript and of an uppercaseSRCscript is ignored<script />does not swallow the markup after it<script-foo>is another element;</SCRIPT>closes a script<!--inside a script does not hide the scripts after it<script>inside an HTML comment is ignored<Script>is a componentAgainst 0.3.0's collector, 9 of these tests fail. Against bd54b62, 7 fail: the four cases from review round 1 (inline scripts,
<!--in a script,<Script>, open frontmatter) and the three from round 2. Against be576cc, the three from round 2 fail. All pass with the change.Checks
bun run typecheck: passesbun test: 171 pass, 0 fail.astrofiles (sourcePatterns: ['src/**/*.{ts,tsx,astro}']): no issues, the same result as with 0.3.0Not covered
The scan is line- and tag-based, not a real Astro parser, which would mean depending on
@astrojs/compiler(WebAssembly). Cases it gets wrong, all rare:---line inside a template literal in the frontmatter closes the frontmatter early.--- // comment), or text before the opening fence: the frontmatter is not found.<script>text inside<textarea>or<style>content, or inside a JS string in the markup, is read as a script.<svg>,<template>,<noscript>or anis:rawelement is collected; Astro does not bundle it.<!--in an attribute value or an expression string hides the scripts up to the next-->.Decisions
src/imports/collector.tssrc/__tests__/imports.test.tsStatus
changes, fixed in be576cc; round 2changes, fixed in 6af5b90; round 3approveon 6af5b90🤖 Generated with Claude Code