@dimassetoid/septum-mcp 0.0.0-stage → 0.3.0

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 (119) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +142 -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 +133 -0
  7. package/src/cli/commands/hook.ts +205 -0
  8. package/src/cli/commands/init.ts +570 -0
  9. package/src/cli/commands/serve.ts +5 -0
  10. package/src/cli/commands/sync.ts +39 -0
  11. package/src/cli/hook-payload.ts +43 -0
  12. package/src/cli/index.ts +124 -0
  13. package/src/cli/scaffold/harness-config.ts +661 -0
  14. package/src/cli/templates/hooks.ts +110 -0
  15. package/src/cli/templates/rules.ts +35 -0
  16. package/src/cli/templates/skill.ts +55 -0
  17. package/src/cli/ui/prompts.ts +122 -0
  18. package/src/core/boundary/evaluator.ts +426 -0
  19. package/src/core/boundary/staged-auditor.ts +120 -0
  20. package/src/core/config/domain-ownership.ts +45 -0
  21. package/src/core/config/loader.ts +174 -0
  22. package/src/core/config/schema.ts +59 -0
  23. package/src/core/database/client.ts +60 -0
  24. package/src/core/database/repositories/dependency.repository.ts +733 -0
  25. package/src/core/database/repositories/domain.repository.ts +373 -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 +647 -0
  29. package/src/core/database/repository.ts +358 -0
  30. package/src/core/database/schema.ts +123 -0
  31. package/src/core/discovery/navigation.ts +105 -0
  32. package/src/core/discovery/topology-detector.ts +863 -0
  33. package/src/core/ingestion/hasher.ts +5 -0
  34. package/src/core/ingestion/pipeline.ts +590 -0
  35. package/src/core/parser/behavioral-analyzer.ts +123 -0
  36. package/src/core/parser/boundary-tracker.ts +458 -0
  37. package/src/core/parser/extractors/base.ts +71 -0
  38. package/src/core/parser/extractors/blade.ts +159 -0
  39. package/src/core/parser/extractors/csharp.ts +248 -0
  40. package/src/core/parser/extractors/django-semantic.ts +655 -0
  41. package/src/core/parser/extractors/elysia-semantic.ts +517 -0
  42. package/src/core/parser/extractors/express-semantic.ts +429 -0
  43. package/src/core/parser/extractors/fastapi-semantic.ts +702 -0
  44. package/src/core/parser/extractors/fastify-semantic.ts +759 -0
  45. package/src/core/parser/extractors/flask-semantic.ts +482 -0
  46. package/src/core/parser/extractors/frontend.ts +149 -0
  47. package/src/core/parser/extractors/go-semantic.ts +670 -0
  48. package/src/core/parser/extractors/go.ts +491 -0
  49. package/src/core/parser/extractors/hono-semantic.ts +546 -0
  50. package/src/core/parser/extractors/java.ts +227 -0
  51. package/src/core/parser/extractors/laravel-semantic.ts +1150 -0
  52. package/src/core/parser/extractors/mcp-cli-semantic.ts +367 -0
  53. package/src/core/parser/extractors/nestjs-semantic.ts +527 -0
  54. package/src/core/parser/extractors/php.ts +585 -0
  55. package/src/core/parser/extractors/python.ts +503 -0
  56. package/src/core/parser/extractors/registry.ts +99 -0
  57. package/src/core/parser/extractors/ruby.ts +237 -0
  58. package/src/core/parser/extractors/rust.ts +304 -0
  59. package/src/core/parser/extractors/semantic-extractor.interface.ts +24 -0
  60. package/src/core/parser/extractors/symfony-semantic.ts +321 -0
  61. package/src/core/parser/extractors/typescript.ts +839 -0
  62. package/src/core/parser/implementation-fingerprint.ts +97 -0
  63. package/src/core/parser/language-capabilities.ts +87 -0
  64. package/src/core/parser/schema/providers/laravel-migration-provider.ts +239 -0
  65. package/src/core/parser/schema/schema-provider-registry.ts +41 -0
  66. package/src/core/parser/schema/schema-provider.interface.ts +34 -0
  67. package/src/core/parser/semantic-revision.ts +3 -0
  68. package/src/core/parser/static-string.ts +51 -0
  69. package/src/core/parser/tree-sitter.ts +63 -0
  70. package/src/core/parser/wasm-assets.ts +25 -0
  71. package/src/core/parser/wasm-loader.ts +274 -0
  72. package/src/core/resolver/call-graph-tracer.ts +1033 -0
  73. package/src/core/resolver/import-resolver.ts +365 -0
  74. package/src/core/resolver/interpretation-inputs.ts +7 -0
  75. package/src/core/resolver/languages/go.ts +103 -0
  76. package/src/core/resolver/languages/managed.ts +200 -0
  77. package/src/core/resolver/languages/python.ts +115 -0
  78. package/src/core/resolver/languages/ruby.ts +40 -0
  79. package/src/core/resolver/languages/rust.ts +127 -0
  80. package/src/core/resolver/languages/shared.ts +71 -0
  81. package/src/core/resolver/module-resolver.ts +486 -0
  82. package/src/core/resolver/path-resolver.ts +200 -0
  83. package/src/core/resolver/slice-catalog.ts +79 -0
  84. package/src/core/resolver/slice-format.ts +9 -0
  85. package/src/core/resolver/symbol-impact.ts +381 -0
  86. package/src/core/resolver/symbol-locator.ts +276 -0
  87. package/src/core/resolver/symbol-reference.ts +53 -0
  88. package/src/core/resolver/vertical-slice-tracer.ts +344 -0
  89. package/src/core/resources/ignore.ts +144 -0
  90. package/src/core/resources/limits.ts +14 -0
  91. package/src/core/resources/source-files.ts +185 -0
  92. package/src/core/resources/source-inventory.ts +55 -0
  93. package/src/core/resources/work-queue.ts +74 -0
  94. package/src/core/retrieval/context-page.ts +109 -0
  95. package/src/core/retrieval/context.ts +320 -0
  96. package/src/core/retrieval/resolution-warnings.ts +21 -0
  97. package/src/core/retrieval/symbol-evidence.ts +97 -0
  98. package/src/core/retrieval/typescript-references.ts +262 -0
  99. package/src/core/telemetry/telemetry.ts +142 -0
  100. package/src/index.ts +18 -0
  101. package/src/mcp/schemas.ts +746 -0
  102. package/src/mcp/server.ts +600 -0
  103. package/src/mcp/tools/boundary.ts +17 -0
  104. package/src/mcp/tools/check-boundary.ts +382 -0
  105. package/src/mcp/tools/get-domain-catalog.ts +39 -0
  106. package/src/mcp/tools/get-symbol-impact.ts +104 -0
  107. package/src/mcp/tools/get-symbol.ts +399 -0
  108. package/src/mcp/tools/locate-symbol.ts +236 -0
  109. package/src/mcp/tools/map.ts +26 -0
  110. package/src/mcp/tools/register-domain.ts +113 -0
  111. package/src/mcp/tools/slice.ts +16 -0
  112. package/src/mcp/tools/symbol.ts +95 -0
  113. package/src/mcp/tools/trace-vertical-slice.ts +106 -0
  114. package/src/mcp/workspace-manager.ts +170 -0
  115. package/src/mcp/workspace-watcher.ts +202 -0
  116. package/src/types/index.ts +438 -0
  117. package/src/types/wasm.d.ts +4 -0
  118. package/src/utils/version-check.ts +209 -0
  119. package/src/version.ts +1 -0
@@ -0,0 +1,517 @@
1
+ import { listSourceFiles, type SourceInventory } from "../../resources/source-inventory.ts";
2
+ import type Parser from "web-tree-sitter";
3
+ import { parseWithTreeSitter } from "../wasm-loader.ts";
4
+ import { readOptionalSourceFile } from "../../resources/source-files.ts";
5
+ import { ImportResolver } from "../../resolver/import-resolver.ts";
6
+ import * as fs from "node:fs";
7
+ import * as path from "node:path";
8
+ import type { SemanticSliceExtractor, VerticalSliceCandidate } from "./semantic-extractor.interface.ts";
9
+
10
+ const ELYSIA_VERBS = new Set(["get", "post", "put", "patch", "delete", "all"]);
11
+
12
+ interface ExtractedRoute {
13
+ file: string;
14
+ relPath: string;
15
+ routerVar: string;
16
+ httpMethods: string[];
17
+ rawPath: string;
18
+ validationSchema?: { symbol: string; file?: string; line?: number };
19
+ handler: { symbol: string; line: number; node?: Parser.SyntaxNode };
20
+ routeLine: number;
21
+ }
22
+
23
+ interface ExtractedMount {
24
+ file: string;
25
+ relPath: string;
26
+ callerVar: string;
27
+ prefix: string;
28
+ targetVar: string;
29
+ line: number;
30
+ }
31
+
32
+ function cleanLiteral(node: Parser.SyntaxNode | null | undefined): string | undefined {
33
+ if (!node) return undefined;
34
+ if (node.type === "string") {
35
+ const raw = node.text.trim();
36
+ return raw.replace(/^['"`]|['"`]$/g, "");
37
+ }
38
+ return undefined;
39
+ }
40
+
41
+ function normalizeRouteUri(prefix: string, rawPath: string): string {
42
+ const p = (prefix ?? "").trim().replace(/\/+$/, "");
43
+ let r = (rawPath ?? "").trim();
44
+ if (!r.startsWith("/")) r = "/" + r;
45
+ if (r === "/" && p.length > 0) return p;
46
+ if (r === "/" && p.length === 0) return "/";
47
+ const joined = `${p}${r}`;
48
+ return joined.startsWith("/") ? joined : `/${joined}`;
49
+ }
50
+
51
+ export class ElysiaSemanticExtractor implements SemanticSliceExtractor {
52
+ public readonly id = "elysia";
53
+ public readonly name = "Elysia TypeBox Composition Extractor";
54
+
55
+ public canHandle(projectRoot: string, framework?: string): boolean {
56
+ if (framework === "elysia") return true;
57
+
58
+ const pkgPath = path.join(projectRoot, "package.json");
59
+ if (fs.existsSync(pkgPath)) {
60
+ try {
61
+ const pkg = JSON.parse(readOptionalSourceFile(pkgPath, "utf-8"));
62
+ const deps = { ...(pkg.dependencies || {}), ...(pkg.devDependencies || {}) };
63
+ if (deps.elysia) return true;
64
+ } catch {}
65
+ }
66
+
67
+ // Check entrypoint source code
68
+ for (const entry of ["src/index.ts", "src/app.ts", "index.ts", "app.ts", "src/server.ts"]) {
69
+ const full = path.join(projectRoot, entry);
70
+ if (fs.existsSync(full)) {
71
+ try {
72
+ const content = readOptionalSourceFile(full, "utf-8");
73
+ if (content.includes("from \"elysia\"") || content.includes("from 'elysia'") || content.includes("require(\"elysia\")")) {
74
+ return true;
75
+ }
76
+ } catch {}
77
+ }
78
+ }
79
+
80
+ return false;
81
+ }
82
+
83
+ public async extractSlices(
84
+ projectRoot: string,
85
+ domainId?: number,
86
+ signal?: AbortSignal, inventory?: SourceInventory
87
+ ): Promise<VerticalSliceCandidate[]> {
88
+ const slices: VerticalSliceCandidate[] = [];
89
+ const sourceFiles = listSourceFiles(projectRoot, /\.(?:[cm]?[jt]sx?)$/, inventory).filter(file => !file.endsWith('.d.ts'));
90
+ const resolver = new ImportResolver(projectRoot);
91
+
92
+ const allRoutes: ExtractedRoute[] = [];
93
+ const allMounts: ExtractedMount[] = [];
94
+ const fileImports = new Map<string, Map<string, string>>(); // relPath -> (ident -> targetRelPath)
95
+
96
+ for (const file of sourceFiles) {
97
+ if (signal?.aborted) throw new Error("semantic_extraction_cancelled");
98
+ const relPath = path.relative(projectRoot, file).replace(/\\/g, "/");
99
+ const content = readOptionalSourceFile(file, "utf-8");
100
+ if (!content.trim()) continue;
101
+
102
+ const fileData = await parseWithTreeSitter(
103
+ "typescript",
104
+ content,
105
+ (rootNode) => this.extractFileAst(file, relPath, rootNode, resolver, projectRoot)
106
+ );
107
+
108
+ if (fileData) {
109
+ allRoutes.push(...fileData.routes);
110
+ allMounts.push(...fileData.mounts);
111
+ if (fileData.resolvedImports.size > 0) {
112
+ fileImports.set(relPath, fileData.resolvedImports);
113
+ }
114
+ }
115
+ }
116
+
117
+ // Resolve Router Prefix Hierarchy
118
+ const routerPrefixes = this.resolvePrefixHierarchy(allMounts, fileImports);
119
+
120
+ // Build Vertical Slices (Level 0 – Level 5)
121
+ for (const route of allRoutes) {
122
+ const prefixes = routerPrefixes.get(`${route.relPath}#${route.routerVar}`) || [""];
123
+
124
+ for (const prefix of prefixes) {
125
+ for (const httpMethod of route.httpMethods) {
126
+ const fullUri = normalizeRouteUri(prefix, route.rawPath);
127
+ const actionSymbol = route.handler.symbol;
128
+ const rootIdent = actionSymbol.includes(".") ? actionSymbol.split(".")[0] : actionSymbol;
129
+ const imports = fileImports.get(route.relPath);
130
+ const resolvedControllerFile = imports?.get(rootIdent) || route.relPath;
131
+ const fileBase = path.basename(route.relPath).replace(/\.[^.]+$/, "");
132
+ const controllerClass = actionSymbol.includes(".")
133
+ ? actionSymbol.split(".")[0]
134
+ : fileBase.charAt(0).toUpperCase() + fileBase.slice(1);
135
+ const actionName = actionSymbol.includes(".") ? actionSymbol.split(".").pop()! : actionSymbol;
136
+
137
+ const chain: Array<{ stage: string; symbol: string; file?: string; line?: number; description?: string }> = [
138
+ {
139
+ stage: "ingress",
140
+ symbol: `${httpMethod} ${fullUri}`,
141
+ file: route.relPath,
142
+ line: route.routeLine,
143
+ description: `Elysia Route: ${httpMethod} ${fullUri}`,
144
+ },
145
+ ];
146
+
147
+ // L2: Validation Schema (TypeBox)
148
+ if (route.validationSchema) {
149
+ chain.push({
150
+ stage: "validation",
151
+ symbol: route.validationSchema.symbol,
152
+ file: route.validationSchema.file || route.relPath,
153
+ line: route.validationSchema.line,
154
+ description: `Elysia TypeBox Schema: ${route.validationSchema.symbol}`,
155
+ });
156
+ }
157
+
158
+ // L3: Controller Action
159
+ chain.push({
160
+ stage: "controller",
161
+ symbol: actionSymbol,
162
+ file: resolvedControllerFile,
163
+ line: route.handler.line,
164
+ description: `Elysia Action Handler`,
165
+ });
166
+
167
+ // L4–L5: Tracing Service & Persistence from handler or imported controller
168
+ const scanFile = resolvedControllerFile !== route.relPath ? resolvedControllerFile : route.relPath;
169
+ const fullScanPath = path.resolve(projectRoot, scanFile);
170
+ const scanContent = readOptionalSourceFile(fullScanPath, "utf-8");
171
+
172
+ if (scanContent) {
173
+ // Check service calls: orderService.create(...) or OrderService.process(...)
174
+ const sMatch = scanContent.match(/\b([a-zA-Z0-9_]+(?:Service|Usecase))\s*\.\s*([A-Za-z0-9_]+)\s*\(/i);
175
+ let serviceFile: string | undefined;
176
+ if (sMatch) {
177
+ const svcIdent = sMatch[1];
178
+ const svcMethod = sMatch[2];
179
+ const imports = fileImports.get(scanFile);
180
+ let svcImport = imports?.get(svcIdent);
181
+ if (!svcImport && imports) {
182
+ const pascal = svcIdent.charAt(0).toUpperCase() + svcIdent.slice(1);
183
+ svcImport = imports.get(pascal);
184
+ if (!svcImport) {
185
+ const instMatch = scanContent.match(new RegExp(`\\b(?:const|let|var)\\s+${svcIdent}\\s*=\\s*new\\s+([a-zA-Z0-9_]+)`));
186
+ if (instMatch) {
187
+ svcImport = imports.get(instMatch[1]);
188
+ }
189
+ }
190
+ }
191
+ serviceFile = svcImport;
192
+
193
+ chain.push({
194
+ stage: "service",
195
+ symbol: `${svcIdent}::${svcMethod}`,
196
+ ...(serviceFile ? { file: serviceFile } : {}),
197
+ description: `Elysia Service Call: ${svcIdent}::${svcMethod}`,
198
+ });
199
+ }
200
+
201
+ // Check Prisma / Drizzle / TypeORM persistence in scanFile or serviceFile
202
+ const filesToInspect = [fullScanPath];
203
+ if (serviceFile) filesToInspect.push(path.resolve(projectRoot, serviceFile));
204
+
205
+ for (const inspectPath of filesToInspect) {
206
+ const contentToInspect = readOptionalSourceFile(inspectPath, "utf-8");
207
+ if (!contentToInspect) continue;
208
+
209
+ const prismaMatch = contentToInspect.match(/prisma\.([a-zA-Z0-9_]+)\.(?:find|create|update|delete|upsert)/);
210
+ const drizzleMatch = contentToInspect.match(/db\.(?:insert|select|update|delete)\s*\(\s*([a-zA-Z0-9_]+)\s*\)/);
211
+
212
+ const entityName =
213
+ prismaMatch ? (prismaMatch[1].charAt(0).toUpperCase() + prismaMatch[1].slice(1)) :
214
+ drizzleMatch ? (drizzleMatch[1].charAt(0).toUpperCase() + drizzleMatch[1].slice(1)) : undefined;
215
+
216
+ if (entityName && !chain.some((n) => n.stage === "entity")) {
217
+ const inspectRel = path.relative(projectRoot, inspectPath).replace(/\\/g, "/");
218
+ const entImport = fileImports.get(inspectRel)?.get(entityName);
219
+
220
+ chain.push({
221
+ stage: "repository",
222
+ symbol: `${entityName}Repository`,
223
+ file: inspectRel,
224
+ description: prismaMatch ? "Prisma ORM Client" : "Drizzle ORM Query",
225
+ });
226
+
227
+ chain.push({
228
+ stage: "entity",
229
+ symbol: entityName,
230
+ ...(entImport ? { file: entImport } : { file: inspectRel }),
231
+ description: "Database Entity / Schema Model",
232
+ });
233
+ }
234
+ }
235
+ }
236
+
237
+ slices.push({
238
+ domain_id: domainId ?? null,
239
+ feature_key: `elysia.${controllerClass.toLowerCase()}.${actionName}`,
240
+ http_method: httpMethod,
241
+ route_uri: fullUri,
242
+ route_name: `${controllerClass}.${actionName}`,
243
+ controller_class: controllerClass,
244
+ action_name: actionName,
245
+ controller_file: resolvedControllerFile,
246
+ controller_line: route.handler.line,
247
+ architecture_style: "mvc",
248
+ entry_kind: "http_route",
249
+ execution_chain_json: JSON.stringify(chain),
250
+ });
251
+ }
252
+ }
253
+ }
254
+
255
+ return slices;
256
+ }
257
+
258
+ private extractFileAst(
259
+ file: string,
260
+ relPath: string,
261
+ rootNode: Parser.SyntaxNode,
262
+ resolver: ImportResolver,
263
+ _projectRoot: string
264
+ ): {
265
+ routes: ExtractedRoute[];
266
+ mounts: ExtractedMount[];
267
+ resolvedImports: Map<string, string>;
268
+ } {
269
+ const routes: ExtractedRoute[] = [];
270
+ const mounts: ExtractedMount[] = [];
271
+ const resolvedImports = new Map<string, string>();
272
+
273
+ // 1. Collect import statements
274
+ for (const child of rootNode.namedChildren) {
275
+ if (child.type === "import_statement") {
276
+ const sourceNode = child.childForFieldName("source");
277
+ const specifier = cleanLiteral(sourceNode);
278
+ if (!specifier) continue;
279
+
280
+ const res = resolver.resolve(specifier, relPath);
281
+ const targetFile = res.status === "resolved" && res.targets[0]?.files[0] ? res.targets[0].files[0] : undefined;
282
+ if (!targetFile) continue;
283
+
284
+ const importClause = child.namedChildren.find((c) => c.type === "import_clause");
285
+ if (importClause) {
286
+ for (const ic of importClause.namedChildren) {
287
+ if (ic.type === "identifier") {
288
+ resolvedImports.set(ic.text.trim(), targetFile);
289
+ } else if (ic.type === "namespace_import") {
290
+ const identNode = ic.namedChildren.find((n) => n.type === "identifier");
291
+ if (identNode) resolvedImports.set(identNode.text.trim(), targetFile);
292
+ } else if (ic.type === "named_imports") {
293
+ for (const spec of ic.namedChildren) {
294
+ if (spec.type === "import_specifier") {
295
+ const alias = spec.childForFieldName("alias");
296
+ const name = spec.childForFieldName("name") ?? spec.namedChildren[0];
297
+ const ident = alias ? alias.text.trim() : name ? name.text.trim() : "";
298
+ if (ident) resolvedImports.set(ident, targetFile);
299
+ }
300
+ }
301
+ }
302
+ }
303
+ }
304
+ }
305
+ }
306
+
307
+ // 2. Walk call expressions for routes and sub-route mounts
308
+ const walkCalls = (node: Parser.SyntaxNode): void => {
309
+ if (node.type === "call_expression") {
310
+ const fnNode = node.childForFieldName("function");
311
+ if (fnNode && fnNode.type === "member_expression") {
312
+ const objNode = fnNode.childForFieldName("object");
313
+ const propNode = fnNode.childForFieldName("property");
314
+ const argsNode = node.childForFieldName("arguments");
315
+
316
+ if (objNode && propNode && argsNode) {
317
+ const callerVar = objNode.text.trim();
318
+ const methodName = propNode.text.trim();
319
+ const lowerMethod = methodName.toLowerCase();
320
+ const args = argsNode.namedChildren;
321
+
322
+ // Route Mount / Plugin: app.use(subPlugin)
323
+ if (lowerMethod === "use") {
324
+ if (args.length >= 1 && args[0].type === "identifier") {
325
+ mounts.push({
326
+ file,
327
+ relPath,
328
+ callerVar,
329
+ prefix: "",
330
+ targetVar: args[0].text.trim(),
331
+ line: node.startPosition.row + 1,
332
+ });
333
+ }
334
+ } else if (lowerMethod === "group") {
335
+ // Group scoping: app.group('/v1', (app) => ...)
336
+ if (args.length >= 2) {
337
+ const prefixLit = cleanLiteral(args[0]);
338
+ const groupFn = args[1];
339
+ if (prefixLit !== undefined && groupFn) {
340
+ // Trace routes inside the group callback
341
+ let scopedVar = callerVar;
342
+ if (groupFn.type === "arrow_function" || groupFn.type === "function") {
343
+ const paramsNode = groupFn.childForFieldName("parameters");
344
+ if (paramsNode && paramsNode.namedChildren[0]) {
345
+ scopedVar = paramsNode.namedChildren[0].text.trim();
346
+ }
347
+ }
348
+ mounts.push({
349
+ file,
350
+ relPath,
351
+ callerVar,
352
+ prefix: prefixLit,
353
+ targetVar: scopedVar,
354
+ line: node.startPosition.row + 1,
355
+ });
356
+ }
357
+ }
358
+ } else if (ELYSIA_VERBS.has(lowerMethod)) {
359
+ // Direct route: app.get('/path', handler, options) or app.post('/path', { body: ... }, handler)
360
+ if (args.length >= 2) {
361
+ const rawPath = cleanLiteral(args[0]);
362
+ if (rawPath !== undefined) {
363
+ let handlerNode: Parser.SyntaxNode | undefined;
364
+ let optionsNode: Parser.SyntaxNode | undefined;
365
+
366
+ for (let i = 1; i < args.length; i++) {
367
+ const arg = args[i];
368
+ if (arg.type === "object") {
369
+ optionsNode = arg;
370
+ } else if (
371
+ arg.type === "identifier" ||
372
+ arg.type === "member_expression" ||
373
+ arg.type === "arrow_function" ||
374
+ arg.type === "function" ||
375
+ arg.type === "function_expression"
376
+ ) {
377
+ handlerNode = arg;
378
+ }
379
+ }
380
+
381
+ if (!handlerNode && args.length >= 2) {
382
+ handlerNode = args[1];
383
+ }
384
+
385
+ const validationSchema = this.extractValidationFromOptions(optionsNode, resolvedImports);
386
+ const handler = this.extractSingleHandler(handlerNode || args[1]);
387
+
388
+ routes.push({
389
+ file,
390
+ relPath,
391
+ routerVar: callerVar,
392
+ httpMethods: lowerMethod === "all" ? ["GET", "POST", "PUT", "DELETE"] : [lowerMethod.toUpperCase()],
393
+ rawPath,
394
+ validationSchema,
395
+ handler,
396
+ routeLine: node.startPosition.row + 1,
397
+ });
398
+ }
399
+ }
400
+ }
401
+ }
402
+ }
403
+ }
404
+
405
+ for (const child of node.namedChildren) {
406
+ walkCalls(child);
407
+ }
408
+ };
409
+
410
+ walkCalls(rootNode);
411
+ return { routes, mounts, resolvedImports };
412
+ }
413
+
414
+ private extractValidationFromOptions(
415
+ optsNode: Parser.SyntaxNode | undefined,
416
+ resolvedImports: Map<string, string>
417
+ ): { symbol: string; file?: string; line?: number } | undefined {
418
+ if (!optsNode || optsNode.type !== "object") return undefined;
419
+
420
+ for (const pair of optsNode.namedChildren) {
421
+ if (pair.type === "pair") {
422
+ const keyNode = pair.childForFieldName("key");
423
+ const valNode = pair.childForFieldName("value");
424
+ const keyText = keyNode ? keyNode.text.trim().replace(/^['"]|['"]$/g, "") : "";
425
+
426
+ if (keyText === "body" && valNode) {
427
+ const schemaSymbol = valNode.text.trim();
428
+ const schemaIdent = schemaSymbol.replace(/^t\.[a-zA-Z0-9_]+\s*\(/, "").replace(/\)$/, "").trim();
429
+ const targetIdent = schemaIdent.split(/[<({]/)[0].trim();
430
+ const schemaFile = resolvedImports.get(targetIdent);
431
+
432
+ return {
433
+ symbol: targetIdent || schemaSymbol,
434
+ file: schemaFile,
435
+ line: pair.startPosition.row + 1,
436
+ };
437
+ }
438
+ }
439
+ }
440
+
441
+ return undefined;
442
+ }
443
+
444
+ private extractSingleHandler(node: Parser.SyntaxNode): { symbol: string; line: number; node: Parser.SyntaxNode } {
445
+ const line = node.startPosition.row + 1;
446
+ if (node.type === "identifier" || node.type === "member_expression") {
447
+ return { symbol: node.text.trim(), line, node };
448
+ }
449
+ if (node.type === "arrow_function" || node.type === "function_expression" || node.type === "function") {
450
+ const nameNode = node.childForFieldName("name");
451
+ const name = nameNode ? nameNode.text.trim() : "closure";
452
+ return { symbol: name, line, node };
453
+ }
454
+ return { symbol: "closure", line, node };
455
+ }
456
+
457
+ private resolvePrefixHierarchy(
458
+ allMounts: ExtractedMount[],
459
+ fileImports: Map<string, Map<string, string>>
460
+ ): Map<string, string[]> {
461
+ const parentMap = new Map<string, Array<{ parentKey: string; prefix: string }>>();
462
+ const allRouterKeys = new Set<string>();
463
+
464
+ for (const m of allMounts) {
465
+ const parentKey = `${m.relPath}#${m.callerVar}`;
466
+ allRouterKeys.add(parentKey);
467
+
468
+ let targetKey: string | undefined;
469
+ const importedFile = fileImports.get(m.relPath)?.get(m.targetVar);
470
+ if (importedFile) {
471
+ targetKey = `${importedFile}#${m.targetVar}`;
472
+ } else {
473
+ targetKey = `${m.relPath}#${m.targetVar}`;
474
+ }
475
+
476
+ allRouterKeys.add(targetKey);
477
+ if (!parentMap.has(targetKey)) parentMap.set(targetKey, []);
478
+ parentMap.get(targetKey)!.push({ parentKey, prefix: m.prefix });
479
+ }
480
+
481
+ const prefixesByRouter = new Map<string, string[]>();
482
+
483
+ for (const rKey of allRouterKeys) {
484
+ const parents = parentMap.get(rKey);
485
+ if (!parents || parents.length === 0) {
486
+ prefixesByRouter.set(rKey, [""]);
487
+ }
488
+ }
489
+
490
+ const queue = Array.from(prefixesByRouter.keys());
491
+ const visitedEdges = new Set<string>();
492
+
493
+ while (queue.length > 0) {
494
+ const current = queue.shift()!;
495
+ const currentPrefixes = prefixesByRouter.get(current) ?? [""];
496
+
497
+ for (const [targetKey, parents] of parentMap.entries()) {
498
+ for (const p of parents) {
499
+ if (p.parentKey === current) {
500
+ const edgeKey = `${current}->${targetKey}:${p.prefix}`;
501
+ if (visitedEdges.has(edgeKey)) continue;
502
+ visitedEdges.add(edgeKey);
503
+
504
+ const nextPrefixes = currentPrefixes.map((cp) => normalizeRouteUri(cp, p.prefix));
505
+ const existing = prefixesByRouter.get(targetKey) ?? [];
506
+ const merged = Array.from(new Set([...existing, ...nextPrefixes]));
507
+ prefixesByRouter.set(targetKey, merged);
508
+ queue.push(targetKey);
509
+ }
510
+ }
511
+ }
512
+ }
513
+
514
+ return prefixesByRouter;
515
+ }
516
+ }
517
+