feat(extraction+resolution): Astro support — frontmatter/template extraction + src/pages routes (#768) (#815)
.astro files were not indexed at all, leaving a typical Astro site mostly
invisible to search/impact/explore. New AstroExtractor (Svelte/Vue SFC
pattern): component node per file, TS frontmatter + <script> blocks
delegated to the TypeScript extractor, template {fn(...)} calls (incl. the
multiline `{posts.map((post) => (` opening line), PascalCase component-tag
references. New astroResolver: Astro global + astro:* virtual modules as
framework-provided, component resolution with the #764 ambiguity rule,
src/pages/ file-based routes ([param]→:param, [...rest]→*rest, _-prefixed
and *.config.* excluded). SFC languages now preload the TS/JS grammars
their extractors delegate to (a pure-SFC file set previously had none
loaded). Also fixes a pre-existing Svelte/Vue script-block off-by-one that
reported every script symbol one line low.
Validated per the playbook: stalux (the issue's repro) 54/54 .astro files
indexed, getIconNode found at its exact line, 14/14 routes, 93.0% fair
cross-file coverage; AstroPaper 27/27 components, 13/13 routes (underscore
dirs correctly excluded), explore connects page→Card→Datetime through the
jsx-render synthesizer; node/edge counts stable across re-syncs.
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
763ee9c825
commit
823ffd1c3d
@@ -16,6 +16,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
### New Features
|
||||
|
||||
- **Astro projects are now indexed.** `.astro` files previously weren't parsed at all — on a typical Astro site that left most of the codebase invisible to search, impact, and `codegraph_explore`. CodeGraph now extracts the TypeScript frontmatter (functions, imports, `getStaticPaths`, …) and client-side `<script>` blocks, captures function calls and `<Component>` usages in template markup so cross-component dependencies trace end-to-end, resolves the `Astro` global and `astro:*` module imports as framework-provided, and maps `src/pages/` file-based routing to route nodes (`.astro` pages and `.ts` endpoints, including `[param]` and `[...rest]` dynamic segments, with underscore-prefixed files correctly excluded). Validated on two real-world Astro sites with 93% measured cross-file coverage and every page mapping to its route. Thanks @xingwangzhe. (#768) (Astro)
|
||||
- Same-named symbols across a monorepo's apps are no longer conflated. In a NestJS-style workspace with one `UserService` per app, `codegraph_callers`, `codegraph_callees`, and `codegraph_impact` now report **one section per distinct definition** — each app's callers and blast radius under its own file-labeled heading — instead of a single merged list, and accept a `file` argument to focus exactly the definition you mean (like `codegraph_node` already did). Impact in particular no longer overstates a change's blast radius by merging unrelated same-named classes. Thanks @Igorgro. (#764)
|
||||
- Fixed a related source of cross-package wrong edges: PascalCase **type references from plain `.ts` files were being resolved as React components**, which could link a file's own type alias to an arbitrary same-named class in another package (on one large monorepo this produced over a thousand wrong cross-package reference edges; 96% are now gone, and the remainder are genuine shared-model imports). Component resolution now applies only to references from JSX-capable files and never guesses between multiple candidates without a positional signal. The **Svelte and Vue component resolvers had the same arbitrary-pick flaw** (Vue resolved the first same-named `.vue` file found anywhere in the tree) and now follow the same rule: same-directory first, otherwise only an unambiguous name resolves. Re-index a project to benefit. (#764) (TypeScript, React, Svelte, Vue)
|
||||
- TypeScript and JavaScript **class fields are now reported as properties instead of methods**. A plain field like `public fonts: Fonts;` previously extracted as a method, misrepresenting class shape and letting calls to same-named functions resolve to data fields (a boolean field named `isArray` was soaking up `Array.isArray(...)` call edges). Fields holding arrow functions or function expressions (`onClick = () => {…}`, including wrapped ones like `onScroll = throttle(() => {…})`) correctly remain methods and their bodies are still analyzed. Field initializers are analyzed too, so `history = createHistory()` records its call — and JavaScript class fields, which previously produced no symbol at all, now appear in the graph. Re-index a project to benefit. (#808) (TypeScript, JavaScript)
|
||||
@@ -38,6 +39,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
### Fixes
|
||||
|
||||
- Symbols defined in Svelte and Vue `<script>` blocks were reported one line below where they actually are — a function on line 3 was reported at line 4 — which offset every script-block symbol's location in search, `codegraph_node`, and explore output. Line numbers now match the file exactly. Re-index a project to benefit. (Svelte, Vue)
|
||||
- Doc comments are now captured for exported, `const`-assigned, and decorated declarations, and the documentation a symbol carries is now clean across every supported language. Previously a comment above `export class X`, `export const fn = () => …`, a plain `const fn = () => …`, or a decorated Python `def`/`class` (`@app.route(...)`, `@dataclass`) was dropped entirely — only comments directly above a plain declaration were kept. CodeGraph now finds the comment through the `export` / `const` / decorator wrapper. Comment-marker cleanup was also rounded out for every language CodeGraph supports: Rust/Swift/Kotlin doc lines (`///`, `//!`), Python/Ruby/shell `#`, Lua/Luau (`--` and `--[[ ]]`), and Pascal (`{ }` and `(* *)`) no longer leave stray markers in the stored text — validated end-to-end across all 19 code languages plus Svelte/Vue `<script>` blocks. (#780). Thanks @caleb-kaiser.
|
||||
- Go method calls made through a chained factory function now resolve to the correct type. A call like `New().Method()` used to drop the receiver, so the chained method attached to a same-named method on an unrelated type — or didn't resolve. CodeGraph now captures Go return types (a pointer `*Foo` resolves to `Foo`, and a multi-return `(*Foo, error)` to its first result), infers the chained receiver's type from what the factory function returns, and resolves the method on it — including methods promoted from an embedded struct — creating the edge only when the type or an embedded type genuinely has the method. Existing Go indexes should be re-indexed (`codegraph index -f`) to benefit. (#750) (Go)
|
||||
- Scala method calls made through a companion-object factory, a fluent chain, or a case-class `apply` now resolve to the correct type. A call like `Foo.create().bar()` or `Builder(cfg).bar()` used to drop the receiver, so the chained method silently attached to a same-named method on an unrelated type — most often mis-attributing a standard-library `Option` / `Iterator` `.map` / `.flatMap` / `.foreach` onto your own same-named class. CodeGraph now captures Scala return types (a generic `List[Foo]` resolves to its container `List`, a qualified `pkg.Foo` to `Foo`), infers the chained receiver's type from what the inner call returns or constructs, and resolves the method on it — including methods inherited from a trait the type extends — creating the edge only when that type or one of its traits genuinely has the method (so a wrong inference produces no edge instead of a misleading one). Existing Scala indexes should be re-indexed (`codegraph index -f`) to benefit. (#750) (Scala)
|
||||
|
||||
@@ -225,8 +225,8 @@ CodeGraph cuts **tokens, tool calls, and wall-clock time on every repo** — acr
|
||||
| **Full-Text Search** | Find code by name instantly across your entire codebase, powered by FTS5 |
|
||||
| **Impact Analysis** | Trace callers, callees, and the full impact radius of any symbol before making changes |
|
||||
| **Always Fresh** | File watcher uses native OS events (FSEvents/inotify/ReadDirectoryChangesW) with debounced auto-sync — the graph stays current as you code, zero config |
|
||||
| **20+ Languages** | TypeScript, JavaScript, Python, Go, Rust, Java, C#, PHP, Ruby, C, C++, Objective-C, Swift, Kotlin, Scala, Dart, Lua, Luau, Svelte, Vue, Liquid, Pascal/Delphi |
|
||||
| **Framework-aware Routes** | Recognizes web-framework routing files and links URL patterns to their handlers across 16 frameworks |
|
||||
| **20+ Languages** | TypeScript, JavaScript, Python, Go, Rust, Java, C#, PHP, Ruby, C, C++, Objective-C, Swift, Kotlin, Scala, Dart, Lua, Luau, Svelte, Vue, Astro, Liquid, Pascal/Delphi |
|
||||
| **Framework-aware Routes** | Recognizes web-framework routing files and links URL patterns to their handlers across 17 frameworks |
|
||||
| **Mixed iOS / React Native / Expo** | Closes cross-language flows that static parsing misses: Swift ↔ ObjC bridging, React Native legacy bridge + TurboModules + Fabric view components, native → JS event emitters, Expo Modules |
|
||||
| **100% Local** | No data leaves your machine. No API keys. No external services. SQLite database only |
|
||||
|
||||
@@ -281,6 +281,7 @@ CodeGraph detects web-framework routing files and emits `route` nodes linked by
|
||||
| **Vapor** | `app.get("x", use: handler)` |
|
||||
| **React Router** / **SvelteKit** | Route component nodes |
|
||||
| **Vue Router** / **Nuxt** | `pages/` file-based routes, `server/api/` endpoints, route middleware |
|
||||
| **Astro** | `src/pages/` file-based routes (`.astro` pages + `.ts` endpoints, `[param]`/`[...rest]` syntax) |
|
||||
|
||||
---
|
||||
|
||||
@@ -636,6 +637,7 @@ is written):
|
||||
| Dart | `.dart` | Full support |
|
||||
| Svelte | `.svelte` | Full support (script extraction, Svelte 5 runes, SvelteKit routes) |
|
||||
| Vue | `.vue` | Full support (script + script-setup extraction, Nuxt page/API/middleware routes) |
|
||||
| Astro | `.astro` | Full support (frontmatter + script extraction, template component/call references, `src/pages/` routes) |
|
||||
| Liquid | `.liquid` | Full support |
|
||||
| Pascal / Delphi | `.pas`, `.dpr`, `.dpk`, `.lpr` | Full support (classes, records, interfaces, enums, DFM/FMX form files) |
|
||||
| Lua | `.lua` | Full support (functions, methods with receivers, local variables, `require` imports, call edges) |
|
||||
@@ -664,12 +666,13 @@ Impact and blast-radius queries are only as good as the dependency graph behind
|
||||
| Dart | flutter/packages | 92.4% |
|
||||
| Svelte / SvelteKit | sveltejs/realworld | 100% |
|
||||
| Vue / Nuxt | nuxt/movies | 93.5% |
|
||||
| Astro | xingwangzhe/stalux | 93.0% |
|
||||
| Lua | nvim-telescope/telescope.nvim | 84.2% |
|
||||
| Luau | dphfox/Fusion | 92.2% |
|
||||
| Liquid | Shopify/dawn | 73.8% |
|
||||
| Pascal / Delphi | PascalCoin | 77.4% |
|
||||
|
||||
Framework routing is validated the same way, on a canonical app per framework: Express 100%, FastAPI 98%, Flask 100%, NestJS 96.8%, Gin 96.5%, Axum 100%, Rocket 93.8%, Vapor 100%, Laravel 92%, Rails 89.6%, React Router 100% — and the convention/reflection-heavy ones at their honest static-analysis ceiling: ASP.NET 83.9%, Spring 83.3%, Drupal 78.9%, Play 76.3%, Django 74.1%. SvelteKit and Vue/Nuxt use file-based routing, so their page/endpoint coverage is the Svelte/SvelteKit (100%) and Vue/Nuxt (93.5%) figures in the table above.
|
||||
Framework routing is validated the same way, on a canonical app per framework: Express 100%, FastAPI 98%, Flask 100%, NestJS 96.8%, Gin 96.5%, Axum 100%, Rocket 93.8%, Vapor 100%, Laravel 92%, Rails 89.6%, React Router 100% — and the convention/reflection-heavy ones at their honest static-analysis ceiling: ASP.NET 83.9%, Spring 83.3%, Drupal 78.9%, Play 76.3%, Django 74.1%. SvelteKit, Vue/Nuxt, and Astro use file-based routing, so their page/endpoint coverage is the Svelte/SvelteKit (100%), Vue/Nuxt (93.5%), and Astro (93.0% — every `src/pages/` file maps to a route node on the two validation repos) figures in the table above.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
|
||||
@@ -5895,6 +5895,218 @@ const value = 42;
|
||||
});
|
||||
});
|
||||
|
||||
describe('Astro Extraction', () => {
|
||||
it('should detect Astro files', () => {
|
||||
expect(detectLanguage('src/pages/index.astro')).toBe('astro');
|
||||
expect(detectLanguage('Layout.astro')).toBe('astro');
|
||||
expect(isLanguageSupported('astro')).toBe(true);
|
||||
});
|
||||
|
||||
it('should extract component node from an .astro file', () => {
|
||||
const code = `---
|
||||
const title = 'Hello';
|
||||
---
|
||||
<h1>{title}</h1>
|
||||
`;
|
||||
const result = extractFromSource('Card.astro', code);
|
||||
|
||||
const componentNode = result.nodes.find((n) => n.kind === 'component');
|
||||
expect(componentNode).toBeDefined();
|
||||
expect(componentNode?.name).toBe('Card');
|
||||
expect(componentNode?.language).toBe('astro');
|
||||
expect(componentNode?.isExported).toBe(true);
|
||||
});
|
||||
|
||||
it('should extract frontmatter symbols with correct line numbers (#768)', () => {
|
||||
const code = `---
|
||||
import { formatDate } from '../utils/format';
|
||||
|
||||
function getIconNode(name: string): string {
|
||||
return name;
|
||||
}
|
||||
|
||||
const { title } = Astro.props;
|
||||
---
|
||||
<span>{title}</span>
|
||||
`;
|
||||
const result = extractFromSource('navs.astro', code);
|
||||
|
||||
// The #768 repro: a function defined in frontmatter must be found
|
||||
const fn = result.nodes.find((n) => n.kind === 'function' && n.name === 'getIconNode');
|
||||
expect(fn).toBeDefined();
|
||||
expect(fn?.language).toBe('astro');
|
||||
expect(fn?.startLine).toBe(4);
|
||||
|
||||
const imp = result.nodes.find((n) => n.kind === 'import');
|
||||
expect(imp).toBeDefined();
|
||||
expect(imp?.startLine).toBe(2);
|
||||
});
|
||||
|
||||
it('should extract exported getStaticPaths from frontmatter', () => {
|
||||
const code = `---
|
||||
export async function getStaticPaths() {
|
||||
return [];
|
||||
}
|
||||
const { slug } = Astro.params;
|
||||
---
|
||||
<p>{slug}</p>
|
||||
`;
|
||||
const result = extractFromSource('[slug].astro', code);
|
||||
|
||||
const fn = result.nodes.find((n) => n.kind === 'function' && n.name === 'getStaticPaths');
|
||||
expect(fn).toBeDefined();
|
||||
expect(fn?.isExported).toBe(true);
|
||||
});
|
||||
|
||||
it('should extract calls from template expressions', () => {
|
||||
const code = `---
|
||||
import { formatDate } from '../utils/format';
|
||||
const date = new Date();
|
||||
---
|
||||
<time>{formatDate(date)}</time>
|
||||
`;
|
||||
const result = extractFromSource('Stamp.astro', code);
|
||||
|
||||
const call = result.unresolvedReferences.find(
|
||||
(ref) => ref.referenceKind === 'calls' && ref.referenceName === 'formatDate' && ref.line === 5
|
||||
);
|
||||
expect(call).toBeDefined();
|
||||
});
|
||||
|
||||
it('should extract calls from a multiline expression opening line', () => {
|
||||
const code = `---
|
||||
const posts = [];
|
||||
---
|
||||
<ul>
|
||||
{posts.map((post) => (
|
||||
<li>{render(post)}</li>
|
||||
))}
|
||||
</ul>
|
||||
`;
|
||||
const result = extractFromSource('List.astro', code);
|
||||
|
||||
const mapCall = result.unresolvedReferences.find(
|
||||
(ref) => ref.referenceKind === 'calls' && ref.referenceName === 'posts.map'
|
||||
);
|
||||
expect(mapCall).toBeDefined();
|
||||
const innerCall = result.unresolvedReferences.find(
|
||||
(ref) => ref.referenceKind === 'calls' && ref.referenceName === 'render'
|
||||
);
|
||||
expect(innerCall).toBeDefined();
|
||||
});
|
||||
|
||||
it('should extract PascalCase component usages from the template', () => {
|
||||
const code = `---
|
||||
import Layout from '../layouts/Layout.astro';
|
||||
import PostCard from '../components/PostCard.astro';
|
||||
---
|
||||
<Layout title="Home">
|
||||
<PostCard />
|
||||
<Fragment slot="head" />
|
||||
<div class="plain-html" />
|
||||
</Layout>
|
||||
`;
|
||||
const result = extractFromSource('index.astro', code);
|
||||
|
||||
const refs = result.unresolvedReferences.filter((r) => r.referenceKind === 'references');
|
||||
const names = refs.map((r) => r.referenceName);
|
||||
expect(names).toContain('Layout');
|
||||
expect(names).toContain('PostCard');
|
||||
// Astro built-ins and lowercase HTML are not component references
|
||||
expect(names).not.toContain('Fragment');
|
||||
expect(names).not.toContain('div');
|
||||
});
|
||||
|
||||
it('should not extract template patterns from frontmatter, script, or style content', () => {
|
||||
const code = `---
|
||||
// <FakeComponent /> inside frontmatter comment
|
||||
const x = { y: maybeCall(1) };
|
||||
---
|
||||
<div>real</div>
|
||||
<script>
|
||||
const z = { w: scriptCall(2) };
|
||||
</script>
|
||||
<style>
|
||||
.a { color: red; }
|
||||
</style>
|
||||
`;
|
||||
const result = extractFromSource('Guard.astro', code);
|
||||
|
||||
const templateRefs = result.unresolvedReferences.filter(
|
||||
(r) => r.referenceKind === 'references' && r.referenceName === 'FakeComponent'
|
||||
);
|
||||
expect(templateRefs).toHaveLength(0);
|
||||
|
||||
// maybeCall/scriptCall come from the delegated TS extraction (once),
|
||||
// not double-counted by the template scanner
|
||||
const maybeCalls = result.unresolvedReferences.filter(
|
||||
(r) => r.referenceName === 'maybeCall' && r.referenceKind === 'calls'
|
||||
);
|
||||
expect(maybeCalls.length).toBeLessThanOrEqual(1);
|
||||
});
|
||||
|
||||
it('should extract <script> block symbols with correct line numbers', () => {
|
||||
const code = `---
|
||||
const a = 1;
|
||||
---
|
||||
<div>hi</div>
|
||||
<script>
|
||||
function trackView(page: string) {
|
||||
console.log(page);
|
||||
}
|
||||
</script>
|
||||
`;
|
||||
const result = extractFromSource('Tracker.astro', code);
|
||||
|
||||
const fn = result.nodes.find((n) => n.kind === 'function' && n.name === 'trackView');
|
||||
expect(fn).toBeDefined();
|
||||
expect(fn?.startLine).toBe(6);
|
||||
expect(fn?.language).toBe('astro');
|
||||
});
|
||||
|
||||
it('should create component node for a frontmatter-less template-only file', () => {
|
||||
const code = `<div>Static content</div>
|
||||
`;
|
||||
const result = extractFromSource('Static.astro', code);
|
||||
|
||||
const componentNode = result.nodes.find((n) => n.kind === 'component');
|
||||
expect(componentNode).toBeDefined();
|
||||
expect(componentNode?.name).toBe('Static');
|
||||
expect(componentNode?.language).toBe('astro');
|
||||
});
|
||||
|
||||
it('should treat an unclosed frontmatter fence as no frontmatter', () => {
|
||||
const code = `---
|
||||
const broken = true;
|
||||
<div>never closed</div>
|
||||
`;
|
||||
const result = extractFromSource('Broken.astro', code);
|
||||
|
||||
// No TS delegation happened (the fence never closes), but the component
|
||||
// node still exists and nothing throws.
|
||||
const componentNode = result.nodes.find((n) => n.kind === 'component');
|
||||
expect(componentNode).toBeDefined();
|
||||
expect(result.nodes.find((n) => n.name === 'broken')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should create containment edges from component to frontmatter nodes', () => {
|
||||
const code = `---
|
||||
const value = 42;
|
||||
---
|
||||
<div>{value}</div>
|
||||
`;
|
||||
const result = extractFromSource('Contained.astro', code);
|
||||
|
||||
const componentNode = result.nodes.find((n) => n.kind === 'component');
|
||||
expect(componentNode).toBeDefined();
|
||||
|
||||
const containEdges = result.edges.filter(
|
||||
(e) => e.source === componentNode!.id && e.kind === 'contains'
|
||||
);
|
||||
expect(containEdges.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Instantiates + Decorates edge extraction', () => {
|
||||
it('emits an instantiates ref for `new Foo()`', () => {
|
||||
const code = `
|
||||
|
||||
@@ -1373,6 +1373,7 @@ func boot(routes: RoutesBuilder) throws {
|
||||
|
||||
import { reactResolver } from '../src/resolution/frameworks/react';
|
||||
import { svelteResolver } from '../src/resolution/frameworks/svelte';
|
||||
import { astroResolver } from '../src/resolution/frameworks/astro';
|
||||
|
||||
describe('reactResolver.extract — React Router', () => {
|
||||
it('extracts a v6 <Route path element={<Comp/>}>', () => {
|
||||
@@ -1428,6 +1429,77 @@ describe('svelteResolver.extract (smoke)', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('astroResolver.extract — src/pages file-based routing', () => {
|
||||
const routeNames = (filePath: string): string[] =>
|
||||
astroResolver.extract!(filePath, '').nodes.filter((n) => n.kind === 'route').map((n) => n.name);
|
||||
|
||||
it('maps index.astro to /', () => {
|
||||
expect(routeNames('src/pages/index.astro')).toEqual(['/']);
|
||||
});
|
||||
|
||||
it('maps nested index and plain pages', () => {
|
||||
expect(routeNames('src/pages/blog/index.astro')).toEqual(['/blog']);
|
||||
expect(routeNames('src/pages/about.astro')).toEqual(['/about']);
|
||||
});
|
||||
|
||||
it('converts [param] and [...rest] syntax', () => {
|
||||
expect(routeNames('src/pages/blog/[slug].astro')).toEqual(['/blog/:slug']);
|
||||
expect(routeNames('src/pages/[...path].astro')).toEqual(['/*path']);
|
||||
});
|
||||
|
||||
it('maps .ts endpoints under src/pages to routes', () => {
|
||||
expect(routeNames('src/pages/api/posts.ts')).toEqual(['/api/posts']);
|
||||
expect(routeNames('src/pages/rss.xml.js')).toEqual(['/rss.xml']);
|
||||
});
|
||||
|
||||
it('excludes underscore-prefixed segments and config files', () => {
|
||||
expect(routeNames('src/pages/_partial.astro')).toEqual([]);
|
||||
expect(routeNames('src/pages/blog/_components/Card.astro')).toEqual([]);
|
||||
expect(routeNames('src/pages/vite.config.ts')).toEqual([]);
|
||||
});
|
||||
|
||||
it('ignores .astro files outside src/pages', () => {
|
||||
expect(routeNames('src/components/Button.astro')).toEqual([]);
|
||||
expect(routeNames('docs/pages/guide.astro')).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('astroResolver.resolve — Astro global and virtual modules', () => {
|
||||
const ctx = {} as never;
|
||||
const baseRef = {
|
||||
fromNodeId: 'component:a',
|
||||
line: 1,
|
||||
column: 0,
|
||||
filePath: 'src/pages/index.astro',
|
||||
language: 'astro',
|
||||
};
|
||||
|
||||
it('claims Astro.* global references as framework-provided', () => {
|
||||
const res = astroResolver.resolve(
|
||||
{ ...baseRef, referenceName: 'Astro.props', referenceKind: 'references' } as never,
|
||||
ctx
|
||||
);
|
||||
expect(res?.resolvedBy).toBe('framework');
|
||||
expect(res?.confidence).toBe(1.0);
|
||||
});
|
||||
|
||||
it('claims astro:content virtual module imports', () => {
|
||||
const res = astroResolver.resolve(
|
||||
{ ...baseRef, referenceName: 'astro:content', referenceKind: 'imports' } as never,
|
||||
ctx
|
||||
);
|
||||
expect(res?.resolvedBy).toBe('framework');
|
||||
});
|
||||
|
||||
it('leaves ordinary names alone', () => {
|
||||
const res = astroResolver.resolve(
|
||||
{ ...baseRef, referenceName: 'astrolabe', referenceKind: 'calls' } as never,
|
||||
{ getNodesByName: () => [] } as never
|
||||
);
|
||||
expect(res).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
// Regression tests: commented-out and docstring route examples must NOT
|
||||
// surface as phantom route nodes. These would have failed before the
|
||||
// strip-comments wiring (the regex would happily scan comments/docstrings).
|
||||
|
||||
@@ -1438,6 +1438,47 @@ func main() {
|
||||
expect(callers.some((c) => c.node.filePath === 'src/Bar.svelte')).toBe(true);
|
||||
});
|
||||
|
||||
it('links an .astro page to the component and TS util it uses (#768)', async () => {
|
||||
// The canonical Astro shape: a page imports a layout/component in
|
||||
// frontmatter and uses it as a template tag; the component's template
|
||||
// calls an imported .ts util. Both hops must produce graph edges or
|
||||
// an Astro project is invisible to callers/impact.
|
||||
fs.mkdirSync(path.join(tempDir, 'src/components'), { recursive: true });
|
||||
fs.mkdirSync(path.join(tempDir, 'src/utils'), { recursive: true });
|
||||
fs.mkdirSync(path.join(tempDir, 'src/pages'), { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(tempDir, 'src/utils/format.ts'),
|
||||
`export function formatDate(d: Date): string { return d.toISOString(); }\n`
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(tempDir, 'src/components/PostCard.astro'),
|
||||
`---\nimport { formatDate } from '../utils/format';\nconst { date } = Astro.props;\n---\n<time>{formatDate(date)}</time>\n`
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(tempDir, 'src/pages/index.astro'),
|
||||
`---\nimport PostCard from '../components/PostCard.astro';\n---\n<PostCard date={new Date()} />\n`
|
||||
);
|
||||
|
||||
cg = await CodeGraph.init(tempDir, { index: true });
|
||||
cg.resolveReferences();
|
||||
|
||||
// Hop 1: page → component (template tag through the frontmatter import)
|
||||
const cardNode = cg
|
||||
.getNodesByKind('component')
|
||||
.find((n) => n.name === 'PostCard' && n.filePath === 'src/components/PostCard.astro');
|
||||
expect(cardNode).toBeDefined();
|
||||
const cardCallers = cg.getCallers(cardNode!.id);
|
||||
expect(cardCallers.some((c) => c.node.filePath === 'src/pages/index.astro')).toBe(true);
|
||||
|
||||
// Hop 2: component template call → .ts util
|
||||
const fmtNode = cg
|
||||
.getNodesByKind('function')
|
||||
.find((n) => n.name === 'formatDate' && n.filePath === 'src/utils/format.ts');
|
||||
expect(fmtNode).toBeDefined();
|
||||
const fmtCallers = cg.getCallers(fmtNode!.id);
|
||||
expect(fmtCallers.some((c) => c.node.filePath === 'src/components/PostCard.astro')).toBe(true);
|
||||
});
|
||||
|
||||
it('resolves a bare directory import (import { x } from "." / "./") to index.ts (#629)', async () => {
|
||||
// `import { helper } from '.'` (or './') must map to the
|
||||
// directory's index.ts before the re-export chase can run. The
|
||||
|
||||
@@ -0,0 +1,365 @@
|
||||
import { Node, Edge, ExtractionResult, ExtractionError, UnresolvedReference } from '../types';
|
||||
import { generateNodeId } from './tree-sitter-helpers';
|
||||
import { TreeSitterExtractor } from './tree-sitter';
|
||||
import { isLanguageSupported } from './grammars';
|
||||
|
||||
/**
|
||||
* Astro built-in components — compiler-provided (`<Fragment>`) or shipped by
|
||||
* `astro:components` (`<Code>`, `<Debug>`), not user code.
|
||||
*/
|
||||
const ASTRO_BUILTIN_COMPONENTS = new Set(['Fragment', 'Code', 'Debug']);
|
||||
|
||||
/**
|
||||
* AstroExtractor - Extracts code relationships from Astro component files
|
||||
*
|
||||
* Astro files are multi-language: a TypeScript frontmatter block fenced by
|
||||
* `---` lines, a JSX-like HTML template, and optional <script>/<style> blocks.
|
||||
* Rather than parsing a full Astro grammar, we extract the frontmatter and
|
||||
* <script> contents and delegate them to the TypeScript TreeSitterExtractor
|
||||
* (Astro processes both as TypeScript by default — no `lang` attr needed).
|
||||
*
|
||||
* Also extracts function calls from template expressions (`{fn(...)}`) and
|
||||
* component usages (`<PascalCase>`) so cross-file edges are captured even
|
||||
* when the only reference lives in markup.
|
||||
*
|
||||
* Every .astro file produces a component node (Astro components are always
|
||||
* importable).
|
||||
*/
|
||||
export class AstroExtractor {
|
||||
private filePath: string;
|
||||
private source: string;
|
||||
private nodes: Node[] = [];
|
||||
private edges: Edge[] = [];
|
||||
private unresolvedReferences: UnresolvedReference[] = [];
|
||||
private errors: ExtractionError[] = [];
|
||||
|
||||
constructor(filePath: string, source: string) {
|
||||
this.filePath = filePath;
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract from Astro source
|
||||
*/
|
||||
extract(): ExtractionResult {
|
||||
const startTime = Date.now();
|
||||
|
||||
try {
|
||||
// Create component node for the .astro file itself
|
||||
const componentNode = this.createComponentNode();
|
||||
|
||||
// Extract and process the frontmatter block (--- fenced, TypeScript)
|
||||
const frontmatter = this.extractFrontmatter();
|
||||
if (frontmatter) {
|
||||
this.processScriptContent(frontmatter, componentNode.id, 'frontmatter');
|
||||
}
|
||||
|
||||
// Extract and process <script> blocks (client-side, TypeScript-capable)
|
||||
for (const block of this.extractScriptBlocks()) {
|
||||
this.processScriptContent(block, componentNode.id, 'script');
|
||||
}
|
||||
|
||||
// Ranges the template scans must skip: frontmatter + <script>/<style>
|
||||
const coveredRanges = this.getCoveredRanges(frontmatter);
|
||||
|
||||
// Extract function calls from template expressions ({fn(...)})
|
||||
this.extractTemplateCalls(componentNode.id, coveredRanges);
|
||||
|
||||
// Extract component usages from template (<ComponentName>)
|
||||
this.extractTemplateComponents(componentNode.id, coveredRanges);
|
||||
} catch (error) {
|
||||
this.errors.push({
|
||||
message: `Astro extraction error: ${error instanceof Error ? error.message : String(error)}`,
|
||||
severity: 'error',
|
||||
code: 'parse_error',
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
nodes: this.nodes,
|
||||
edges: this.edges,
|
||||
unresolvedReferences: this.unresolvedReferences,
|
||||
errors: this.errors,
|
||||
durationMs: Date.now() - startTime,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a component node for the .astro file
|
||||
*/
|
||||
private createComponentNode(): Node {
|
||||
const lines = this.source.split('\n');
|
||||
const fileName = this.filePath.split(/[/\\]/).pop() || this.filePath;
|
||||
const componentName = fileName.replace(/\.astro$/, '');
|
||||
const id = generateNodeId(this.filePath, 'component', componentName, 1);
|
||||
|
||||
const node: Node = {
|
||||
id,
|
||||
kind: 'component',
|
||||
name: componentName,
|
||||
qualifiedName: `${this.filePath}::${componentName}`,
|
||||
filePath: this.filePath,
|
||||
language: 'astro',
|
||||
startLine: 1,
|
||||
endLine: lines.length,
|
||||
startColumn: 0,
|
||||
endColumn: lines[lines.length - 1]?.length || 0,
|
||||
isExported: true, // Astro components are always importable
|
||||
updatedAt: Date.now(),
|
||||
};
|
||||
|
||||
this.nodes.push(node);
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the frontmatter block: the content between the opening `---`
|
||||
* fence (first non-blank line of the file) and the closing `---` fence.
|
||||
* An unclosed fence is treated as "no frontmatter" rather than swallowing
|
||||
* the whole template as TypeScript.
|
||||
*
|
||||
* Returns the content plus its 0-indexed start line, or null.
|
||||
*/
|
||||
private extractFrontmatter(): { content: string; startLine: number; endLine: number } | null {
|
||||
const lines = this.source.split('\n');
|
||||
|
||||
// Opening fence must be the first non-blank line
|
||||
let openIdx = -1;
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const trimmed = lines[i]!.trim();
|
||||
if (trimmed === '') continue;
|
||||
if (trimmed === '---') openIdx = i;
|
||||
break;
|
||||
}
|
||||
if (openIdx === -1) return null;
|
||||
|
||||
// Closing fence
|
||||
let closeIdx = -1;
|
||||
for (let i = openIdx + 1; i < lines.length; i++) {
|
||||
if (lines[i]!.trim() === '---') {
|
||||
closeIdx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (closeIdx === -1) return null;
|
||||
|
||||
return {
|
||||
content: lines.slice(openIdx + 1, closeIdx).join('\n'),
|
||||
startLine: openIdx + 1, // 0-indexed line where content starts
|
||||
endLine: closeIdx, // 0-indexed line of the closing fence
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract <script> blocks from the template portion
|
||||
*/
|
||||
private extractScriptBlocks(): Array<{ content: string; startLine: number }> {
|
||||
const blocks: Array<{ content: string; startLine: number }> = [];
|
||||
|
||||
const scriptRegex = /<script(\s[^>]*)?>(?<content>[\s\S]*?)<\/script>/g;
|
||||
let match;
|
||||
|
||||
while ((match = scriptRegex.exec(this.source)) !== null) {
|
||||
const content = match.groups?.content || match[2] || '';
|
||||
|
||||
// Calculate the 0-indexed line where the content begins. The content
|
||||
// starts right after the opening tag's `>` — its leading `\n` is part
|
||||
// of the content, so relative line 1 sits ON the tag's closing line
|
||||
// (do not add 1 here; that double-counts the embedded newline).
|
||||
const beforeScript = this.source.substring(0, match.index);
|
||||
const scriptTagLine = (beforeScript.match(/\n/g) || []).length;
|
||||
const openingTag = match[0].substring(0, match[0].indexOf('>') + 1);
|
||||
const openingTagLines = (openingTag.match(/\n/g) || []).length;
|
||||
const contentStartLine = scriptTagLine + openingTagLines; // 0-indexed
|
||||
|
||||
blocks.push({ content, startLine: contentStartLine });
|
||||
}
|
||||
|
||||
return blocks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process frontmatter / script content by delegating to TreeSitterExtractor.
|
||||
* Astro treats both as TypeScript by default.
|
||||
*/
|
||||
private processScriptContent(
|
||||
block: { content: string; startLine: number },
|
||||
componentNodeId: string,
|
||||
label: 'frontmatter' | 'script'
|
||||
): void {
|
||||
if (!isLanguageSupported('typescript')) {
|
||||
this.errors.push({
|
||||
message: `Parser for typescript not available, cannot parse Astro ${label} block`,
|
||||
severity: 'warning',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Delegate to TreeSitterExtractor
|
||||
const extractor = new TreeSitterExtractor(this.filePath, block.content, 'typescript');
|
||||
const result = extractor.extract();
|
||||
|
||||
// Offset line numbers from the block back to .astro file positions
|
||||
for (const node of result.nodes) {
|
||||
node.startLine += block.startLine;
|
||||
node.endLine += block.startLine;
|
||||
node.language = 'astro'; // Mark as astro, not TS
|
||||
|
||||
this.nodes.push(node);
|
||||
|
||||
// Add containment edge from component to this node
|
||||
this.edges.push({
|
||||
source: componentNodeId,
|
||||
target: node.id,
|
||||
kind: 'contains',
|
||||
});
|
||||
}
|
||||
|
||||
// Offset edges (they reference line numbers)
|
||||
for (const edge of result.edges) {
|
||||
if (edge.line) {
|
||||
edge.line += block.startLine;
|
||||
}
|
||||
this.edges.push(edge);
|
||||
}
|
||||
|
||||
// Offset unresolved references
|
||||
for (const ref of result.unresolvedReferences) {
|
||||
ref.line += block.startLine;
|
||||
ref.filePath = this.filePath;
|
||||
ref.language = 'astro';
|
||||
this.unresolvedReferences.push(ref);
|
||||
}
|
||||
|
||||
// Carry over errors
|
||||
for (const error of result.errors) {
|
||||
if (error.line) {
|
||||
error.line += block.startLine;
|
||||
}
|
||||
this.errors.push(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Line ranges (0-indexed, inclusive) the template scans must skip:
|
||||
* the frontmatter block and <script>/<style> blocks.
|
||||
*/
|
||||
private getCoveredRanges(
|
||||
frontmatter: { startLine: number; endLine: number } | null
|
||||
): Array<[number, number]> {
|
||||
const coveredRanges: Array<[number, number]> = [];
|
||||
|
||||
if (frontmatter) {
|
||||
// Cover from the opening fence line through the closing fence line
|
||||
coveredRanges.push([frontmatter.startLine - 1, frontmatter.endLine]);
|
||||
}
|
||||
|
||||
const tagRegex = /<(script|style)(\s[^>]*)?>[\s\S]*?<\/\1>/g;
|
||||
let tagMatch;
|
||||
while ((tagMatch = tagRegex.exec(this.source)) !== null) {
|
||||
const startLine = (this.source.substring(0, tagMatch.index).match(/\n/g) || []).length;
|
||||
const endLine = startLine + (tagMatch[0].match(/\n/g) || []).length;
|
||||
coveredRanges.push([startLine, endLine]);
|
||||
}
|
||||
|
||||
return coveredRanges;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract function calls from Astro template expressions.
|
||||
*
|
||||
* Astro templates embed JSX-like expressions (`{formatDate(post.date)}`,
|
||||
* `class:list={cn(...)}`), so calls frequently live in markup rather than
|
||||
* the frontmatter. We scan template lines for `{expression}` groups and
|
||||
* extract call patterns from them. A `{` group left open at end-of-line
|
||||
* (the pervasive `{posts.map((post) => (` pattern) contributes the calls
|
||||
* on its opening line.
|
||||
*/
|
||||
private extractTemplateCalls(
|
||||
componentNodeId: string,
|
||||
coveredRanges: Array<[number, number]>
|
||||
): void {
|
||||
const lines = this.source.split('\n');
|
||||
// Complete groups: {...} — excluding JSX comments ({/* ... */})
|
||||
const exprRegex = /\{([^}/][^}]*)\}/g;
|
||||
// A group opened but not closed on this line
|
||||
const openExprRegex = /\{([^}/][^}]*)$/;
|
||||
|
||||
for (let lineIdx = 0; lineIdx < lines.length; lineIdx++) {
|
||||
if (coveredRanges.some(([start, end]) => lineIdx >= start && lineIdx <= end)) continue;
|
||||
|
||||
const line = lines[lineIdx]!;
|
||||
const exprs: Array<{ text: string; offset: number }> = [];
|
||||
|
||||
let exprMatch;
|
||||
while ((exprMatch = exprRegex.exec(line)) !== null) {
|
||||
exprs.push({ text: exprMatch[1]!, offset: exprMatch.index });
|
||||
}
|
||||
const openMatch = openExprRegex.exec(line.replace(exprRegex, ''));
|
||||
if (openMatch) {
|
||||
exprs.push({ text: openMatch[1]!, offset: line.lastIndexOf('{') });
|
||||
}
|
||||
|
||||
for (const expr of exprs) {
|
||||
// Extract function calls: identifiers followed by (
|
||||
// Matches: cn(...), formatDate(...), obj.method(...)
|
||||
const callRegex = /\b([a-zA-Z_$][\w$.]*)\s*\(/g;
|
||||
let callMatch;
|
||||
while ((callMatch = callRegex.exec(expr.text)) !== null) {
|
||||
const calleeName = callMatch[1]!;
|
||||
// Skip control-flow keywords valid inside expressions
|
||||
if (calleeName === 'if' || calleeName === 'await' || calleeName === 'function') continue;
|
||||
|
||||
this.unresolvedReferences.push({
|
||||
fromNodeId: componentNodeId,
|
||||
referenceName: calleeName,
|
||||
referenceKind: 'calls',
|
||||
line: lineIdx + 1, // 1-indexed
|
||||
column: expr.offset + callMatch.index,
|
||||
filePath: this.filePath,
|
||||
language: 'astro',
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract component usages from the Astro template.
|
||||
*
|
||||
* PascalCase tags like <Layout>, <PostCard /> represent component
|
||||
* instantiations — analogous to function calls in imperative code.
|
||||
* Lowercase tags are native HTML (Astro does not register kebab-case
|
||||
* components the way Vue does, so those are real custom elements and
|
||||
* are skipped).
|
||||
*/
|
||||
private extractTemplateComponents(
|
||||
componentNodeId: string,
|
||||
coveredRanges: Array<[number, number]>
|
||||
): void {
|
||||
const lines = this.source.split('\n');
|
||||
// Opening/self-closing tags (closing tags </Foo> start with </ so won't match)
|
||||
const componentTagRegex = /<([A-Z][a-zA-Z0-9_$]*)\b/g;
|
||||
|
||||
for (let lineIdx = 0; lineIdx < lines.length; lineIdx++) {
|
||||
if (coveredRanges.some(([start, end]) => lineIdx >= start && lineIdx <= end)) continue;
|
||||
|
||||
const line = lines[lineIdx]!;
|
||||
let match;
|
||||
while ((match = componentTagRegex.exec(line)) !== null) {
|
||||
const componentName = match[1]!;
|
||||
if (ASTRO_BUILTIN_COMPONENTS.has(componentName)) continue;
|
||||
|
||||
this.unresolvedReferences.push({
|
||||
fromNodeId: componentNodeId,
|
||||
referenceName: componentName,
|
||||
referenceKind: 'references',
|
||||
line: lineIdx + 1, // 1-indexed
|
||||
column: match.index + 1,
|
||||
filePath: this.filePath,
|
||||
language: 'astro',
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import * as path from 'path';
|
||||
import { Parser, Language as WasmLanguage } from 'web-tree-sitter';
|
||||
import { Language } from '../types';
|
||||
|
||||
export type GrammarLanguage = Exclude<Language, 'svelte' | 'vue' | 'liquid' | 'razor' | 'yaml' | 'twig' | 'xml' | 'properties' | 'unknown'>;
|
||||
export type GrammarLanguage = Exclude<Language, 'svelte' | 'vue' | 'astro' | 'liquid' | 'razor' | 'yaml' | 'twig' | 'xml' | 'properties' | 'unknown'>;
|
||||
|
||||
/**
|
||||
* WASM filename map — maps each language to its .wasm grammar file
|
||||
@@ -93,6 +93,7 @@ export const EXTENSION_MAP: Record<string, Language> = {
|
||||
'.liquid': 'liquid',
|
||||
'.svelte': 'svelte',
|
||||
'.vue': 'vue',
|
||||
'.astro': 'astro',
|
||||
'.pas': 'pascal',
|
||||
'.dpr': 'pascal',
|
||||
'.dpk': 'pascal',
|
||||
@@ -183,6 +184,14 @@ export async function loadGrammarsForLanguages(languages: Language[]): Promise<v
|
||||
await initGrammars();
|
||||
}
|
||||
|
||||
// SFC languages (svelte/vue/astro) have no grammar of their own — their
|
||||
// extractors delegate <script>/frontmatter content to the TS/JS extractor,
|
||||
// so those grammars must be loaded even when no plain .ts/.js file is in
|
||||
// the index set (e.g. a pure-.astro content site).
|
||||
if (languages.some((l) => l === 'svelte' || l === 'vue' || l === 'astro')) {
|
||||
languages = [...languages, 'typescript', 'javascript'];
|
||||
}
|
||||
|
||||
// Deduplicate and filter to languages that have WASM grammars and aren't already loaded
|
||||
const toLoad = [...new Set(languages)].filter(
|
||||
(lang): lang is GrammarLanguage =>
|
||||
@@ -300,6 +309,7 @@ function looksLikeObjc(source: string): boolean {
|
||||
export function isLanguageSupported(language: Language): boolean {
|
||||
if (language === 'svelte') return true; // custom extractor (script block delegation)
|
||||
if (language === 'vue') return true; // custom extractor (script block delegation)
|
||||
if (language === 'astro') return true; // custom extractor (frontmatter/script block delegation)
|
||||
if (language === 'liquid') return true; // custom regex extractor
|
||||
if (language === 'razor') return true; // custom RazorExtractor (.cshtml/.razor markup)
|
||||
if (language === 'yaml') return true; // file-level tracking only; Drupal routing extraction via framework resolver
|
||||
@@ -314,7 +324,7 @@ export function isLanguageSupported(language: Language): boolean {
|
||||
* Check if a grammar has been loaded and is ready for parsing.
|
||||
*/
|
||||
export function isGrammarLoaded(language: Language): boolean {
|
||||
if (language === 'svelte' || language === 'vue' || language === 'liquid' || language === 'razor') return true;
|
||||
if (language === 'svelte' || language === 'vue' || language === 'astro' || language === 'liquid' || language === 'razor') return true;
|
||||
if (language === 'yaml' || language === 'twig') return true; // no WASM grammar needed
|
||||
if (language === 'xml' || language === 'properties') return true; // no WASM grammar needed
|
||||
return languageCache.has(language);
|
||||
@@ -337,7 +347,7 @@ export function isFileLevelOnlyLanguage(language: Language): boolean {
|
||||
* Get all supported languages (those with grammar definitions).
|
||||
*/
|
||||
export function getSupportedLanguages(): Language[] {
|
||||
return [...(Object.keys(WASM_GRAMMAR_FILES) as GrammarLanguage[]), 'svelte', 'vue', 'liquid'];
|
||||
return [...(Object.keys(WASM_GRAMMAR_FILES) as GrammarLanguage[]), 'svelte', 'vue', 'astro', 'liquid'];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -403,6 +413,7 @@ export function getLanguageDisplayName(language: Language): string {
|
||||
dart: 'Dart',
|
||||
svelte: 'Svelte',
|
||||
vue: 'Vue',
|
||||
astro: 'Astro',
|
||||
liquid: 'Liquid',
|
||||
pascal: 'Pascal / Delphi',
|
||||
scala: 'Scala',
|
||||
|
||||
@@ -135,13 +135,16 @@ export class SvelteExtractor {
|
||||
// Detect module script
|
||||
const isModule = /context\s*=\s*["']module["']/.test(attrs);
|
||||
|
||||
// Calculate start line of the script content (line after <script>)
|
||||
// Calculate the 0-indexed line where the content begins. The content
|
||||
// starts right after the opening tag's `>` — its leading `\n` is part
|
||||
// of the content, so relative line 1 sits ON the tag's closing line
|
||||
// (adding 1 here double-counted the embedded newline and shifted every
|
||||
// script-block symbol down a line).
|
||||
const beforeScript = this.source.substring(0, match.index);
|
||||
const scriptTagLine = (beforeScript.match(/\n/g) || []).length;
|
||||
// The content starts on the line after the opening <script> tag
|
||||
const openingTag = match[0].substring(0, match[0].indexOf('>') + 1);
|
||||
const openingTagLines = (openingTag.match(/\n/g) || []).length;
|
||||
const contentStartLine = scriptTagLine + openingTagLines + 1; // 0-indexed line
|
||||
const contentStartLine = scriptTagLine + openingTagLines; // 0-indexed line
|
||||
|
||||
blocks.push({
|
||||
content,
|
||||
|
||||
@@ -24,6 +24,7 @@ import { EXTRACTORS } from './languages';
|
||||
import { LiquidExtractor } from './liquid-extractor';
|
||||
import { RazorExtractor } from './razor-extractor';
|
||||
import { SvelteExtractor } from './svelte-extractor';
|
||||
import { AstroExtractor } from './astro-extractor';
|
||||
import { DfmExtractor } from './dfm-extractor';
|
||||
import { VueExtractor } from './vue-extractor';
|
||||
import { MyBatisExtractor } from './mybatis-extractor';
|
||||
@@ -4752,6 +4753,10 @@ export function extractFromSource(
|
||||
// Use custom extractor for Vue
|
||||
const extractor = new VueExtractor(filePath, source);
|
||||
result = extractor.extract();
|
||||
} else if (detectedLanguage === 'astro') {
|
||||
// Use custom extractor for Astro (frontmatter + template delegation)
|
||||
const extractor = new AstroExtractor(filePath, source);
|
||||
result = extractor.extract();
|
||||
} else if (detectedLanguage === 'liquid') {
|
||||
// Use custom extractor for Liquid
|
||||
const extractor = new LiquidExtractor(filePath, source);
|
||||
|
||||
@@ -143,13 +143,16 @@ export class VueExtractor {
|
||||
// Detect <script setup>
|
||||
const isSetup = /\bsetup\b/.test(attrs);
|
||||
|
||||
// Calculate start line of the script content (line after <script>)
|
||||
// Calculate the 0-indexed line where the content begins. The content
|
||||
// starts right after the opening tag's `>` — its leading `\n` is part
|
||||
// of the content, so relative line 1 sits ON the tag's closing line
|
||||
// (adding 1 here double-counted the embedded newline and shifted every
|
||||
// script-block symbol down a line).
|
||||
const beforeScript = this.source.substring(0, match.index);
|
||||
const scriptTagLine = (beforeScript.match(/\n/g) || []).length;
|
||||
// The content starts on the line after the opening <script> tag
|
||||
const openingTag = match[0].substring(0, match[0].indexOf('>') + 1);
|
||||
const openingTagLines = (openingTag.match(/\n/g) || []).length;
|
||||
const contentStartLine = scriptTagLine + openingTagLines + 1; // 0-indexed line
|
||||
const contentStartLine = scriptTagLine + openingTagLines; // 0-indexed line
|
||||
|
||||
blocks.push({
|
||||
content,
|
||||
|
||||
+2
-2
@@ -1446,7 +1446,7 @@ export class ToolHandler {
|
||||
// names (Class.method / Class::method) — the agent's most precise input,
|
||||
// resolved exactly by findAllSymbols. (The old strip mangled Class.method
|
||||
// into Class, throwing the method away.)
|
||||
const FILE_EXT = /\.(?:java|kt|kts|ts|tsx|js|jsx|mjs|cjs|cs|py|go|rb|php|swift|rs|cpp|cc|cxx|c|h|hpp|scala|lua|dart|vue|svelte)$/i;
|
||||
const FILE_EXT = /\.(?:java|kt|kts|ts|tsx|js|jsx|mjs|cjs|cs|py|go|rb|php|swift|rs|cpp|cc|cxx|c|h|hpp|scala|lua|dart|vue|svelte|astro)$/i;
|
||||
const tokens = [...new Set(
|
||||
query.split(/[\s,()[\]]+/)
|
||||
.map((t) => t.replace(FILE_EXT, '').trim())
|
||||
@@ -1794,7 +1794,7 @@ export class ToolHandler {
|
||||
// agent explicitly named is in the subgraph and its file is scored.
|
||||
const namedSeedIds = new Set<string>();
|
||||
{
|
||||
const FILE_EXT = /\.(?:java|kt|kts|ts|tsx|js|jsx|mjs|cjs|cs|py|go|rb|php|swift|rs|cpp|cc|cxx|c|h|hpp|scala|lua|dart|vue|svelte)$/i;
|
||||
const FILE_EXT = /\.(?:java|kt|kts|ts|tsx|js|jsx|mjs|cjs|cs|py|go|rb|php|swift|rs|cpp|cc|cxx|c|h|hpp|scala|lua|dart|vue|svelte|astro)$/i;
|
||||
const CALLABLE = new Set(['method', 'function', 'component', 'constructor']);
|
||||
const isTestPath = (p: string) => /(^|\/)(tests?|specs?|__tests__|testdata|mocks?|fixtures?)\//i.test(p) || /\.(test|spec)\.[a-z]+$/i.test(p);
|
||||
const bodyLines = (n: Node) => Math.max(0, (n.endLine ?? n.startLine) - n.startLine);
|
||||
|
||||
@@ -0,0 +1,195 @@
|
||||
/**
|
||||
* Astro Framework Resolver
|
||||
*
|
||||
* Handles Astro component references, the `Astro` global, `astro:*` virtual
|
||||
* module imports, and Astro's `src/pages/` file-based routing.
|
||||
*/
|
||||
|
||||
import { Node } from '../../types';
|
||||
import { FrameworkResolver, UnresolvedRef, ResolvedRef, ResolutionContext } from '../types';
|
||||
|
||||
/**
|
||||
* Astro virtual module prefixes — framework-provided, not user code
|
||||
*/
|
||||
const ASTRO_VIRTUAL_MODULES = [
|
||||
'astro:content',
|
||||
'astro:assets',
|
||||
'astro:actions',
|
||||
'astro:env',
|
||||
'astro:i18n',
|
||||
'astro:middleware',
|
||||
'astro:transitions',
|
||||
'astro:components',
|
||||
'astro:schema',
|
||||
];
|
||||
|
||||
export const astroResolver: FrameworkResolver = {
|
||||
name: 'astro',
|
||||
|
||||
detect(context: ResolutionContext): boolean {
|
||||
// Check for astro in package.json
|
||||
const packageJson = context.readFile('package.json');
|
||||
if (packageJson) {
|
||||
try {
|
||||
const pkg = JSON.parse(packageJson);
|
||||
const deps = { ...pkg.dependencies, ...pkg.devDependencies };
|
||||
if (deps.astro) {
|
||||
return true;
|
||||
}
|
||||
} catch {
|
||||
// Invalid JSON
|
||||
}
|
||||
}
|
||||
|
||||
// Check for .astro files in project
|
||||
const allFiles = context.getAllFiles();
|
||||
return allFiles.some((f) => f.endsWith('.astro'));
|
||||
},
|
||||
|
||||
resolve(ref: UnresolvedRef, context: ResolutionContext): ResolvedRef | null {
|
||||
// Pattern 1: the `Astro` global (Astro.props, Astro.url, Astro.params, …)
|
||||
// — runtime-provided in every component's frontmatter. Resolving it as
|
||||
// framework-provided keeps it from name-matching a user symbol named Astro.
|
||||
if (ref.referenceName === 'Astro' || ref.referenceName.startsWith('Astro.')) {
|
||||
return {
|
||||
original: ref,
|
||||
targetNodeId: ref.fromNodeId,
|
||||
confidence: 1.0,
|
||||
resolvedBy: 'framework',
|
||||
};
|
||||
}
|
||||
|
||||
// Pattern 2: astro:* virtual module imports (astro:content, astro:assets, …)
|
||||
if (ref.referenceKind === 'imports' && ref.referenceName.startsWith('astro:')) {
|
||||
if (ASTRO_VIRTUAL_MODULES.some((prefix) => ref.referenceName.startsWith(prefix))) {
|
||||
return {
|
||||
original: ref,
|
||||
targetNodeId: ref.fromNodeId,
|
||||
confidence: 1.0,
|
||||
resolvedBy: 'framework',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Pattern 3: Component references (PascalCase) — resolve to component
|
||||
// nodes. Template tags arrive as `references`, frontmatter expression
|
||||
// usages as `calls`.
|
||||
if (
|
||||
isPascalCase(ref.referenceName) &&
|
||||
(ref.referenceKind === 'references' || ref.referenceKind === 'calls')
|
||||
) {
|
||||
const result = resolveComponent(ref.referenceName, ref.filePath, context);
|
||||
if (result) {
|
||||
return {
|
||||
original: ref,
|
||||
targetNodeId: result,
|
||||
confidence: 0.8,
|
||||
resolvedBy: 'framework',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
|
||||
extract(filePath: string, _content: string) {
|
||||
const nodes: Node[] = [];
|
||||
const now = Date.now();
|
||||
|
||||
// Normalize to forward slashes
|
||||
const normalized = filePath.replace(/\\/g, '/');
|
||||
|
||||
// Astro file-based routing lives under src/pages/ — .astro files are
|
||||
// pages, .ts/.js files are API endpoints. (.md/.mdx pages exist too but
|
||||
// aren't indexed as source.) Underscore-prefixed segments are excluded
|
||||
// from routing by Astro.
|
||||
const pagesMatch = /(?:^|\/)src\/pages\//.exec(normalized);
|
||||
if (pagesMatch && /\.(astro|ts|js|mjs)$/.test(normalized)) {
|
||||
const afterPages = normalized.substring(pagesMatch.index + pagesMatch[0].length);
|
||||
const base = afterPages.split('/').pop() || '';
|
||||
|
||||
// Underscore-prefixed segments are excluded from routing by Astro;
|
||||
// a stray `*.config.*` in a pages dir is never a route.
|
||||
if (
|
||||
!afterPages.split('/').some((segment) => segment.startsWith('_')) &&
|
||||
!/\.config\.[a-z]+$/.test(base)
|
||||
) {
|
||||
const routePath = filePathToAstroRoute(afterPages);
|
||||
|
||||
nodes.push({
|
||||
id: `route:${filePath}:${routePath}:1`,
|
||||
kind: 'route',
|
||||
name: routePath,
|
||||
qualifiedName: `${filePath}::route:${routePath}`,
|
||||
filePath,
|
||||
startLine: 1,
|
||||
endLine: 1,
|
||||
startColumn: 0,
|
||||
endColumn: 0,
|
||||
language: normalized.endsWith('.astro') ? 'astro' : 'typescript',
|
||||
updatedAt: now,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return { nodes, references: [] };
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if string is PascalCase
|
||||
*/
|
||||
function isPascalCase(str: string): boolean {
|
||||
return /^[A-Z][a-zA-Z0-9]*$/.test(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve an Astro component reference using name-based lookup
|
||||
*/
|
||||
function resolveComponent(
|
||||
name: string,
|
||||
fromFile: string,
|
||||
context: ResolutionContext
|
||||
): string | null {
|
||||
// Look for component nodes by name
|
||||
const candidates = context.getNodesByName(name);
|
||||
const components = candidates.filter((n) => n.kind === 'component');
|
||||
|
||||
if (components.length === 0) return null;
|
||||
|
||||
// Prefer same directory
|
||||
const fromDir = fromFile.substring(0, fromFile.lastIndexOf('/'));
|
||||
const sameDir = components.filter((n) => n.filePath.startsWith(fromDir));
|
||||
if (sameDir.length > 0) return sameDir[0]!.id;
|
||||
|
||||
// No positional signal: only an UNAMBIGUOUS name may resolve — picking
|
||||
// components[0] would choose an arbitrary same-named component in a
|
||||
// multi-app monorepo (#764). Ambiguity falls through to the name-matcher,
|
||||
// whose proximity scoring decides.
|
||||
return components.length === 1 ? components[0]!.id : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a path under src/pages/ to an Astro route path.
|
||||
*
|
||||
* blog/[slug].astro -> /blog/:slug
|
||||
* blog/[...path].astro -> /blog/*path
|
||||
* api/posts.ts -> /api/posts
|
||||
* index.astro -> /
|
||||
*/
|
||||
function filePathToAstroRoute(afterPages: string): string {
|
||||
// Remove the extension
|
||||
const withoutExt = afterPages.replace(/\.(astro|ts|js|mjs)$/, '');
|
||||
|
||||
// index files map to their parent path (index -> /, blog/index -> /blog)
|
||||
const withoutIndex = withoutExt.replace(/(^|\/)index$/, '$1').replace(/\/$/, '');
|
||||
|
||||
// Convert Astro param syntax
|
||||
const route = '/' + withoutIndex
|
||||
.replace(/\[\.\.\.([^\]]+)\]/g, '*$1') // [...rest] -> *rest (catch-all)
|
||||
.replace(/\[([^\]]+)\]/g, ':$1'); // [param] -> :param
|
||||
|
||||
if (route === '/') return '/';
|
||||
// Remove trailing slash
|
||||
return route.replace(/\/$/, '');
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import { nestjsResolver } from './nestjs';
|
||||
import { reactResolver } from './react';
|
||||
import { svelteResolver } from './svelte';
|
||||
import { vueResolver } from './vue';
|
||||
import { astroResolver } from './astro';
|
||||
import { djangoResolver, flaskResolver, fastapiResolver } from './python';
|
||||
import { railsResolver } from './ruby';
|
||||
import { springResolver } from './java';
|
||||
@@ -39,6 +40,7 @@ const FRAMEWORK_RESOLVERS: FrameworkResolver[] = [
|
||||
reactResolver,
|
||||
svelteResolver,
|
||||
vueResolver,
|
||||
astroResolver,
|
||||
// Python
|
||||
djangoResolver,
|
||||
flaskResolver,
|
||||
@@ -128,6 +130,7 @@ export { nestjsResolver } from './nestjs';
|
||||
export { reactResolver } from './react';
|
||||
export { svelteResolver } from './svelte';
|
||||
export { vueResolver } from './vue';
|
||||
export { astroResolver } from './astro';
|
||||
export { djangoResolver, flaskResolver, fastapiResolver } from './python';
|
||||
export { railsResolver } from './ruby';
|
||||
export { springResolver } from './java';
|
||||
|
||||
@@ -24,6 +24,7 @@ const EXTENSION_RESOLUTION: Record<string, string[]> = {
|
||||
// `.svelte`/`.vue` file resolve to nothing, so barrel callers vanish (#629).
|
||||
svelte: ['.ts', '.js', '.svelte', '.tsx', '.jsx', '/index.ts', '/index.js', '/index.svelte'],
|
||||
vue: ['.ts', '.js', '.vue', '.tsx', '.jsx', '/index.ts', '/index.js', '/index.vue'],
|
||||
astro: ['.ts', '.js', '.astro', '.tsx', '.jsx', '/index.ts', '/index.js', '/index.astro'],
|
||||
python: ['.py', '/__init__.py'],
|
||||
go: ['.go'],
|
||||
rust: ['.rs', '/mod.rs'],
|
||||
@@ -582,9 +583,10 @@ export function extractImportMappings(
|
||||
|
||||
if (language === 'typescript' || language === 'javascript' || language === 'tsx' || language === 'jsx') {
|
||||
mappings.push(...extractJSImports(content));
|
||||
} else if (language === 'svelte' || language === 'vue') {
|
||||
} else if (language === 'svelte' || language === 'vue' || language === 'astro') {
|
||||
// Svelte/Vue single-file components import via plain ES6 inside their
|
||||
// `<script>` block. Without this, a `.svelte`/`.vue` consumer produces
|
||||
// `<script>` block (Astro: the `---` frontmatter). Without this, a
|
||||
// `.svelte`/`.vue`/`.astro` consumer produces
|
||||
// zero import mappings, so `resolveViaImport` can't run and a barrel
|
||||
// import (`import { Foo } from './lib'`) falls back to name-matching —
|
||||
// which silently fails whenever the re-export alias differs from the
|
||||
|
||||
@@ -83,6 +83,7 @@ export const LANGUAGES = [
|
||||
'dart',
|
||||
'svelte',
|
||||
'vue',
|
||||
'astro',
|
||||
'liquid',
|
||||
'pascal',
|
||||
'scala',
|
||||
|
||||
Reference in New Issue
Block a user