Skip to content

feat(razor): index inline <script> JS in .cshtml/.razor views#959

Open
Johnny-Tsai wants to merge 1 commit into
colbymchenry:mainfrom
Johnny-Tsai:pr/razor-inline-script
Open

feat(razor): index inline <script> JS in .cshtml/.razor views#959
Johnny-Tsai wants to merge 1 commit into
colbymchenry:mainfrom
Johnny-Tsai:pr/razor-inline-script

Conversation

@Johnny-Tsai

Copy link
Copy Markdown

What

Index JavaScript (and TypeScript via lang="ts") inside <script> blocks of Razor / .cshtml views, so that a shared JS module/function correctly shows its calling views as dependents.

Closes #319 (part of #648).

Why

Traditional ASP.NET MVC / Razor views routinely put front-end logic in inline <script> blocks that import and call shared JS helpers. Before this change those <script> contents were invisible to the graph — a JS helper never listed the views that call it, so impact / blast-radius missed real front-end callers.

How

  • Added <script> block extraction to razor-extractor.ts: each block's JS/TS is run through the existing TreeSitterExtractor, and its unresolved references are attributed to the component node (lightweight style — no extra per-function nodes, so node count stays stable, consistent with the design doc invariant).
  • Key detail: these refs are tagged in the javascript / typescript language family, not razor. The dotnet-family language gate would otherwise drop cross-family JS references. Directive refs (@model, Blazor tags, @code) keep their existing razor (dotnet) tagging — only <script> contents get the web-family tag.
  • <script> blocks with a non-JS type (e.g. text/html templates) are skipped.

Testing

  • New unit test in extraction.test.ts (Razor / Blazor markup extraction describe): an inline <script> that imports + calls a shared helper makes the .cshtml view appear in the helper's impact radius. All existing Razor tests still pass.
  • Verified against a large real-world .NET ERP codebase: view callers of shared JS helpers (e.g. an AJAX-header helper) now surface correctly, matching ground-truth grep.

🤖 Generated with Claude Code

Traditional ASP.NET MVC / Razor views put front-end logic in inline
<script> blocks that import and call shared JS modules. These were
previously invisible to the graph, so a JS helper never showed its
calling views as dependents.

This extracts JS (and TS via lang="ts") inside <script> blocks of a
Razor component and attributes the unresolved references to the
component node, lightweight-style (no extra per-function nodes, so the
node count stays stable). The key detail is tagging these refs in the
`javascript`/`typescript` language family rather than `razor` — otherwise
the dotnet-family language gate drops cross-family JS references.

Directive refs (@model, Blazor tags, @code) keep their existing razor
(dotnet) tagging; only <script> contents get the web-family tag.

Verified with a synthetic test plus a large real-world .NET ERP repo
where view callers of shared JS helpers now surface correctly.

Part of colbymchenry#648, closes colbymchenry#319.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

Feature request: index Razor (.cshtml) files — embedded C# regions + script blocks

1 participant