Skip to content

Commit daa7acc

Browse files
More TS syntactic sugar
1 parent 082959b commit daa7acc

4 files changed

Lines changed: 54 additions & 91 deletions

File tree

src/core/importResolver.ts

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ import type { Parser } from "./parser"
2424
const fileExistsCache = new Map<string, boolean>()
2525

2626
function cachedExistsSync(path: string): boolean {
27-
const cached = fileExistsCache.get(path)
28-
if (cached !== undefined) {
29-
return cached
27+
if (fileExistsCache.has(path)) {
28+
return fileExistsCache.get(path)!
3029
}
3130
const exists = existsSync(path)
3231
fileExistsCache.set(path, exists)
@@ -55,25 +54,16 @@ function resolvePythonModule(basePath: string): string | null {
5554
return null
5655
}
5756

58-
/**
59-
* Finds an import that provides a given exported name
60-
* Used for resolving re-exports in __init__.py files
61-
**/
57+
/** Finds an import that provides a given exported name (for re-exports in __init__.py) */
6258
function findImportByExportedName(
6359
imports: ImportInfo[],
6460
name: string,
65-
): ImportInfo | null {
66-
// Each import may provide multiple named imports
67-
// e.g. from module import A as B, C
68-
for (const imp of imports) {
69-
for (const namedImport of imp.namedImports) {
70-
const providedName = namedImport.alias ?? namedImport.name
71-
if (providedName === name) {
72-
return imp
73-
}
74-
}
75-
}
76-
return null
61+
): ImportInfo | undefined {
62+
return imports.find((imp) =>
63+
imp.namedImports.some(
64+
(namedImport) => (namedImport.alias ?? namedImport.name) === name,
65+
),
66+
)
7767
}
7868

7969
/**
@@ -190,19 +180,11 @@ export function resolveRouterFromInit(
190180
}
191181

192182
const analysis = analyzeFile(initFilePath, parser)
193-
if (!analysis) {
194-
return null
195-
}
196-
197-
// If this file has routers defined, no need to follow re-exports
198-
if (analysis.routers.length > 0) {
183+
// If file has routers defined, no need to follow re-exports
184+
if (!analysis || analysis.routers.length > 0) {
199185
return null
200186
}
201187

202188
const imp = findImportByExportedName(analysis.imports, "router")
203-
if (imp) {
204-
return resolveImport(imp, initFilePath, projectRoot)
205-
}
206-
207-
return null
189+
return imp ? resolveImport(imp, initFilePath, projectRoot) : null
208190
}

src/core/internal.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,11 @@ export const ROUTE_METHODS = new Set([
2020
"websocket",
2121
])
2222

23-
/**
24-
* Normalizes a method string to a valid RouteMethod.
25-
* Returns "GET" as default for invalid methods.
26-
*/
23+
/** Normalizes a method string to a valid RouteMethod. Returns "GET" for invalid methods. */
2724
export function normalizeMethod(method: string): RouteMethod {
28-
const lower = method.toLowerCase()
29-
if (ROUTE_METHODS.has(lower)) {
30-
return method.toUpperCase() as RouteMethod
31-
}
32-
return "GET"
25+
return ROUTE_METHODS.has(method.toLowerCase())
26+
? (method.toUpperCase() as RouteMethod)
27+
: "GET"
3328
}
3429

3530
export interface RouteInfo {

src/core/routerResolver.ts

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,11 @@ function buildRouterGraphInternal(
3636
): RouterNode | null {
3737
// Resolve the full path of the entry file if necessary
3838
let resolvedEntryFile = entryFile
39+
if (!existsSync(resolvedEntryFile) && !isAbsolute(entryFile)) {
40+
resolvedEntryFile = join(projectRoot, entryFile)
41+
}
3942
if (!existsSync(resolvedEntryFile)) {
40-
// Only try joining if entryFile is not already absolute
41-
if (!isAbsolute(entryFile)) {
42-
resolvedEntryFile = join(projectRoot, entryFile)
43-
}
44-
if (!existsSync(resolvedEntryFile)) {
45-
return null
46-
}
43+
return null
4744
}
4845

4946
// Prevent infinite recursion on circular imports
@@ -116,17 +113,18 @@ function buildRouterGraphInternal(
116113
parser,
117114
visited,
118115
)
119-
if (childRouter) {
120-
// Merge tags from include_router call with the router's own tags
121-
if (include.tags.length > 0) {
122-
childRouter.tags = [...new Set([...childRouter.tags, ...include.tags])]
123-
}
124-
rootRouter.children.push({
125-
router: childRouter,
126-
prefix: include.prefix,
127-
tags: include.tags,
128-
})
116+
if (!childRouter) {
117+
continue
118+
}
119+
// Merge tags from include_router call with the router's own tags
120+
if (include.tags.length > 0) {
121+
childRouter.tags = [...new Set([...childRouter.tags, ...include.tags])]
129122
}
123+
rootRouter.children.push({
124+
router: childRouter,
125+
prefix: include.prefix,
126+
tags: include.tags,
127+
})
130128
}
131129

132130
// Process mount() calls for subapps
@@ -139,13 +137,14 @@ function buildRouterGraphInternal(
139137
parser,
140138
visited,
141139
)
142-
if (childRouter) {
143-
rootRouter.children.push({
144-
router: childRouter,
145-
prefix: mount.path,
146-
tags: [],
147-
})
140+
if (!childRouter) {
141+
continue
148142
}
143+
rootRouter.children.push({
144+
router: childRouter,
145+
prefix: mount.path,
146+
tags: [],
147+
})
149148
}
150149

151150
return rootRouter
@@ -168,7 +167,6 @@ function resolveRouterReference(
168167
const matchingImport = analysis.imports.find((imp) =>
169168
imp.names.includes(moduleName),
170169
)
171-
172170
if (!matchingImport) {
173171
return null
174172
}
@@ -184,7 +182,6 @@ function resolveRouterReference(
184182
projectRoot,
185183
parser,
186184
)
187-
188185
if (!importedFilePath) {
189186
return null
190187
}

src/providers/EndpointTreeProvider.ts

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ export class EndpointTreeProvider
9393
return `${route.method} ${path}`.toLowerCase()
9494
}
9595

96-
/**
97-
* Counts total routes including nested children.
98-
*/
96+
/** Counts total routes including nested children. */
9997
private getTotalRouteCount(router: RouterDefinition): number {
100-
let count = router.routes.length
101-
for (const child of router.children) {
102-
count += this.getTotalRouteCount(child)
103-
}
104-
return count
98+
return (
99+
router.routes.length +
100+
router.children.reduce(
101+
(sum, child) => sum + this.getTotalRouteCount(child),
102+
0,
103+
)
104+
)
105105
}
106106

107107
/**
@@ -206,13 +206,10 @@ export class EndpointTreeProvider
206206
case "app": {
207207
// Check if apps are grouped under workspaces
208208
const rootItems = this.groupApps(this.apps)
209-
for (const root of rootItems) {
210-
if (root.type === "workspace" && root.apps.includes(element.app)) {
211-
return root
212-
}
213-
}
214-
// App is at root level
215-
return undefined
209+
return rootItems.find(
210+
(root) =>
211+
root.type === "workspace" && root.apps.includes(element.app),
212+
)
216213
}
217214

218215
case "router": {
@@ -222,12 +219,8 @@ export class EndpointTreeProvider
222219
return { type: "router", router: parentRouter }
223220
}
224221
// Find which app contains this router at top level
225-
for (const app of this.apps) {
226-
if (app.routers.includes(element.router)) {
227-
return { type: "app", app }
228-
}
229-
}
230-
return undefined
222+
const app = this.apps.find((a) => a.routers.includes(element.router))
223+
return app ? { type: "app", app } : undefined
231224
}
232225

233226
case "route": {
@@ -237,12 +230,8 @@ export class EndpointTreeProvider
237230
return { type: "router", router: parentRouter }
238231
}
239232
// Check if route is directly on an app
240-
for (const app of this.apps) {
241-
if (app.routes.includes(element.route)) {
242-
return { type: "app", app }
243-
}
244-
}
245-
return undefined
233+
const app = this.apps.find((a) => a.routes.includes(element.route))
234+
return app ? { type: "app", app } : undefined
246235
}
247236
}
248237
}

0 commit comments

Comments
 (0)