@dimassetoid/septum-mcp 0.0.0-stage → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +124 -2
  3. package/bin/septum.ts +4 -0
  4. package/package.json +70 -4
  5. package/scripts/postinstall.ts +25 -0
  6. package/src/cli/commands/check.ts +122 -0
  7. package/src/cli/commands/hook.ts +268 -0
  8. package/src/cli/commands/init.ts +553 -0
  9. package/src/cli/commands/serve.ts +5 -0
  10. package/src/cli/commands/sync.ts +41 -0
  11. package/src/cli/commands/topology.ts +161 -0
  12. package/src/cli/index.ts +167 -0
  13. package/src/cli/scaffold/harness-config.ts +621 -0
  14. package/src/cli/templates/hooks.ts +75 -0
  15. package/src/cli/templates/rules.ts +50 -0
  16. package/src/cli/templates/skill.ts +51 -0
  17. package/src/cli/ui/prompts.ts +122 -0
  18. package/src/core/boundary/evaluator.ts +442 -0
  19. package/src/core/boundary/staged-auditor.ts +58 -0
  20. package/src/core/config/loader.ts +122 -0
  21. package/src/core/config/schema.ts +69 -0
  22. package/src/core/database/client.ts +60 -0
  23. package/src/core/database/repositories/dependency.repository.ts +712 -0
  24. package/src/core/database/repositories/domain.repository.ts +330 -0
  25. package/src/core/database/repositories/environment.repository.ts +207 -0
  26. package/src/core/database/repositories/file.repository.ts +117 -0
  27. package/src/core/database/repositories/meta.repository.ts +33 -0
  28. package/src/core/database/repositories/symbol.repository.ts +508 -0
  29. package/src/core/database/repository.ts +303 -0
  30. package/src/core/database/schema.ts +154 -0
  31. package/src/core/discovery/topology-detector.ts +805 -0
  32. package/src/core/ingestion/hasher.ts +5 -0
  33. package/src/core/ingestion/pipeline.ts +623 -0
  34. package/src/core/parser/behavioral-analyzer.ts +119 -0
  35. package/src/core/parser/boundary-tracker.ts +443 -0
  36. package/src/core/parser/extractors/base.ts +67 -0
  37. package/src/core/parser/extractors/blade.ts +159 -0
  38. package/src/core/parser/extractors/csharp.ts +244 -0
  39. package/src/core/parser/extractors/django-semantic.ts +687 -0
  40. package/src/core/parser/extractors/elysia-semantic.ts +538 -0
  41. package/src/core/parser/extractors/express-semantic.ts +444 -0
  42. package/src/core/parser/extractors/fastapi-semantic.ts +736 -0
  43. package/src/core/parser/extractors/fastify-semantic.ts +774 -0
  44. package/src/core/parser/extractors/flask-semantic.ts +515 -0
  45. package/src/core/parser/extractors/frontend.ts +149 -0
  46. package/src/core/parser/extractors/go-semantic.ts +611 -0
  47. package/src/core/parser/extractors/go.ts +491 -0
  48. package/src/core/parser/extractors/hono-semantic.ts +567 -0
  49. package/src/core/parser/extractors/java.ts +225 -0
  50. package/src/core/parser/extractors/laravel-semantic.ts +1122 -0
  51. package/src/core/parser/extractors/mcp-cli-semantic.ts +368 -0
  52. package/src/core/parser/extractors/nestjs-semantic.ts +543 -0
  53. package/src/core/parser/extractors/php.ts +569 -0
  54. package/src/core/parser/extractors/python.ts +498 -0
  55. package/src/core/parser/extractors/registry.ts +78 -0
  56. package/src/core/parser/extractors/ruby.ts +234 -0
  57. package/src/core/parser/extractors/rust.ts +286 -0
  58. package/src/core/parser/extractors/semantic-extractor.interface.ts +22 -0
  59. package/src/core/parser/extractors/symfony-semantic.ts +354 -0
  60. package/src/core/parser/extractors/typescript.ts +826 -0
  61. package/src/core/parser/language-capabilities.ts +71 -0
  62. package/src/core/parser/schema/providers/laravel-migration-provider.ts +216 -0
  63. package/src/core/parser/schema/providers/prisma-schema-provider.ts +90 -0
  64. package/src/core/parser/schema/schema-provider-registry.ts +43 -0
  65. package/src/core/parser/schema/schema-provider.interface.ts +33 -0
  66. package/src/core/parser/tree-sitter.ts +63 -0
  67. package/src/core/parser/wasm-assets.ts +25 -0
  68. package/src/core/parser/wasm-loader.ts +274 -0
  69. package/src/core/resolver/call-graph-tracer.ts +724 -0
  70. package/src/core/resolver/import-resolver.ts +437 -0
  71. package/src/core/resolver/module-resolver.ts +482 -0
  72. package/src/core/resolver/path-resolver.ts +200 -0
  73. package/src/core/resolver/symbol-locator.ts +276 -0
  74. package/src/core/resolver/symbol-reference.ts +53 -0
  75. package/src/core/resolver/vertical-slice-tracer.ts +366 -0
  76. package/src/core/resources/ignore.ts +123 -0
  77. package/src/core/resources/limits.ts +14 -0
  78. package/src/core/resources/source-files.ts +62 -0
  79. package/src/core/resources/work-queue.ts +74 -0
  80. package/src/core/session/session-manager.ts +107 -0
  81. package/src/core/telemetry/telemetry.ts +97 -0
  82. package/src/core/topology/environment-detector.ts +973 -0
  83. package/src/core/topology/types.ts +83 -0
  84. package/src/index.ts +21 -0
  85. package/src/mcp/schemas.ts +1105 -0
  86. package/src/mcp/server.ts +774 -0
  87. package/src/mcp/tools/boundary.ts +17 -0
  88. package/src/mcp/tools/check-boundary.ts +387 -0
  89. package/src/mcp/tools/domain.ts +40 -0
  90. package/src/mcp/tools/feature.ts +67 -0
  91. package/src/mcp/tools/get-domain-catalog.ts +226 -0
  92. package/src/mcp/tools/get-environment-topology.ts +138 -0
  93. package/src/mcp/tools/get-feature-context.ts +224 -0
  94. package/src/mcp/tools/get-symbol-hotspots.ts +54 -0
  95. package/src/mcp/tools/get-symbol-impact.ts +64 -0
  96. package/src/mcp/tools/get-symbol.ts +175 -0
  97. package/src/mcp/tools/locate-symbol.ts +196 -0
  98. package/src/mcp/tools/register-domain.ts +75 -0
  99. package/src/mcp/tools/register-feature.ts +110 -0
  100. package/src/mcp/tools/slice.ts +16 -0
  101. package/src/mcp/tools/symbol.ts +93 -0
  102. package/src/mcp/tools/trace-vertical-slice.ts +80 -0
  103. package/src/mcp/workspace-manager.ts +159 -0
  104. package/src/mcp/workspace-watcher.ts +202 -0
  105. package/src/types/index.ts +456 -0
  106. package/src/types/wasm.d.ts +4 -0
  107. package/src/utils/version-check.ts +209 -0
  108. package/src/version.ts +1 -0
@@ -0,0 +1,826 @@
1
+ import { getLanguageCapability } from "../language-capabilities.ts";
2
+ import type Parser from "web-tree-sitter";
3
+ import type {
4
+ ExtractedDependency,
5
+ ExtractedSymbol,
6
+ ParsedFileAST,
7
+ SymbolKind,
8
+ Visibility,
9
+ } from "../../../types/index.ts";
10
+ import {
11
+ blankCommentsAndStrings,
12
+ calculateControlFlowNesting,
13
+ findBraceBlockEnd,
14
+ } from "../boundary-tracker.ts";
15
+ import { calculateAstControlFlowNesting, getNodeHeader, parseSourceWithTreeSitter } from "../wasm-loader.ts";
16
+ import type { CodeExtractor } from "./base.ts";
17
+
18
+ const TS_CONTROL_FLOW_NODES = new Set([
19
+ "if_statement",
20
+ "for_statement",
21
+ "for_in_statement",
22
+ "while_statement",
23
+ "do_statement",
24
+ "switch_statement",
25
+ "try_statement",
26
+ "catch_clause",
27
+ "ternary_expression",
28
+ ]);
29
+
30
+ const TS_FUNCTION_BOUNDARY_NODES = new Set([
31
+ "function_declaration",
32
+ "generator_function_declaration",
33
+ "function_expression",
34
+ "arrow_function",
35
+ "method_definition",
36
+ ]);
37
+
38
+ export class TypeScriptExtractor implements CodeExtractor {
39
+ public canHandle(filePath: string): boolean {
40
+ return getLanguageCapability(filePath)?.extractor === "typescript";
41
+ }
42
+
43
+ public async extract(filePath: string, content: string): Promise<ParsedFileAST> {
44
+ const langKey = filePath;
45
+ const treeSitterResult = await parseSourceWithTreeSitter(langKey, content, (rootNode) =>
46
+ this.extractFromAst(rootNode, content),
47
+ () => this.extractFallback(content)
48
+ );
49
+
50
+ if (treeSitterResult) {
51
+ return treeSitterResult;
52
+ }
53
+
54
+ return { ...this.extractFallback(content), status: "degraded" };
55
+ }
56
+
57
+ private extractFallback(content: string): ParsedFileAST {
58
+ const symbols: ExtractedSymbol[] = [];
59
+ const dependencies: ExtractedDependency[] = [];
60
+
61
+ const rawLines = content.split("\n");
62
+ const cleanContent = blankCommentsAndStrings(content);
63
+ const cleanLines = cleanContent.split("\n");
64
+
65
+ const importRegex =
66
+ /^\s*(?:(?:import|export)\s+(?:type\s+)?(?:([\w*\s{},]+)\s+from\s+)?['"]([^'"]+)['"]|(?:const|let|var)\s+([\w\s{},:$]+)\s*=\s*require\s*\(\s*['"]([^'"]+)['"]\s*\)|require\s*\(\s*['"]([^'"]+)['"]\s*\))/;
67
+
68
+ for (let i = 0; i < rawLines.length; i++) {
69
+ if (!cleanLines[i]?.trim()) continue;
70
+ const line = rawLines[i];
71
+ const match = importRegex.exec(line);
72
+ if (match) {
73
+ const target = match[2] || match[4] || match[5];
74
+ const clause = match[1] || match[3] || "";
75
+ if (target) {
76
+ const importedSymbols = clause
77
+ .replace(/[{}*]/g, " ")
78
+ .split(/,|\bas\b/)
79
+ .map((s) => s.trim())
80
+ .filter((s) => /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(s) && s !== "type");
81
+ const isExternal = !target.startsWith(".") && !target.startsWith("/");
82
+ dependencies.push({
83
+ target,
84
+ statement: line.trim(),
85
+ line_number: i + 1,
86
+ is_external: isExternal,
87
+ ...(importedSymbols.length > 0 ? { imported_symbols: Array.from(new Set(importedSymbols)) } : {}),
88
+ });
89
+ }
90
+ }
91
+ }
92
+
93
+ // 2. Extract Symbols using cleanLines + brace depth tracking
94
+ let currentClass: string | null = null;
95
+ let currentClassDepth = 0;
96
+ let braceDepth = 0;
97
+
98
+ for (let i = 0; i < cleanLines.length; i++) {
99
+ const cleanTrimmed = cleanLines[i].trim();
100
+ const rawTrimmed = rawLines[i].trim();
101
+ const lineNum = i + 1;
102
+
103
+ if (currentClass && braceDepth < currentClassDepth) {
104
+ currentClass = null;
105
+ currentClassDepth = 0;
106
+ }
107
+
108
+ if (cleanTrimmed && braceDepth === 0) {
109
+ const classMatch =
110
+ /^(?:export\s+)?(?:default\s+)?(?:abstract\s+)?\b(class|interface)\s+([A-Za-z_$][A-Za-z0-9_$]*)\b/.exec(
111
+ cleanTrimmed
112
+ );
113
+ if (classMatch) {
114
+ currentClass = classMatch[2];
115
+ currentClassDepth = braceDepth + 1;
116
+ const endLine = findBraceBlockEnd(cleanLines, i);
117
+ symbols.push({
118
+ name: classMatch[2],
119
+ kind: classMatch[1] as "class" | "interface",
120
+ signature: rawTrimmed.replace(/\{$/, "").trim(),
121
+ visibility: "public",
122
+ line_start: lineNum,
123
+ line_end: endLine,
124
+ line_count: Math.max(1, endLine - lineNum + 1),
125
+ });
126
+ } else {
127
+ const typeMatch = /^(?:export\s+)?\btype\s+([A-Za-z_$][A-Za-z0-9_$]*)\b\s*=/.exec(cleanTrimmed);
128
+ if (typeMatch) {
129
+ const endLine = cleanTrimmed.endsWith(";") ? lineNum : findBraceBlockEnd(cleanLines, i);
130
+ symbols.push({
131
+ name: typeMatch[1],
132
+ kind: "type",
133
+ signature: rawTrimmed.replace(/;$/, "").trim(),
134
+ visibility: "public",
135
+ line_start: lineNum,
136
+ line_end: endLine,
137
+ line_count: Math.max(1, endLine - lineNum + 1),
138
+ });
139
+ }
140
+
141
+ const enumMatch = /^(?:export\s+)?(?:const\s+)?\benum\s+([A-Za-z_$][A-Za-z0-9_$]*)\b/.exec(cleanTrimmed);
142
+ if (enumMatch) {
143
+ const endLine = findBraceBlockEnd(cleanLines, i);
144
+ symbols.push({
145
+ name: enumMatch[1],
146
+ kind: "enum",
147
+ signature: rawTrimmed.replace(/\{$/, "").trim(),
148
+ visibility: "public",
149
+ line_start: lineNum,
150
+ line_end: endLine,
151
+ line_count: Math.max(1, endLine - lineNum + 1),
152
+ });
153
+ }
154
+
155
+ const fnMatch =
156
+ /^(?:export\s+)?(?:default\s+)?(?:async\s+)?\bfunction\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*\(([^)]*)\)(?:\s*:\s*([^;{]+))?/.exec(
157
+ cleanTrimmed
158
+ );
159
+ if (fnMatch) {
160
+ const name = fnMatch[1];
161
+ const params = fnMatch[2];
162
+ const retType = fnMatch[3] ? `: ${fnMatch[3].trim()}` : "";
163
+ const endLine = findBraceBlockEnd(cleanLines, i);
164
+ const nestingDepth = calculateControlFlowNesting(rawLines, i, endLine - 1);
165
+ symbols.push({
166
+ name,
167
+ kind: "function",
168
+ signature: `${name}(${params})${retType}`,
169
+ visibility: "public",
170
+ line_start: lineNum,
171
+ line_end: endLine,
172
+ line_count: Math.max(1, endLine - lineNum + 1),
173
+ nesting_depth: nestingDepth,
174
+ });
175
+ }
176
+
177
+ const varMatch =
178
+ /^(?:export\s+)?\b(const|let|var)\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*(?::\s*([^=;]+))?\s*=\s*(.*)$/.exec(
179
+ cleanTrimmed
180
+ );
181
+ if (varMatch) {
182
+ const declKind = varMatch[1];
183
+ const varName = varMatch[2];
184
+ const typeStr = varMatch[3] ? `: ${varMatch[3].trim()}` : "";
185
+ const rhs = varMatch[4].trim();
186
+ const isArrowOrFn = /^(?:async\s+)?(?:\([^)]*\)|[A-Za-z_$][A-Za-z0-9_$]*)\s*=>|^function\b/.test(rhs);
187
+ symbols.push({
188
+ name: varName,
189
+ kind: isArrowOrFn ? "function" : declKind === "const" ? "constant" : "variable",
190
+ signature: `${declKind} ${varName}${typeStr}`,
191
+ visibility: cleanTrimmed.startsWith("export ") ? "public" : "private",
192
+ line_start: lineNum,
193
+ line_end: lineNum,
194
+ line_count: 1,
195
+ });
196
+ }
197
+ }
198
+ } else if (cleanTrimmed && currentClass && braceDepth === currentClassDepth) {
199
+ const methodMatch =
200
+ /^(?:(public|protected|private)\s+)?(?:static\s+)?(?:readonly\s+)?(?:async\s+)?\b([A-Za-z_$][A-Za-z0-9_$]*)\s*\(([^)]*)\)(?:\s*:\s*([^;{]+))?/.exec(
201
+ cleanTrimmed
202
+ );
203
+ if (
204
+ methodMatch &&
205
+ !["if", "for", "while", "switch", "catch", "function", "return"].includes(methodMatch[2])
206
+ ) {
207
+ const vis = (methodMatch[1] as Visibility) || "public";
208
+ const name = methodMatch[2];
209
+ const params = methodMatch[3];
210
+ const retType = methodMatch[4] ? `: ${methodMatch[4].trim()}` : "";
211
+ const endLine = findBraceBlockEnd(cleanLines, i);
212
+ const nestingDepth = calculateControlFlowNesting(rawLines, i, endLine - 1);
213
+ symbols.push({
214
+ name: `${currentClass}::${name}`,
215
+ kind: "method",
216
+ signature: `${vis} ${name}(${params})${retType}`,
217
+ visibility: vis,
218
+ line_start: lineNum,
219
+ line_end: endLine,
220
+ line_count: Math.max(1, endLine - lineNum + 1),
221
+ nesting_depth: nestingDepth,
222
+ });
223
+ } else {
224
+ const propMatch =
225
+ /^(?:(public|protected|private)\s+)?(?:static\s+)?(?:readonly\s+)?\b([A-Za-z_$][A-Za-z0-9_$]*)\s*(?:\?|\!)?\s*(?::\s*([^;={]+))?(?:\s*=[^;]+)?;\s*$/.exec(
226
+ cleanTrimmed
227
+ );
228
+ if (
229
+ propMatch &&
230
+ !["return", "const", "let", "var", "throw", "import", "export", "continue", "break"].includes(
231
+ propMatch[2]
232
+ )
233
+ ) {
234
+ const vis = (propMatch[1] as Visibility) || "public";
235
+ const propName = propMatch[2];
236
+ const typeStr = propMatch[3] ? `: ${propMatch[3].trim()}` : "";
237
+ symbols.push({
238
+ name: `${currentClass}::${propName}`,
239
+ kind: "property",
240
+ signature: `${vis} ${propName}${typeStr}`,
241
+ visibility: vis,
242
+ line_start: lineNum,
243
+ line_end: lineNum,
244
+ line_count: 1,
245
+ });
246
+ }
247
+ }
248
+ }
249
+
250
+ for (const ch of cleanLines[i]) {
251
+ if (ch === "{") braceDepth++;
252
+ else if (ch === "}") braceDepth = Math.max(0, braceDepth - 1);
253
+ }
254
+ if (currentClass && braceDepth < currentClassDepth) {
255
+ currentClass = null;
256
+ currentClassDepth = 0;
257
+ }
258
+ }
259
+
260
+ return { symbols, dependencies };
261
+ }
262
+
263
+ private extractFromAst(rootNode: Parser.SyntaxNode, content: string): ParsedFileAST {
264
+ const symbols: ExtractedSymbol[] = [];
265
+ const dependencies: ExtractedDependency[] = [];
266
+ const lines = content.split("\n");
267
+
268
+ const stripQuotes = (raw: string): string => raw.replace(/^['"`]|['"`]$/g, "");
269
+
270
+ const walkDependencies = (node: Parser.SyntaxNode): void => {
271
+ if (node.type === "import_statement") {
272
+ const sourceNode =
273
+ node.childForFieldName("source") ??
274
+ node.namedChildren.find((c) => c.type === "string");
275
+ if (sourceNode) {
276
+ const target = stripQuotes(sourceNode.text.trim());
277
+ if (target) {
278
+ const importedSymbols = this.extractImportedSymbols(node);
279
+ const isExternal = !target.startsWith(".") && !target.startsWith("/");
280
+ dependencies.push({
281
+ target,
282
+ statement: (lines[node.startPosition.row] ?? node.text).trim(),
283
+ line_number: node.startPosition.row + 1,
284
+ is_external: isExternal,
285
+ ...(importedSymbols.length > 0 ? { imported_symbols: importedSymbols } : {}),
286
+ });
287
+ }
288
+ }
289
+ return;
290
+ }
291
+
292
+ if (node.type === "export_statement") {
293
+ const sourceNode =
294
+ node.childForFieldName("source") ??
295
+ node.namedChildren.find((c) => c.type === "string");
296
+ if (sourceNode) {
297
+ const target = stripQuotes(sourceNode.text.trim());
298
+ if (target) {
299
+ const importedSymbols = this.extractExportedSymbols(node);
300
+ const isExternal = !target.startsWith(".") && !target.startsWith("/");
301
+ dependencies.push({
302
+ target,
303
+ statement: (lines[node.startPosition.row] ?? node.text).trim(),
304
+ line_number: node.startPosition.row + 1,
305
+ is_external: isExternal,
306
+ ...(importedSymbols.length > 0 ? { imported_symbols: importedSymbols } : {}),
307
+ });
308
+ }
309
+ }
310
+ return;
311
+ }
312
+
313
+ if (node.type === "call_expression") {
314
+ const fnNode = node.childForFieldName("function") ?? node.namedChildren[0];
315
+ const argsNode = node.childForFieldName("arguments") ?? node.namedChildren.find((c) => c.type === "arguments");
316
+ if (fnNode && (fnNode.text === "require" || fnNode.type === "import") && argsNode) {
317
+ const firstArg = argsNode.namedChildren[0];
318
+ if (firstArg && (firstArg.type !== "string" && firstArg.type !== "template_string" || firstArg.text.includes("${"))) {
319
+ dependencies.push({ target: `dynamic:${node.text}`, statement: node.text, line_number: node.startPosition.row + 1, is_external: false });
320
+ }
321
+ if (firstArg && (firstArg.type === "string" || firstArg.type === "template_string")) {
322
+ const target = stripQuotes(firstArg.text.trim());
323
+ if (target && !target.includes("${")) {
324
+ const importedSymbols = this.extractRequireImportedSymbols(node);
325
+ const isExternal = !target.startsWith(".") && !target.startsWith("/");
326
+ dependencies.push({
327
+ target,
328
+ statement: (lines[node.startPosition.row] ?? node.text).trim(),
329
+ line_number: node.startPosition.row + 1,
330
+ is_external: isExternal,
331
+ ...(importedSymbols.length > 0 ? { imported_symbols: importedSymbols } : {}),
332
+ });
333
+ }
334
+ }
335
+ }
336
+ }
337
+
338
+ for (const child of node.namedChildren) {
339
+ walkDependencies(child);
340
+ }
341
+ };
342
+
343
+ walkDependencies(rootNode);
344
+
345
+ const processTopLevelNode = (stmtNode: Parser.SyntaxNode, isExported: boolean, outerStartRow?: number): void => {
346
+ const startRow = outerStartRow ?? stmtNode.startPosition.row;
347
+ const endRow = stmtNode.endPosition.row;
348
+ const lineStart = startRow + 1;
349
+ const lineEnd = Math.max(lineStart, endRow + 1);
350
+ const lineCount = Math.max(1, lineEnd - lineStart + 1);
351
+
352
+ if (stmtNode.type === "export_statement") {
353
+ const decl =
354
+ stmtNode.childForFieldName("declaration") ??
355
+ stmtNode.namedChildren.find((c) => c.type !== "decorator" && c.type !== "export_clause");
356
+ if (decl) {
357
+ processTopLevelNode(decl, true, stmtNode.startPosition.row);
358
+ return;
359
+ }
360
+
361
+ const sourceNode =
362
+ stmtNode.childForFieldName("source") ??
363
+ stmtNode.namedChildren.find((c) => c.type === "string");
364
+ if (!sourceNode) {
365
+ const exportClause = stmtNode.namedChildren.find((c) => c.type === "export_clause");
366
+ if (exportClause) {
367
+ for (const spec of exportClause.namedChildren) {
368
+ if (spec.type !== "export_specifier") continue;
369
+ const origName = spec.childForFieldName("name") ?? spec.namedChildren[0];
370
+ const aliasName = spec.childForFieldName("alias");
371
+ const exportedName = aliasName ? aliasName.text.trim() : origName ? origName.text.trim() : "";
372
+ if (!exportedName) continue;
373
+ if (aliasName && origName) {
374
+ symbols.push({
375
+ name: exportedName,
376
+ kind: "constant",
377
+ signature: `export { ${origName.text.trim()} as ${exportedName} }`,
378
+ visibility: "public",
379
+ line_start: lineStart,
380
+ line_end: lineEnd,
381
+ line_count: lineCount,
382
+ });
383
+ }
384
+ }
385
+ }
386
+ }
387
+ return;
388
+ }
389
+
390
+ if (
391
+ stmtNode.type === "class_declaration" ||
392
+ stmtNode.type === "abstract_class_declaration" ||
393
+ stmtNode.type === "interface_declaration"
394
+ ) {
395
+ const nameNode =
396
+ stmtNode.childForFieldName("name") ??
397
+ stmtNode.namedChildren.find((c) => c.type === "type_identifier" || c.type === "identifier");
398
+ if (!nameNode) return;
399
+
400
+ const className = nameNode.text;
401
+ const kind: SymbolKind = stmtNode.type === "interface_declaration" ? "interface" : "class";
402
+ symbols.push({
403
+ name: className,
404
+ kind,
405
+ signature: getNodeHeader(stmtNode),
406
+ visibility: "public",
407
+ line_start: lineStart,
408
+ line_end: lineEnd,
409
+ line_count: lineCount,
410
+ });
411
+
412
+ const bodyNode =
413
+ stmtNode.childForFieldName("body") ??
414
+ stmtNode.namedChildren.find(
415
+ (c) => c.type === "class_body" || c.type === "interface_body" || c.type === "object_type"
416
+ );
417
+ if (bodyNode) {
418
+ this.extractClassOrInterfaceMembers(className, bodyNode, symbols);
419
+ }
420
+ return;
421
+ }
422
+
423
+ if (stmtNode.type === "type_alias_declaration" || stmtNode.type === "enum_declaration") {
424
+ const nameNode =
425
+ stmtNode.childForFieldName("name") ??
426
+ stmtNode.namedChildren.find((c) => c.type === "type_identifier" || c.type === "identifier");
427
+ if (!nameNode) return;
428
+
429
+ symbols.push({
430
+ name: nameNode.text,
431
+ kind: stmtNode.type === "enum_declaration" ? "enum" : "type",
432
+ signature: getNodeHeader(stmtNode),
433
+ visibility: "public",
434
+ line_start: lineStart,
435
+ line_end: lineEnd,
436
+ line_count: lineCount,
437
+ });
438
+ return;
439
+ }
440
+
441
+ if (stmtNode.type === "function_declaration" || stmtNode.type === "generator_function_declaration") {
442
+ const nameNode =
443
+ stmtNode.childForFieldName("name") ??
444
+ stmtNode.namedChildren.find((c) => c.type === "identifier");
445
+ if (!nameNode) return;
446
+
447
+ const fnName = nameNode.text;
448
+ const paramsNode =
449
+ stmtNode.childForFieldName("parameters") ??
450
+ stmtNode.namedChildren.find((c) => c.type === "formal_parameters");
451
+ const retNode = stmtNode.childForFieldName("return_type");
452
+ const paramsText = paramsNode ? paramsNode.text : "()";
453
+ const retText = retNode ? `: ${retNode.text.replace(/^:\s*/, "")}` : "";
454
+ const nestingDepth = calculateAstControlFlowNesting(
455
+ stmtNode,
456
+ TS_CONTROL_FLOW_NODES,
457
+ TS_FUNCTION_BOUNDARY_NODES
458
+ );
459
+
460
+ symbols.push({
461
+ name: fnName,
462
+ kind: "function",
463
+ signature: `${fnName}${paramsText}${retText}`,
464
+ visibility: "public",
465
+ line_start: lineStart,
466
+ line_end: lineEnd,
467
+ line_count: lineCount,
468
+ nesting_depth: nestingDepth,
469
+ });
470
+ return;
471
+ }
472
+
473
+ if (stmtNode.type === "lexical_declaration" || stmtNode.type === "variable_declaration") {
474
+ this.extractVariableDeclarations(stmtNode, isExported, outerStartRow, symbols);
475
+ }
476
+ };
477
+
478
+ for (const child of rootNode.namedChildren) {
479
+ processTopLevelNode(child, false);
480
+ }
481
+
482
+ return { symbols, dependencies };
483
+ }
484
+
485
+ private extractVariableDeclarations(
486
+ stmtNode: Parser.SyntaxNode,
487
+ isExported: boolean,
488
+ outerStartRow: number | undefined,
489
+ symbols: ExtractedSymbol[]
490
+ ): void {
491
+ const declText = stmtNode.text.trimStart();
492
+ const isConst = declText.startsWith("const ") || declText.startsWith("const\t");
493
+ const declKeyword = isConst ? "const" : declText.startsWith("var ") ? "var" : "let";
494
+
495
+ for (const child of stmtNode.namedChildren) {
496
+ if (child.type !== "variable_declarator") continue;
497
+ const nameNode = child.childForFieldName("name") ?? child.namedChildren[0];
498
+ if (!nameNode) continue;
499
+
500
+ const dStart = (outerStartRow ?? child.startPosition.row) + 1;
501
+ const dEnd = Math.max(dStart, child.endPosition.row + 1);
502
+ const dCount = Math.max(1, dEnd - dStart + 1);
503
+
504
+ if (nameNode.type !== "identifier") {
505
+ const patternVars = this.extractPatternIdentifiers(nameNode);
506
+ for (const varName of patternVars) {
507
+ symbols.push({
508
+ name: varName,
509
+ kind: isConst ? "constant" : "variable",
510
+ signature: `${declKeyword} ${varName}`,
511
+ visibility: isExported ? "public" : "private",
512
+ line_start: dStart,
513
+ line_end: dEnd,
514
+ line_count: dCount,
515
+ });
516
+ }
517
+ continue;
518
+ }
519
+
520
+ const varName = nameNode.text;
521
+ const typeNode = child.childForFieldName("type");
522
+ const valueNode = child.childForFieldName("value");
523
+ const typeStr = typeNode ? `: ${typeNode.text.replace(/^:\s*/, "")}` : "";
524
+
525
+ if (
526
+ valueNode &&
527
+ (valueNode.type === "arrow_function" ||
528
+ valueNode.type === "function_expression" ||
529
+ valueNode.type === "function")
530
+ ) {
531
+ const paramsNode =
532
+ valueNode.childForFieldName("parameters") ??
533
+ valueNode.namedChildren.find((c) => c.type === "formal_parameters");
534
+ const retNode = valueNode.childForFieldName("return_type");
535
+ const paramsText = paramsNode ? paramsNode.text : "()";
536
+ const retText = retNode ? `: ${retNode.text.replace(/^:\s*/, "")}` : typeStr;
537
+ const nestingDepth = calculateAstControlFlowNesting(
538
+ valueNode,
539
+ TS_CONTROL_FLOW_NODES,
540
+ TS_FUNCTION_BOUNDARY_NODES
541
+ );
542
+
543
+ symbols.push({
544
+ name: varName,
545
+ kind: "function",
546
+ signature: `${varName}${paramsText}${retText}`,
547
+ visibility: isExported ? "public" : "private",
548
+ line_start: dStart,
549
+ line_end: dEnd,
550
+ line_count: dCount,
551
+ nesting_depth: nestingDepth,
552
+ });
553
+ } else {
554
+ symbols.push({
555
+ name: varName,
556
+ kind: isConst ? "constant" : "variable",
557
+ signature: `${declKeyword} ${varName}${typeStr}`,
558
+ visibility: isExported ? "public" : "private",
559
+ line_start: dStart,
560
+ line_end: dEnd,
561
+ line_count: dCount,
562
+ });
563
+ }
564
+ }
565
+ }
566
+
567
+ private extractClassOrInterfaceMembers(
568
+ className: string,
569
+ bodyNode: Parser.SyntaxNode,
570
+ symbols: ExtractedSymbol[]
571
+ ): void {
572
+ for (const member of bodyNode.namedChildren) {
573
+ const mStart = member.startPosition.row + 1;
574
+ const mEnd = Math.max(mStart, member.endPosition.row + 1);
575
+ const mCount = Math.max(1, mEnd - mStart + 1);
576
+
577
+ if (
578
+ member.type === "method_definition" ||
579
+ member.type === "method_signature" ||
580
+ member.type === "abstract_method_signature"
581
+ ) {
582
+ const nameNode =
583
+ member.childForFieldName("name") ??
584
+ member.namedChildren.find(
585
+ (c) =>
586
+ c.type === "property_identifier" ||
587
+ c.type === "private_property_identifier" ||
588
+ c.type === "identifier"
589
+ );
590
+ if (!nameNode) continue;
591
+
592
+ const methodName = nameNode.text;
593
+ const vis = this.extractVisibility(member, methodName);
594
+ const paramsNode =
595
+ member.childForFieldName("parameters") ??
596
+ member.namedChildren.find((c) => c.type === "formal_parameters");
597
+
598
+ if (methodName === "constructor" && paramsNode) {
599
+ this.extractConstructorParameterProperties(className, paramsNode, symbols);
600
+ }
601
+
602
+ const retNode = member.childForFieldName("return_type");
603
+ const paramsText = paramsNode ? paramsNode.text : "()";
604
+ const retText = retNode ? `: ${retNode.text.replace(/^:\s*/, "")}` : "";
605
+ const nestingDepth = calculateAstControlFlowNesting(
606
+ member,
607
+ TS_CONTROL_FLOW_NODES,
608
+ TS_FUNCTION_BOUNDARY_NODES
609
+ );
610
+
611
+ symbols.push({
612
+ name: `${className}::${methodName}`,
613
+ kind: "method",
614
+ signature: `${vis} ${methodName}${paramsText}${retText}`,
615
+ visibility: vis,
616
+ line_start: mStart,
617
+ line_end: mEnd,
618
+ line_count: mCount,
619
+ nesting_depth: nestingDepth,
620
+ });
621
+ continue;
622
+ }
623
+
624
+ if (
625
+ member.type === "public_field_definition" ||
626
+ member.type === "field_definition" ||
627
+ member.type === "property_signature"
628
+ ) {
629
+ const nameNode =
630
+ member.childForFieldName("name") ??
631
+ member.namedChildren.find(
632
+ (c) =>
633
+ c.type === "property_identifier" ||
634
+ c.type === "private_property_identifier" ||
635
+ c.type === "identifier"
636
+ );
637
+ if (!nameNode) continue;
638
+
639
+ const propName = nameNode.text;
640
+ const vis = this.extractVisibility(member, propName);
641
+ const typeNode = member.childForFieldName("type");
642
+ const typeStr = typeNode ? `: ${typeNode.text.replace(/^:\s*/, "")}` : "";
643
+
644
+ symbols.push({
645
+ name: `${className}::${propName}`,
646
+ kind: "property",
647
+ signature: `${vis} ${propName}${typeStr}`,
648
+ visibility: vis,
649
+ line_start: mStart,
650
+ line_end: mEnd,
651
+ line_count: mCount,
652
+ });
653
+ }
654
+ }
655
+ }
656
+
657
+ private extractVisibility(memberNode: Parser.SyntaxNode, memberName: string): Visibility {
658
+ if (memberName.startsWith("#")) return "private";
659
+ for (const child of memberNode.children) {
660
+ if (child.type === "accessibility_modifier") {
661
+ const text = child.text.trim();
662
+ if (text === "private" || text === "protected" || text === "public") {
663
+ return text;
664
+ }
665
+ }
666
+ }
667
+ return "public";
668
+ }
669
+
670
+ private extractImportedSymbols(importNode: Parser.SyntaxNode): string[] {
671
+ const names = new Set<string>();
672
+ const walkClause = (node: Parser.SyntaxNode): void => {
673
+ if (node.type === "import_specifier") {
674
+ const origName = node.childForFieldName("name") ?? node.namedChildren[0];
675
+ const aliasName = node.childForFieldName("alias");
676
+ if (origName && origName.text) names.add(origName.text.trim());
677
+ if (aliasName && aliasName.text) names.add(aliasName.text.trim());
678
+ return;
679
+ }
680
+ if (node.type === "namespace_import") {
681
+ const idNode = node.namedChildren.find((c) => c.type === "identifier");
682
+ if (idNode?.text) names.add(idNode.text.trim());
683
+ return;
684
+ }
685
+ if (node.type === "import_clause") {
686
+ for (const child of node.namedChildren) {
687
+ if (child.type === "identifier") {
688
+ names.add(child.text.trim());
689
+ } else {
690
+ walkClause(child);
691
+ }
692
+ }
693
+ return;
694
+ }
695
+ for (const child of node.namedChildren) {
696
+ if (child.type !== "string") {
697
+ walkClause(child);
698
+ }
699
+ }
700
+ };
701
+ walkClause(importNode);
702
+ return Array.from(names);
703
+ }
704
+
705
+ private extractRequireImportedSymbols(callNode: Parser.SyntaxNode): string[] {
706
+ const names = new Set<string>();
707
+ const parent = callNode.parent;
708
+ if (parent && parent.type === "variable_declarator") {
709
+ const nameNode = parent.childForFieldName("name") ?? parent.namedChildren[0];
710
+ if (nameNode) {
711
+ if (nameNode.type === "identifier") {
712
+ names.add(nameNode.text.trim());
713
+ } else if (nameNode.type === "object_pattern") {
714
+ for (const prop of nameNode.namedChildren) {
715
+ if (prop.type === "shorthand_property_identifier_pattern" || prop.type === "identifier") {
716
+ names.add(prop.text.trim());
717
+ } else if (prop.type === "pair_pattern") {
718
+ const key = prop.childForFieldName("key") ?? prop.namedChildren[0];
719
+ const val = prop.childForFieldName("value") ?? prop.namedChildren[1];
720
+ if (key?.text) names.add(key.text.trim());
721
+ if (val?.text) names.add(val.text.trim());
722
+ }
723
+ }
724
+ }
725
+ }
726
+ }
727
+ return Array.from(names);
728
+ }
729
+
730
+ private extractExportedSymbols(exportNode: Parser.SyntaxNode): string[] {
731
+ const names = new Set<string>();
732
+ const walk = (node: Parser.SyntaxNode): void => {
733
+ if (node.type === "export_specifier") {
734
+ const origName = node.childForFieldName("name") ?? node.namedChildren[0];
735
+ const aliasName = node.childForFieldName("alias");
736
+ if (origName?.text) names.add(origName.text.trim());
737
+ if (aliasName?.text) names.add(aliasName.text.trim());
738
+ return;
739
+ }
740
+ if (node.type === "namespace_export") {
741
+ const idNode = node.namedChildren.find((c) => c.type === "identifier");
742
+ if (idNode?.text) names.add(idNode.text.trim());
743
+ return;
744
+ }
745
+ for (const child of node.namedChildren) {
746
+ if (child.type !== "string") {
747
+ walk(child);
748
+ }
749
+ }
750
+ };
751
+ walk(exportNode);
752
+ return Array.from(names);
753
+ }
754
+
755
+ private extractConstructorParameterProperties(
756
+ className: string,
757
+ paramsNode: Parser.SyntaxNode,
758
+ symbols: ExtractedSymbol[]
759
+ ): void {
760
+ for (const param of paramsNode.namedChildren) {
761
+ if (param.type !== "required_parameter" && param.type !== "optional_parameter") continue;
762
+
763
+ const hasAccessibility = param.namedChildren.some((c) => c.type === "accessibility_modifier");
764
+ const hasReadonly = param.namedChildren.some((c) => c.type === "readonly");
765
+ if (!hasAccessibility && !hasReadonly) continue;
766
+
767
+ const nameNode =
768
+ param.childForFieldName("pattern") ??
769
+ param.childForFieldName("name") ??
770
+ param.namedChildren.find((c) => c.type === "identifier");
771
+ if (!nameNode) continue;
772
+
773
+ const propName = nameNode.text.trim();
774
+ const vis = this.extractVisibility(param, propName);
775
+ const typeNode = param.childForFieldName("type");
776
+ const typeStr = typeNode ? `: ${typeNode.text.replace(/^:\s*/, "")}` : "";
777
+ const pStart = param.startPosition.row + 1;
778
+ const pEnd = Math.max(pStart, param.endPosition.row + 1);
779
+
780
+ symbols.push({
781
+ name: `${className}::${propName}`,
782
+ kind: "property",
783
+ signature: `${vis} ${propName}${typeStr}`,
784
+ visibility: vis,
785
+ line_start: pStart,
786
+ line_end: pEnd,
787
+ line_count: Math.max(1, pEnd - pStart + 1),
788
+ });
789
+ }
790
+ }
791
+
792
+ private extractPatternIdentifiers(node: Parser.SyntaxNode): string[] {
793
+ const results: string[] = [];
794
+ const walk = (n: Parser.SyntaxNode): void => {
795
+ if (n.type === "identifier" || n.type === "shorthand_property_identifier_pattern") {
796
+ results.push(n.text.trim());
797
+ return;
798
+ }
799
+ if (n.type === "pair_pattern") {
800
+ const val = n.childForFieldName("value") ?? n.namedChildren[1];
801
+ if (val) walk(val);
802
+ return;
803
+ }
804
+ if (n.type === "assignment_pattern" || n.type === "object_assignment_pattern") {
805
+ const left = n.childForFieldName("left") ?? n.namedChildren[0];
806
+ if (left) walk(left);
807
+ return;
808
+ }
809
+ if (n.type === "rest_pattern") {
810
+ const arg = n.namedChildren[0];
811
+ if (arg) walk(arg);
812
+ return;
813
+ }
814
+ if (n.type === "object_pattern" || n.type === "array_pattern") {
815
+ for (const child of n.namedChildren) {
816
+ walk(child);
817
+ }
818
+ }
819
+ };
820
+ walk(node);
821
+ return results;
822
+ }
823
+ }
824
+
825
+
826
+