Skip to content

feat(container): index .svelte and .astro via their script blocks - #444

Closed
davidpru wants to merge 1 commit into
trailhq:mainfrom
davidpru:feat/svelte-astro-container
Closed

davidpru wants to merge 1 commit into
trailhq:mainfrom
davidpru:feat/svelte-astro-container

Conversation

@davidpru

Copy link
Copy Markdown
Contributor

Follow-up to #101, which left this on the table: "Svelte and Astro are the same shape and would be a registry row each, but they are left out until someone has a repo to verify them against." They are now verified, so here are the rows.

What

.svelte and .astro join .vue in the container tier. The wrapper grammar (both ship in the tree-sitter-wasm bundle graft already depends on — no new dependency) only answers "where does the embedded code start and end"; the code itself goes through the depth-tier TypeScript extractor, so a component gets bindings, imports and resolved calls, and a call from a component into a .ts module is a real edge.

How

Svelte is a one-row addition. Its <script> is script_element > raw_text, exactly like Vue, with one wrinkle: raw_text does not start right after the tag's >. It starts at the first non-blank character of the next line, so its row is one past the tag's and the slice begins mid-line. The span shift already reads the offset off the body node's own start row rather than the tag's, so the formula "add the start row to a 1-based span" holds unchanged — but it is the kind of thing that would have silently misplaced every span had the shift been taken from the tag, so there is a fixture that pins it (attributes on the tag, indented script, markup above).

Astro needed a small generalisation of the registry: an Astro component keeps its code in two kinds of node. The --- frontmatter (frontmatter > frontmatter_js_block) is where imports and logic live; <script> (script_element > raw_text, Vue-shaped) is the client-side island. ContainerLang.block/body became blocks: EmbeddedBlock[], and blocks() matches any registered pair. The two-script Vue path already handled multiple bodies with their own offsets, so Astro reuses it as-is.

Astro also hoists a <script> from wherever it sits in the markup, and the common shape is a page wrapped in <Layout> with the script inside it. In Astro's own repo, 50 of 169 <script> blocks are nested and 12 of those carry imports — a quarter of all import-bearing scripts. So the Astro row sets deep: true and the scan descends through the tree (stopping at a block, since a block cannot nest inside another). Vue and Svelte stay top-level only: Vue's compiler ignores a <script> inside <template>, and Svelte treats only a top-level one as the component script.

Verification

  • test/container-extract.test.ts: 9 new tests. Svelte: the start-a-line-late span, <script module> + instance script each with its own offset (Svelte 5 runes are plain calls to the TS grammar), no-script degradation. Astro: frontmatter span, frontmatter + <script> each with its own offset, a nested <script> inside <Layout>, and no/empty frontmatter degradation. Plus an end-to-end build for both that asserts the calls edge into a .ts helper resolves and that graft check reads a clean build as in sync (graft check reports every .vue node as removed after a clean build — checkGraph has no container-tier branch #236).
  • test/supported-extensions.test.ts: .svelte was the "unsupported" example, as its header comment predicted; .hbs carries that side now.
  • Full suite: 1257 pass, 0 fail, 1 skipped (npm test), tsc --noEmit clean.
  • Beyond the fixtures, the same check feat(container): index .vue SFCs via their <script> block #101 ran for .vue, against four real repos — sveltejs/kit, huntabyte/shadcn-svelte, withastro/astro, withastro/docs (2,838 .svelte and 1,667 .astro files): every one of the 399 Svelte and 324 Astro symbols extracted has its name on the line its span points to, and every file that yields nothing yields nothing as .ts either (the inner extractor only emits declarations, and most components are let/$props() plus markup).
  • graft build on real projects, before → after:
    • shadcn-svelte docs/ (1,729 .svelte): 951 → 2,680 files parsed, 1,292 → 3,311 nodes, 1,132 → 8,323 edges. 7,191 of them leave a component (6,785 imports, 114 resolved calls), 182 land in a .ts/.js module.
    • withastro/docs (82 .astro): 67 → 149 files parsed, 233 → 394 nodes, 476 → 796 edges. 321 leave a component (175 imports, 53 resolved calls), 40 land in .ts/.js.

Known edges

  • tree-sitter-svelte predates Svelte 5 and errors on {@render …} / {#snippet} in the markup (286 of 2,838 files in the corpus). Checked that no <script> block is lost to those errors: script elements parse fine either side of them.
  • A <script> inside a string literal or a {…} expression is not a block, and the grammar rightly does not report one (4 fixtures in Astro's own test suite do this on purpose).

Follow-up to trailhq#101, which left Svelte and Astro out of the container tier
"until someone has a repo to verify them against". Both grammars ship in the
tree-sitter-wasm bundle graft already depends on, so this is two registry
rows plus one generalisation, now verified against four real repos.

Svelte is Vue-shaped (script_element > raw_text) with one wrinkle: raw_text
starts at the first non-blank character of the line after the tag, not right
after its `>`. The span shift reads the offset off the body node's own row,
so the formula holds unchanged; a fixture pins it because taking the row from
the tag would misplace every span silently.

Astro keeps its code in two kinds of node, the `---` frontmatter and
<script>, so ContainerLang.block/body became blocks: EmbeddedBlock[]. Astro
also hoists a <script> from anywhere in the markup, and in its own repo a
quarter of the import-bearing ones sit inside a <Layout>, so the Astro row
sets deep: true and the scan descends. Vue and Svelte stay top-level only,
which is what their compilers do.

Verified beyond the fixtures on sveltejs/kit, huntabyte/shadcn-svelte,
withastro/astro and withastro/docs: every one of the 399 Svelte and 324
Astro symbols extracted has its name on the line its span points to, and
no <script> block is lost to the errors tree-sitter-svelte raises on
Svelte 5 markup.
@trailhq-graft

trailhq-graft Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

🌱 graft blast radius

1 area changed → 3 areas can be affected. 8 dependent symbols, depth 2.
Tests: 1 area updated its tests.
Tag: @anirudhkumar-nanonets — 3 of 4 areas · @shhdwi — Workspace Graph

flowchart TB
  A0(("Workspace Graph<br/>4 symbols"))
  A1(("Graph Engine<br/>2 symbols"))
  A2(("Pull Request Review<br/>2 symbols"))
  classDef reached fill:#D9EDF3,stroke:#3AA7C9,stroke-width:1.5px,color:#0E313C;
  class A0,A1,A2 reached;
Loading
Can be affected Symbols Nearest hop Reached from
Workspace Graph 4 src/graph/build.ts:L151-L410 buildGraph — calls, depth 1 Container Extraction
Graph Engine 2 src/engine.ts:L91-L101 graph — calls, depth 2 Container Extraction
Pull Request Review 2 src/app/brain-build.ts:L251-L358 readRepository — calls, depth 2 Container Extraction
Who knows this code — 2 people across 4 areas
Area Who knows it
Workspace Graph · affected @anirudhkumar-nanonets — 20 commits, last 26d ago · @shhdwi — 12 commits, last 1mo ago
Graph Engine · affected @anirudhkumar-nanonets — 16 commits, last 2mo ago
Pull Request Review · affected @anirudhkumar-nanonets — 9 commits, last 12d ago
Container Extraction · changed only you — nobody else has touched these files

Ownership is git history over each area's own files, weighted towards recent work (120-day half-life). Merge commits and bots are dropped, and you are dropped from your own PR. A name with no @ has no GitHub handle in its commit email — tag them by hand, or add a .mailmap entry. A suggestion from history, not a CODEOWNERS rule.

All 8 dependent symbols, grouped by area

Workspace Graph — 4 symbols in 4 files

  • src/graph/build.ts:L151-L410 — buildGraph (calls, depth 1)
    200: // for the same reason: extractContainer runs inside the sync loop below.
  • src/graph/check.ts:L58-L164 — checkGraph (calls, depth 1)
    121: ? extractContainer(rel, source, container)
  • src/graph/refresh.ts:L150-L227 — ensureFreshGraph (calls, depth 2)
  • src/graph/workspace.ts:L630-L655 — federateCheck (calls, depth 2)

Graph Engine — 2 symbols in 1 file

  • src/engine.ts:L91-L101 — graph (calls, depth 2)
  • src/engine.ts:L82-L84 — checkGraph (calls, depth 2)

Pull Request Review — 2 symbols in 2 files

  • src/app/brain-build.ts:L251-L358 — readRepository (calls, depth 2)
  • src/app/review.ts:L45-L99 — reviewPullRequest (calls, depth 2)
Test signal per changed area — 1 ✓

Reached = a node under a test path has a resolved edge into the changed symbol. It undercounts anything called indirectly — through a CLI, a spawned process or a dynamic import — so read a low ratio as “look here”, never as a coverage gate.

  • ✓ Container Extraction — 1 of 2 reached · 1 test file changed here: test/container-extract.test.ts
    • not reached: visit
32 test suites also reference this code

34 symbols, kept out of the diagram and the table so they cannot crowd out the areas a reviewer has to look at.

  • test/ask-index.test.ts
  • test/ask.test.ts
  • test/context-only-dir.test.ts
  • test/context.test.ts
  • test/covers.test.ts
  • test/generic-extract.test.ts
  • test/graph-go.test.ts
  • test/graph-incremental.test.ts
  • test/graph-invariants.test.ts
  • test/graph-java.test.ts
  • test/graph-languages.test.ts
  • test/graph-php.test.ts
  • test/graph-posix-paths.test.ts
  • test/graph-python.test.ts
  • test/graph-r-classes.test.ts
  • test/graph-r-phase3.test.ts
  • test/graph-r-phase4.test.ts
  • test/graph-r-phase5.test.ts
  • test/graph-r.test.ts
  • test/graph-references.test.ts
  • …12 more

⚠️ 1 changed file not in the graph (README.md) — no parser claims the extension, or the index predates the file.

graft blast · origin/main...HEAD · depth 2 · 4 changed files

Open the interactive graph → — click an area to see its dependent symbols at file:line.

github-actions Bot added a commit that referenced this pull request Sep 22, 2026
@davidpru

Copy link
Copy Markdown
Contributor Author

Heads-up: #430 (opened two days before this) covers the same ground. I missed it — sorry.

For the maintainers, the delta here: Astro <script> blocks as well as the frontmatter (a quarter of the import-bearing ones in Astro's own repo sit nested inside <Layout>, hence deep), the registry generalised to blocks[] so a wrapper can hold more than one kind of block, and the span check run across four repos / 4,500 files with before→after build numbers. Happy to rebase onto #430 and carry only the delta if you'd rather merge that one first.

shhdwi pushed a commit that referenced this pull request Nov 9, 2026
Re-landed from #444 with the conflicts against main resolved; the contributor's fork couldn't take the merge commit.

Co-Authored-By: Shrish Dwivedi <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@shhdwi shhdwi closed this in #785 Nov 9, 2026
github-actions Bot added a commit that referenced this pull request Nov 9, 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