Skip to content

Commit 4c983ba

Browse files
colbymchenryclaude
andcommitted
Add Svelte language support with SvelteKit framework resolver
- Add 'svelte' to Language type, DEFAULT_CONFIG includes, grammars, and config validation - Add SvelteExtractor that extracts <script> blocks and delegates to TS/JS TreeSitterExtractor - Add Svelte framework resolver for runes ($state, $derived, $effect, etc.), store auto-subscriptions, SvelteKit module aliases ($app/*, $env/*, $lib/*), and SvelteKit route detection - Update README to list Svelte and Dart in supported languages Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c034c50 commit 4c983ba

7 files changed

Lines changed: 517 additions & 4 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ Know exactly what breaks before you change it. Trace callers, callees, and the f
128128
<tr>
129129
<td width="33%" valign="top">
130130

131-
### 🌍 15+ Languages
132-
TypeScript, JavaScript, Python, Go, Rust, Java, C#, PHP, Ruby, C, C++, Swift, Kotlin—all with the same API.
131+
### 🌍 17+ Languages
132+
TypeScript, JavaScript, Python, Go, Rust, Java, C#, PHP, Ruby, C, C++, Swift, Kotlin, Dart, Svelte—all with the same API.
133133

134134
</td>
135135
<td width="33%" valign="top">
@@ -598,6 +598,8 @@ The `.codegraph/config.json` file controls indexing behavior:
598598
| C++ | `.cpp`, `.hpp`, `.cc` | Full support |
599599
| Swift | `.swift` | Basic support |
600600
| Kotlin | `.kt` | Basic support |
601+
| Dart | `.dart` | Full support |
602+
| Svelte | `.svelte` | Full support (script extraction, Svelte 5 runes, SvelteKit routes) |
601603

602604
## 🔧 Troubleshooting
603605

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export function validateConfig(config: unknown): config is CodeGraphConfig {
5454
'go',
5555
'rust',
5656
'java',
57+
'svelte',
5758
'unknown',
5859
];
5960
if (!c.languages.every((l) => validLanguages.includes(l as Language))) return false;

src/extraction/grammars.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Parser from 'tree-sitter';
99
import { Language } from '../types';
1010

1111
type GrammarLoader = () => unknown;
12-
type GrammarLanguage = Exclude<Language, 'liquid' | 'unknown'>;
12+
type GrammarLanguage = Exclude<Language, 'svelte' | 'liquid' | 'unknown'>;
1313

1414
/**
1515
* Lazy grammar loaders — each language's native binding is only loaded
@@ -115,6 +115,7 @@ export const EXTENSION_MAP: Record<string, Language> = {
115115
'.kts': 'kotlin',
116116
'.dart': 'dart',
117117
'.liquid': 'liquid',
118+
'.svelte': 'svelte',
118119
};
119120

120121
/**
@@ -186,6 +187,7 @@ export function detectLanguage(filePath: string): Language {
186187
* Check if a language is supported by currently available parsers.
187188
*/
188189
export function isLanguageSupported(language: Language): boolean {
190+
if (language === 'svelte') return true; // custom extractor (script block delegation)
189191
if (language === 'liquid') return true; // custom regex extractor
190192
if (language === 'unknown') return false;
191193
return loadGrammar(language) !== null;
@@ -197,7 +199,7 @@ export function isLanguageSupported(language: Language): boolean {
197199
export function getSupportedLanguages(): Language[] {
198200
const available = (Object.keys(grammarLoaders) as GrammarLanguage[])
199201
.filter((language) => loadGrammar(language) !== null);
200-
return [...available, 'liquid'];
202+
return [...available, 'svelte', 'liquid'];
201203
}
202204

203205
/**
@@ -241,6 +243,7 @@ export function getLanguageDisplayName(language: Language): string {
241243
swift: 'Swift',
242244
kotlin: 'Kotlin',
243245
dart: 'Dart',
246+
svelte: 'Svelte',
244247
liquid: 'Liquid',
245248
unknown: 'Unknown',
246249
};

src/extraction/tree-sitter.ts

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,6 +2287,201 @@ export class LiquidExtractor {
22872287
}
22882288
}
22892289

2290+
/**
2291+
* SvelteExtractor - Extracts code relationships from Svelte component files
2292+
*
2293+
* Svelte files are multi-language (script + template + style). Rather than
2294+
* parsing the full Svelte grammar, we extract the <script> block content
2295+
* and delegate it to the TypeScript/JavaScript TreeSitterExtractor.
2296+
*
2297+
* Every .svelte file produces a component node (Svelte components are always importable).
2298+
*/
2299+
export class SvelteExtractor {
2300+
private filePath: string;
2301+
private source: string;
2302+
private nodes: Node[] = [];
2303+
private edges: Edge[] = [];
2304+
private unresolvedReferences: UnresolvedReference[] = [];
2305+
private errors: ExtractionError[] = [];
2306+
2307+
constructor(filePath: string, source: string) {
2308+
this.filePath = filePath;
2309+
this.source = source;
2310+
}
2311+
2312+
/**
2313+
* Extract from Svelte source
2314+
*/
2315+
extract(): ExtractionResult {
2316+
const startTime = Date.now();
2317+
2318+
try {
2319+
// Create component node for the .svelte file itself
2320+
const componentNode = this.createComponentNode();
2321+
2322+
// Extract and process script blocks
2323+
const scriptBlocks = this.extractScriptBlocks();
2324+
2325+
for (const block of scriptBlocks) {
2326+
this.processScriptBlock(block, componentNode.id);
2327+
}
2328+
} catch (error) {
2329+
captureException(error, { operation: 'svelte-extraction', filePath: this.filePath });
2330+
this.errors.push({
2331+
message: `Svelte extraction error: ${error instanceof Error ? error.message : String(error)}`,
2332+
severity: 'error',
2333+
});
2334+
}
2335+
2336+
return {
2337+
nodes: this.nodes,
2338+
edges: this.edges,
2339+
unresolvedReferences: this.unresolvedReferences,
2340+
errors: this.errors,
2341+
durationMs: Date.now() - startTime,
2342+
};
2343+
}
2344+
2345+
/**
2346+
* Create a component node for the .svelte file
2347+
*/
2348+
private createComponentNode(): Node {
2349+
const lines = this.source.split('\n');
2350+
const fileName = this.filePath.split(/[/\\]/).pop() || this.filePath;
2351+
const componentName = fileName.replace(/\.svelte$/, '');
2352+
const id = generateNodeId(this.filePath, 'component', componentName, 1);
2353+
2354+
const node: Node = {
2355+
id,
2356+
kind: 'component',
2357+
name: componentName,
2358+
qualifiedName: `${this.filePath}::${componentName}`,
2359+
filePath: this.filePath,
2360+
language: 'svelte',
2361+
startLine: 1,
2362+
endLine: lines.length,
2363+
startColumn: 0,
2364+
endColumn: lines[lines.length - 1]?.length || 0,
2365+
isExported: true, // Svelte components are always importable
2366+
updatedAt: Date.now(),
2367+
};
2368+
2369+
this.nodes.push(node);
2370+
return node;
2371+
}
2372+
2373+
/**
2374+
* Extract <script> blocks from the Svelte source
2375+
*/
2376+
private extractScriptBlocks(): Array<{
2377+
content: string;
2378+
startLine: number;
2379+
isModule: boolean;
2380+
isTypeScript: boolean;
2381+
}> {
2382+
const blocks: Array<{
2383+
content: string;
2384+
startLine: number;
2385+
isModule: boolean;
2386+
isTypeScript: boolean;
2387+
}> = [];
2388+
2389+
const scriptRegex = /<script(\s[^>]*)?>(?<content>[\s\S]*?)<\/script>/g;
2390+
let match;
2391+
2392+
while ((match = scriptRegex.exec(this.source)) !== null) {
2393+
const attrs = match[1] || '';
2394+
const content = match.groups?.content || match[2] || '';
2395+
2396+
// Detect TypeScript from lang attribute
2397+
const isTypeScript = /lang\s*=\s*["'](ts|typescript)["']/.test(attrs);
2398+
2399+
// Detect module script
2400+
const isModule = /context\s*=\s*["']module["']/.test(attrs);
2401+
2402+
// Calculate start line of the script content (line after <script>)
2403+
const beforeScript = this.source.substring(0, match.index);
2404+
const scriptTagLine = (beforeScript.match(/\n/g) || []).length;
2405+
// The content starts on the line after the opening <script> tag
2406+
const openingTag = match[0].substring(0, match[0].indexOf('>') + 1);
2407+
const openingTagLines = (openingTag.match(/\n/g) || []).length;
2408+
const contentStartLine = scriptTagLine + openingTagLines + 1; // 0-indexed line
2409+
2410+
blocks.push({
2411+
content,
2412+
startLine: contentStartLine,
2413+
isModule,
2414+
isTypeScript,
2415+
});
2416+
}
2417+
2418+
return blocks;
2419+
}
2420+
2421+
/**
2422+
* Process a script block by delegating to TreeSitterExtractor
2423+
*/
2424+
private processScriptBlock(
2425+
block: { content: string; startLine: number; isModule: boolean; isTypeScript: boolean },
2426+
componentNodeId: string
2427+
): void {
2428+
const scriptLanguage: Language = block.isTypeScript ? 'typescript' : 'javascript';
2429+
2430+
// Check if the script language parser is available
2431+
if (!isLanguageSupported(scriptLanguage)) {
2432+
this.errors.push({
2433+
message: `Parser for ${scriptLanguage} not available, cannot parse Svelte script block`,
2434+
severity: 'warning',
2435+
});
2436+
return;
2437+
}
2438+
2439+
// Delegate to TreeSitterExtractor
2440+
const extractor = new TreeSitterExtractor(this.filePath, block.content, scriptLanguage);
2441+
const result = extractor.extract();
2442+
2443+
// Offset line numbers from script block back to .svelte file positions
2444+
for (const node of result.nodes) {
2445+
node.startLine += block.startLine;
2446+
node.endLine += block.startLine;
2447+
node.language = 'svelte'; // Mark as svelte, not TS/JS
2448+
2449+
this.nodes.push(node);
2450+
2451+
// Add containment edge from component to this node
2452+
this.edges.push({
2453+
source: componentNodeId,
2454+
target: node.id,
2455+
kind: 'contains',
2456+
});
2457+
}
2458+
2459+
// Offset edges (they reference line numbers)
2460+
for (const edge of result.edges) {
2461+
if (edge.line) {
2462+
edge.line += block.startLine;
2463+
}
2464+
this.edges.push(edge);
2465+
}
2466+
2467+
// Offset unresolved references
2468+
for (const ref of result.unresolvedReferences) {
2469+
ref.line += block.startLine;
2470+
ref.filePath = this.filePath;
2471+
ref.language = 'svelte';
2472+
this.unresolvedReferences.push(ref);
2473+
}
2474+
2475+
// Carry over errors
2476+
for (const error of result.errors) {
2477+
if (error.line) {
2478+
error.line += block.startLine;
2479+
}
2480+
this.errors.push(error);
2481+
}
2482+
}
2483+
}
2484+
22902485
/**
22912486
* Extract nodes and edges from source code
22922487
*/
@@ -2297,6 +2492,12 @@ export function extractFromSource(
22972492
): ExtractionResult {
22982493
const detectedLanguage = language || detectLanguage(filePath);
22992494

2495+
// Use custom extractor for Svelte
2496+
if (detectedLanguage === 'svelte') {
2497+
const extractor = new SvelteExtractor(filePath, source);
2498+
return extractor.extract();
2499+
}
2500+
23002501
// Use custom extractor for Liquid
23012502
if (detectedLanguage === 'liquid') {
23022503
const extractor = new LiquidExtractor(filePath, source);

src/resolution/frameworks/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { FrameworkResolver, ResolutionContext } from '../types';
88
import { laravelResolver } from './laravel';
99
import { expressResolver } from './express';
1010
import { reactResolver } from './react';
11+
import { svelteResolver } from './svelte';
1112
import { djangoResolver, flaskResolver, fastapiResolver } from './python';
1213
import { railsResolver } from './ruby';
1314
import { springResolver } from './java';
@@ -25,6 +26,7 @@ const FRAMEWORK_RESOLVERS: FrameworkResolver[] = [
2526
// JavaScript/TypeScript
2627
expressResolver,
2728
reactResolver,
29+
svelteResolver,
2830
// Python
2931
djangoResolver,
3032
flaskResolver,
@@ -88,6 +90,7 @@ export function registerFrameworkResolver(resolver: FrameworkResolver): void {
8890
export { laravelResolver, FACADE_MAPPINGS } from './laravel';
8991
export { expressResolver } from './express';
9092
export { reactResolver } from './react';
93+
export { svelteResolver } from './svelte';
9194
export { djangoResolver, flaskResolver, fastapiResolver } from './python';
9295
export { railsResolver } from './ruby';
9396
export { springResolver } from './java';

0 commit comments

Comments
 (0)