feat: wire up framework route extraction (#89)
* docs: add framework extract wiring plan
* feat(resolution): replace extractNodes with extract() returning nodes and references
* feat(resolution): add getApplicableFrameworks helper for per-language dispatch
* feat(django): emit route nodes and route->view references in extract()
* feat(flask,fastapi): emit route nodes and route->handler references
* feat(express): emit route nodes and route->handler references
* feat(laravel): emit route nodes and route->handler references
* feat(rails): emit route nodes and route->handler references
* feat(spring): emit route nodes and route->handler references
* feat(go): emit route nodes and route->handler references
* feat(rust): emit route nodes and route->handler references
* feat(aspnet): emit route nodes and route->handler references
* feat(swift,vapor): emit route nodes and route->handler references
* chore(react,svelte): migrate resolvers to extract() interface
* feat(extraction): run framework extractors after tree-sitter parse
* docs: document framework route extraction
* feat(strip-comments): add per-language comment stripper for framework extractors
Replaces comment characters and string-literal contents with spaces (not
removal) so source offsets stay valid for downstream regex match index ->
line number conversion. Handles Python triple-quoted docstrings, Ruby
=begin/=end, Rust nested block comments, and the standard //, #, /* */
forms across the supported languages.
This is consumed by framework extract() methods in a follow-up commit so
that commented-out / docstring routing examples don't surface as phantom
route nodes in the graph.
* feat(frameworks): strip comments before regex extraction (prevents phantom routes)
Pipes the per-language stripCommentsForRegex helper into every framework
extract() that scans raw source: django/flask/fastapi (python.ts),
express, laravel, rails, spring, go, rust, aspnet, vapor, plus
swiftui/uikit struct extraction in swift.ts.
Without this, examples like:
# path('/admin/', AdminPanel.as_view())
""" path('/users/', UserListView.as_view()) """
urlpatterns = [path('/real/', RealView.as_view())]
produced 3 phantom route nodes. Now only the real one is extracted.
Each framework gets a regression test in __tests__/frameworks.test.ts
asserting that line-, block-, docstring- and (where relevant)
heredoc-style commented-out routes do not surface as nodes.
---------
Co-authored-by: Colby McHenry <me@colbymchenry.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Colby McHenry
Claude Opus 4.7
parent
5ab81746e8
commit
74327814ee
@@ -6,9 +6,11 @@
|
||||
|
||||
import { Node } from '../../types';
|
||||
import { FrameworkResolver, UnresolvedRef, ResolvedRef, ResolutionContext } from '../types';
|
||||
import { stripCommentsForRegex } from '../strip-comments';
|
||||
|
||||
export const rustResolver: FrameworkResolver = {
|
||||
name: 'rust',
|
||||
languages: ['rust'],
|
||||
|
||||
detect(context: ResolutionContext): boolean {
|
||||
// Check for Cargo.toml (Rust project signature)
|
||||
@@ -71,24 +73,27 @@ export const rustResolver: FrameworkResolver = {
|
||||
return null;
|
||||
},
|
||||
|
||||
extractNodes(filePath: string, content: string): Node[] {
|
||||
extract(filePath, content) {
|
||||
if (!filePath.endsWith('.rs')) return { nodes: [], references: [] };
|
||||
const nodes: Node[] = [];
|
||||
const references: UnresolvedRef[] = [];
|
||||
const now = Date.now();
|
||||
const safe = stripCommentsForRegex(content, 'rust');
|
||||
|
||||
// Extract Actix-web routes
|
||||
// #[get("/path")], #[post("/path")], etc.
|
||||
const actixRoutePattern = /#\[(get|post|put|patch|delete)\s*\(\s*["']([^"']+)["']/g;
|
||||
// Actix-web / Rocket attribute: #[get("/path")] fn handler(..)
|
||||
// Capture the method, path, and the fn identifier that follows.
|
||||
const attrRegex = /#\[(get|post|put|patch|delete|head|options)\s*\(\s*["']([^"']+)["'][^\]]*\)\]/g;
|
||||
let match: RegExpExecArray | null;
|
||||
while ((match = attrRegex.exec(safe)) !== null) {
|
||||
const [, method, routePath] = match;
|
||||
const line = safe.slice(0, match.index).split('\n').length;
|
||||
const upper = method!.toUpperCase();
|
||||
|
||||
let match;
|
||||
while ((match = actixRoutePattern.exec(content)) !== null) {
|
||||
const [, method, path] = match;
|
||||
const line = content.slice(0, match.index).split('\n').length;
|
||||
|
||||
nodes.push({
|
||||
id: `route:${filePath}:${method!.toUpperCase()}:${path}:${line}`,
|
||||
const routeNode: Node = {
|
||||
id: `route:${filePath}:${line}:${upper}:${routePath}`,
|
||||
kind: 'route',
|
||||
name: `${method!.toUpperCase()} ${path}`,
|
||||
qualifiedName: `${filePath}::${method!.toUpperCase()}:${path}`,
|
||||
name: `${upper} ${routePath}`,
|
||||
qualifiedName: `${filePath}::route:${routePath}`,
|
||||
filePath,
|
||||
startLine: line,
|
||||
endLine: line,
|
||||
@@ -96,49 +101,36 @@ export const rustResolver: FrameworkResolver = {
|
||||
endColumn: match[0].length,
|
||||
language: 'rust',
|
||||
updatedAt: now,
|
||||
});
|
||||
}
|
||||
};
|
||||
nodes.push(routeNode);
|
||||
|
||||
// Extract Rocket routes
|
||||
// #[get("/path")], #[post("/path", ...)]
|
||||
const rocketRoutePattern = /#\[(get|post|put|patch|delete|head|options)\s*\(\s*["']([^"']+)["']/g;
|
||||
|
||||
while ((match = rocketRoutePattern.exec(content)) !== null) {
|
||||
const [, method, path] = match;
|
||||
const line = content.slice(0, match.index).split('\n').length;
|
||||
|
||||
// Avoid duplicates from actix pattern
|
||||
const routeId = `route:${filePath}:${method!.toUpperCase()}:${path}:${line}`;
|
||||
if (!nodes.some((n) => n.id === routeId)) {
|
||||
nodes.push({
|
||||
id: routeId,
|
||||
kind: 'route',
|
||||
name: `${method!.toUpperCase()} ${path}`,
|
||||
qualifiedName: `${filePath}::${method!.toUpperCase()}:${path}`,
|
||||
const tail = safe.slice(match.index + match[0].length);
|
||||
const fnMatch = tail.match(/\n\s*(?:pub\s+)?(?:async\s+)?fn\s+(\w+)/);
|
||||
if (fnMatch) {
|
||||
references.push({
|
||||
fromNodeId: routeNode.id,
|
||||
referenceName: fnMatch[1]!,
|
||||
referenceKind: 'references',
|
||||
line,
|
||||
column: 0,
|
||||
filePath,
|
||||
startLine: line,
|
||||
endLine: line,
|
||||
startColumn: 0,
|
||||
endColumn: match[0].length,
|
||||
language: 'rust',
|
||||
updatedAt: now,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Extract Axum routes (method chaining style)
|
||||
// .route("/path", get(handler))
|
||||
const axumRoutePattern = /\.route\s*\(\s*["']([^"']+)["']\s*,\s*(get|post|put|patch|delete)/g;
|
||||
// Axum: .route("/path", get(handler))
|
||||
const axumRegex = /\.route\s*\(\s*"([^"]+)"\s*,\s*(get|post|put|patch|delete)\s*\(\s*(\w+)/g;
|
||||
while ((match = axumRegex.exec(safe)) !== null) {
|
||||
const [, routePath, method, handler] = match;
|
||||
const line = safe.slice(0, match.index).split('\n').length;
|
||||
const upper = method!.toUpperCase();
|
||||
|
||||
while ((match = axumRoutePattern.exec(content)) !== null) {
|
||||
const [, path, method] = match;
|
||||
const line = content.slice(0, match.index).split('\n').length;
|
||||
|
||||
nodes.push({
|
||||
id: `route:${filePath}:${method!.toUpperCase()}:${path}:${line}`,
|
||||
const routeNode: Node = {
|
||||
id: `route:${filePath}:${line}:${upper}:${routePath}`,
|
||||
kind: 'route',
|
||||
name: `${method!.toUpperCase()} ${path}`,
|
||||
qualifiedName: `${filePath}::${method!.toUpperCase()}:${path}`,
|
||||
name: `${upper} ${routePath}`,
|
||||
qualifiedName: `${filePath}::route:${routePath}`,
|
||||
filePath,
|
||||
startLine: line,
|
||||
endLine: line,
|
||||
@@ -146,10 +138,21 @@ export const rustResolver: FrameworkResolver = {
|
||||
endColumn: match[0].length,
|
||||
language: 'rust',
|
||||
updatedAt: now,
|
||||
};
|
||||
nodes.push(routeNode);
|
||||
|
||||
references.push({
|
||||
fromNodeId: routeNode.id,
|
||||
referenceName: handler!,
|
||||
referenceKind: 'references',
|
||||
line,
|
||||
column: 0,
|
||||
filePath,
|
||||
language: 'rust',
|
||||
});
|
||||
}
|
||||
|
||||
return nodes;
|
||||
return { nodes, references };
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user