Skip to content

Commit 8931742

Browse files
♻️ Extract router node creation to deduplicate (#112)
1 parent 2050edc commit 8931742

1 file changed

Lines changed: 42 additions & 55 deletions

File tree

src/core/routerResolver.ts

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import { log } from "../utils/logger"
22
import { analyzeFile } from "./analyzer"
33
import type { FileSystem } from "./filesystem"
44
import { resolveNamedImport, resolveRouterFromInit } from "./importResolver"
5-
import type { FileAnalysis, RouterInfo, RouterNode } from "./internal"
5+
import type {
6+
FileAnalysis,
7+
RouteInfo,
8+
RouterInfo,
9+
RouterNode,
10+
} from "./internal"
611
import type { Parser } from "./parser"
712

813
export type { RouterNode }
@@ -25,6 +30,31 @@ function findAppRouter(
2530
)
2631
}
2732

33+
function createRouterNode(
34+
router: RouterInfo,
35+
routes: RouteInfo[],
36+
filePath: string,
37+
): RouterNode {
38+
return {
39+
filePath,
40+
variableName: router.variableName,
41+
type: router.type,
42+
prefix: router.prefix,
43+
tags: router.tags,
44+
line: router.line,
45+
column: router.column,
46+
routes: routes.map((r) => ({
47+
method: r.method,
48+
path: r.path,
49+
function: r.function,
50+
line: r.line,
51+
column: r.column,
52+
docstring: r.docstring,
53+
})),
54+
children: [],
55+
}
56+
}
57+
2858
/**
2959
* Builds a router graph starting from the given entry file.
3060
* If targetVariable is specified, only that specific app/router will be used.
@@ -141,24 +171,7 @@ async function buildRouterGraphInternal(
141171
const appRoutes = analysis.routes.filter(
142172
(r) => r.owner === appRouter.variableName,
143173
)
144-
const rootRouter: RouterNode = {
145-
filePath: resolvedEntryUri,
146-
variableName: appRouter.variableName,
147-
type: appRouter.type,
148-
prefix: appRouter.prefix,
149-
tags: appRouter.tags,
150-
line: appRouter.line,
151-
column: appRouter.column,
152-
routes: appRoutes.map((r) => ({
153-
method: r.method,
154-
path: r.path,
155-
function: r.function,
156-
line: r.line,
157-
column: r.column,
158-
docstring: r.docstring,
159-
})),
160-
children: [],
161-
}
174+
const rootRouter = createRouterNode(appRouter, appRoutes, resolvedEntryUri)
162175

163176
// Process include_router calls to find child routers
164177
for (const include of analysis.includeRouters) {
@@ -238,24 +251,11 @@ async function resolveRouterReference(
238251
if (localRouter) {
239252
// Filter routes that belong to this router (decorated with @router.method)
240253
const routerRoutes = analysis.routes.filter((r) => r.owner === moduleName)
241-
const routerNode: RouterNode = {
242-
filePath: currentFileUri,
243-
variableName: localRouter.variableName,
244-
type: localRouter.type,
245-
prefix: localRouter.prefix,
246-
tags: localRouter.tags,
247-
line: localRouter.line,
248-
column: localRouter.column,
249-
routes: routerRoutes.map((r) => ({
250-
method: r.method,
251-
path: r.path,
252-
function: r.function,
253-
line: r.line,
254-
column: r.column,
255-
docstring: r.docstring,
256-
})),
257-
children: [],
258-
}
254+
const routerNode = createRouterNode(
255+
localRouter,
256+
routerRoutes,
257+
currentFileUri,
258+
)
259259

260260
// Process include_router calls owned by this router (nested routers)
261261
const routerIncludes = analysis.includeRouters.filter(
@@ -355,24 +355,11 @@ async function resolveRouterReference(
355355
const routerRoutes = importedAnalysis.routes.filter(
356356
(r) => r.owner === attributeName,
357357
)
358-
const routerNode: RouterNode = {
359-
filePath: importedFileUri,
360-
variableName: targetRouter.variableName,
361-
type: targetRouter.type,
362-
prefix: targetRouter.prefix,
363-
tags: targetRouter.tags,
364-
line: targetRouter.line,
365-
column: targetRouter.column,
366-
routes: routerRoutes.map((r) => ({
367-
method: r.method,
368-
path: r.path,
369-
function: r.function,
370-
line: r.line,
371-
column: r.column,
372-
docstring: r.docstring,
373-
})),
374-
children: [],
375-
}
358+
const routerNode = createRouterNode(
359+
targetRouter,
360+
routerRoutes,
361+
importedFileUri,
362+
)
376363

377364
// Process include_router calls owned by this router (nested routers)
378365
const routerIncludes = importedAnalysis.includeRouters.filter(

0 commit comments

Comments
 (0)