diff --git a/packages/repo-map/src/symbols/detectFrameworkRoutes.ts b/packages/repo-map/src/symbols/detectFrameworkRoutes.ts index a58a805..1d07e89 100644 --- a/packages/repo-map/src/symbols/detectFrameworkRoutes.ts +++ b/packages/repo-map/src/symbols/detectFrameworkRoutes.ts @@ -1,6 +1,119 @@ import type { WorkspaceScanResult, RepoMapRoute, RepoMapFile } from "@wma/core"; import { readFileSync } from "node:fs"; +function maskCommentsAndStrings(content: string, extension: string): string { + const masked = content.split(""); + const isPython = extension === ".py"; + + let state: "code" | "line" | "block" | "string" = "code"; + let quote = ""; + let triple = false; + + for (let i = 0; i < content.length; i++) { + const char = content[i]; + const next = content[i + 1]; + const nextTwo = content[i + 2]; + + if (state === "line") { + if (char === "\n" || char === "\r") { + state = "code"; + } else { + masked[i] = " "; + } + continue; + } + + if (state === "block") { + if (char === "*" && next === "/") { + masked[i] = " "; + masked[i + 1] = " "; + i++; + state = "code"; + } else if (char !== "\n" && char !== "\r") { + masked[i] = " "; + } + continue; + } + + if (state === "string") { + if (char === "\\") { + masked[i] = " "; + if (i + 1 < content.length) { + if (next !== "\n" && next !== "\r") { + masked[i + 1] = " "; + } + i++; + } + continue; + } + + if ( + triple && + char === quote && + next === quote && + nextTwo === quote + ) { + masked[i] = " "; + masked[i + 1] = " "; + masked[i + 2] = " "; + i += 2; + state = "code"; + continue; + } + + if (!triple && char === quote) { + masked[i] = " "; + state = "code"; + continue; + } + + if (char !== "\n" && char !== "\r") { + masked[i] = " "; + } + continue; + } + + if (isPython && char === "#") { + masked[i] = " "; + state = "line"; + continue; + } + + if (!isPython && char === "/" && next === "/") { + masked[i] = " "; + masked[i + 1] = " "; + i++; + state = "line"; + continue; + } + + if (!isPython && char === "/" && next === "*") { + masked[i] = " "; + masked[i + 1] = " "; + i++; + state = "block"; + continue; + } + + if (char === "'" || char === '"' || (!isPython && char === "`")) { + quote = char; + triple = char !== "`" && next === char && nextTwo === char; + + masked[i] = " "; + + if (triple) { + masked[i + 1] = " "; + masked[i + 2] = " "; + i += 2; + } + + state = "string"; + } + } + + return masked.join(""); +} + function hasNextProjectSignal( root: string, pathSet: Set, @@ -116,15 +229,28 @@ export function detectFrameworkRoutes( for (const file of scanResult.files) { if (!pathSet.has(file.relativePath)) continue; if (![".ts", ".tsx", ".js", ".jsx"].includes(file.extension)) continue; + try { const content = readFileSync(file.path, "utf-8"); - const routeRegex = /(?:app|router)\.(get|post|put|delete|patch|use)\s*\(\s*['"`]([^'"`]+)['"`]/g; + const codeMask = maskCommentsAndStrings(content, file.extension); + + const routeRegex = + /(?:app|router)\.(get|post|put|delete|patch|use)\s*\(\s*['"`]([^'"`]+)['"`]/g; + let m: RegExpExecArray | null; + while ((m = routeRegex.exec(content)) !== null) { - routes.push({ relativePath: file.relativePath, routePattern: m[2], framework: "express", reason: `${m[1].toUpperCase()} ${m[2]}` }); + if (codeMask[m.index] === " ") continue; + + routes.push({ + relativePath: file.relativePath, + routePattern: m[2], + framework: "express", + reason: `${m[1].toUpperCase()} ${m[2]}`, + }); } } catch { - // Skip files that can't be read + // Skip files that can't be read. } } @@ -132,6 +258,7 @@ export function detectFrameworkRoutes( if (!pathSet.has(file.relativePath) || ![".py", ".java", ".kt"].includes(file.extension)) continue; try { const content = readFileSync(file.path, "utf-8"); + const codeMask = maskCommentsAndStrings(content, file.extension); const patterns = file.extension === ".py" ? [ { regex: /@(app|router)\.(get|post|put|delete|patch)\s*\(\s*['"]([^'"]+)['"]/g, framework: "fastapi" as const }, @@ -141,6 +268,8 @@ export function detectFrameworkRoutes( for (const { regex, framework } of patterns) { let match: RegExpExecArray | null; while ((match = regex.exec(content)) !== null) { + if (codeMask[match.index] === " ") continue; + const route = match[3] ?? match[2] ?? match[1]; routes.push({ relativePath: file.relativePath, routePattern: route, framework, reason: `${framework} route: ${route}` }); } diff --git a/packages/repo-map/tests/symbols/detectFrameworkRoutes.test.ts b/packages/repo-map/tests/symbols/detectFrameworkRoutes.test.ts index 3794b8f..b640a8c 100644 --- a/packages/repo-map/tests/symbols/detectFrameworkRoutes.test.ts +++ b/packages/repo-map/tests/symbols/detectFrameworkRoutes.test.ts @@ -73,4 +73,55 @@ describe("detectFrameworkRoutes", () => { const patterns = detectFrameworkRoutes(scan(files), files.map((f) => makeRepoMapFile(f.relativePath))).map((r) => r.routePattern); expect(patterns).toContain("/api/health"); expect(patterns).toContain("/api/data"); expect(patterns).toContain("/users"); }); + + it("ignores Express routes inside comments and string literals", () => { + const content = ` + // app.get("/api/commented", (req, res) => {}); + const singleQuotedExample = 'app.post("/api/single-string", handler)'; + const doubleQuotedExample = "router.delete('/api/double-string', handler)"; + const templateExample = \`app.put("/api/template-string", handler)\`; + + app.get("/api/real", (req, res) => {}); +`; + writeFileSync(join(tempDir, "server-examples.ts"), content, "utf-8"); + + const files = [makeFileInfo("server-examples.ts", ".ts", content)]; + const routes = detectFrameworkRoutes( + scan(files), + files.map((f) => makeRepoMapFile(f.relativePath)), + ); + + expect(routes.map((route) => route.routePattern)).toEqual(["/api/real"]); + }); + + it("ignores Python decorator routes inside comments and strings", () => { + const content = ` +# @app.get("/commented") +singleExample = '@app.post("/single-string")' +doubleExample = "@router.delete('/double-string')" +tripleExample = """ +@app.put("/triple-string") +""" + +@app.get("/real-fastapi") +def real_fastapi(): + pass + +@app.route("/real-flask") +def real_flask(): + pass +`; + writeFileSync(join(tempDir, "routes.py"), content, "utf-8"); + + const files = [makeFileInfo("routes.py", ".py", content)]; + const routes = detectFrameworkRoutes( + scan(files), + files.map((f) => makeRepoMapFile(f.relativePath)), + ); + + expect(routes.map((route) => route.routePattern)).toEqual([ + "/real-fastapi", + "/real-flask", + ]); + }); });