Skip to content

Commit a1d1f7b

Browse files
♻️ Cleanup
1 parent 025d210 commit a1d1f7b

8 files changed

Lines changed: 26 additions & 79 deletions

File tree

src/appDiscovery.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ import type { Parser } from "./core/parser"
1010
import { findProjectRoot, uriPath } from "./core/pathUtils"
1111
import { buildRouterGraph } from "./core/routerResolver"
1212
import { routerNodeToAppDefinition } from "./core/transformer"
13-
import {
14-
collectAllRoutes,
15-
countRouters,
16-
countRoutesInRouter,
17-
} from "./core/treeUtils"
13+
import { collectRoutes, countRouters } from "./core/treeUtils"
1814
import type { AppDefinition } from "./core/types"
1915
import { vscodeFileSystem } from "./providers/vscodeFileSystem"
2016
import { log } from "./utils/logger"
@@ -176,9 +172,7 @@ export async function discoverFastAPIApps(
176172

177173
if (routerNode) {
178174
const app = routerNodeToAppDefinition(routerNode, folder.uri.fsPath)
179-
const totalRoutes =
180-
app.routes.length +
181-
app.routers.reduce((sum, r) => sum + countRoutesInRouter(r), 0)
175+
const totalRoutes = collectRoutes([app]).length
182176
log(
183177
`Found FastAPI app "${app.name}" with ${totalRoutes} route(s) in ${app.routers.length} router(s)`,
184178
)
@@ -193,7 +187,7 @@ export async function discoverFastAPIApps(
193187
duration_ms: folderTimer(),
194188
method: detectionMethod,
195189
success: folderApps.length > 0,
196-
routes_count: collectAllRoutes(folderApps).length,
190+
routes_count: collectRoutes(folderApps).length,
197191
routers_count: countRouters(folderApps),
198192
})
199193
}

src/core/index.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@ export { Parser } from "./parser"
1111
export { findProjectRoot } from "./pathUtils"
1212
export { buildRouterGraph, type RouterNode } from "./routerResolver"
1313
export { routerNodeToAppDefinition } from "./transformer"
14-
export {
15-
collectAllRoutes,
16-
countRouters,
17-
countRoutesInRouter,
18-
findRouter,
19-
} from "./treeUtils"
14+
export { collectRoutes, countRouters, findRouter } from "./treeUtils"
2015
export type {
2116
AppDefinition,
2217
HTTPMethod,

src/core/treeUtils.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,13 @@ function collectRoutesFromRouters(
5757
/**
5858
* Collects all routes from all apps, including nested router routes.
5959
*/
60-
export function collectAllRoutes(apps: AppDefinition[]): RouteDefinition[] {
60+
export function collectRoutes(apps: AppDefinition[]): RouteDefinition[] {
6161
return apps.flatMap((app) => [
6262
...app.routes,
6363
...collectRoutesFromRouters(app.routers),
6464
])
6565
}
6666

67-
/**
68-
* Counts total routes in a router tree (including nested children).
69-
*/
70-
export function countRoutesInRouter(router: RouterDefinition): number {
71-
return (
72-
router.routes.length +
73-
router.children.reduce((sum, child) => sum + countRoutesInRouter(child), 0)
74-
)
75-
}
76-
7767
/**
7868
* Counts total routers across all apps.
7969
*/

src/extension.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { discoverFastAPIApps } from "./appDiscovery"
77
import { clearImportCache } from "./core/importResolver"
88
import { Parser } from "./core/parser"
99
import { stripLeadingDynamicSegments } from "./core/pathUtils"
10-
import { collectAllRoutes, countRouters } from "./core/treeUtils"
10+
import { collectRoutes, countRouters } from "./core/treeUtils"
1111
import type { AppDefinition, SourceLocation } from "./core/types"
1212
import {
1313
type EndpointTreeItem,
@@ -109,7 +109,7 @@ export async function activate(context: vscode.ExtensionContext) {
109109
trackActivation({
110110
duration_ms: elapsed(),
111111
success,
112-
routes_count: collectAllRoutes(apps).length,
112+
routes_count: collectRoutes(apps).length,
113113
routers_count: countRouters(apps),
114114
apps_count: apps.length,
115115
workspace_folder_count: vscode.workspace.workspaceFolders?.length ?? 0,
@@ -234,7 +234,7 @@ function registerCommands(
234234
async () => {
235235
const workspacePrefix =
236236
vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? ""
237-
const items = collectAllRoutes(endpointProvider.getApps())
237+
const items = collectRoutes(endpointProvider.getApps())
238238
.map((route) => {
239239
const path = stripLeadingDynamicSegments(route.path)
240240
return {

src/providers/endpointTreeProvider.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
TreeItemCollapsibleState,
88
} from "vscode"
99
import { stripLeadingDynamicSegments } from "../core/pathUtils"
10-
import { countRoutesInRouter, findRouter } from "../core/treeUtils"
10+
import { findRouter } from "../core/treeUtils"
1111
import type {
1212
AppDefinition,
1313
RouteDefinition,
@@ -319,7 +319,10 @@ export class EndpointTreeProvider
319319
routerItem.id = `router-${element.router.location.filePath}-${element.router.prefix}-${this.toggleCount}`
320320
routerItem.iconPath = new ThemeIcon("symbol-namespace")
321321

322-
const totalRoutes = countRoutesInRouter(element.router)
322+
const countRoutes = (r: RouterDefinition): number =>
323+
r.routes.length +
324+
r.children.reduce((sum, child) => sum + countRoutes(child), 0)
325+
const totalRoutes = countRoutes(element.router)
323326
routerItem.description =
324327
totalRoutes !== 1 ? `${totalRoutes} routes` : "1 route"
325328
routerItem.contextValue = "router"

src/providers/testCodeLensProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
pathMatchesEndpoint,
2222
stripLeadingDynamicSegments,
2323
} from "../core/pathUtils"
24-
import { collectAllRoutes } from "../core/treeUtils"
24+
import { collectRoutes } from "../core/treeUtils"
2525
import type { AppDefinition, SourceLocation } from "../core/types"
2626
import { trackCodeLensProvided } from "../utils/telemetry"
2727

@@ -158,7 +158,7 @@ export class TestCodeLensProvider implements CodeLensProvider {
158158
testPath: string,
159159
testMethod: string,
160160
): SourceLocation[] {
161-
return collectAllRoutes(this.apps)
161+
return collectRoutes(this.apps)
162162
.filter(
163163
(route) =>
164164
route.method.toLowerCase() === testMethod.toLowerCase() &&

src/test/core/treeUtils.test.ts

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import * as assert from "node:assert"
2-
import {
3-
collectAllRoutes,
4-
countRouters,
5-
countRoutesInRouter,
6-
findRouter,
7-
} from "../../core/treeUtils"
2+
import { collectRoutes, countRouters, findRouter } from "../../core/treeUtils"
83
import type {
94
AppDefinition,
105
RouteDefinition,
@@ -96,17 +91,17 @@ suite("treeUtils", () => {
9691
})
9792
})
9893

99-
suite("collectAllRoutes", () => {
94+
suite("collectRoutes", () => {
10095
test("returns empty array for empty apps", () => {
101-
const result = collectAllRoutes([])
96+
const result = collectRoutes([])
10297
assert.deepStrictEqual(result, [])
10398
})
10499

105100
test("collects direct app routes", () => {
106101
const route = makeRoute("GET", "/")
107102
const apps = [makeApp("app", [route])]
108103

109-
const result = collectAllRoutes(apps)
104+
const result = collectRoutes(apps)
110105
assert.deepStrictEqual(result, [route])
111106
})
112107

@@ -121,7 +116,7 @@ suite("treeUtils", () => {
121116
),
122117
]
123118

124-
const result = collectAllRoutes(apps)
119+
const result = collectRoutes(apps)
125120
assert.deepStrictEqual(result, [directRoute, routerRoute])
126121
})
127122

@@ -130,41 +125,11 @@ suite("treeUtils", () => {
130125
const route2 = makeRoute("GET", "/app2")
131126
const apps = [makeApp("app1", [route1]), makeApp("app2", [route2])]
132127

133-
const result = collectAllRoutes(apps)
128+
const result = collectRoutes(apps)
134129
assert.deepStrictEqual(result, [route1, route2])
135130
})
136131
})
137132

138-
suite("countRoutesInRouter", () => {
139-
test("returns 0 for router with no routes", () => {
140-
const router = makeRouter("empty", "/empty")
141-
assert.strictEqual(countRoutesInRouter(router), 0)
142-
})
143-
144-
test("counts direct routes", () => {
145-
const router = makeRouter("r", "/r", [
146-
makeRoute("GET", "/r"),
147-
makeRoute("POST", "/r"),
148-
])
149-
assert.strictEqual(countRoutesInRouter(router), 2)
150-
})
151-
152-
test("counts routes in nested children", () => {
153-
const router = makeRouter(
154-
"parent",
155-
"/parent",
156-
[makeRoute("GET", "/parent")],
157-
[
158-
makeRouter("child", "/parent/child", [
159-
makeRoute("GET", "/parent/child"),
160-
makeRoute("POST", "/parent/child"),
161-
]),
162-
],
163-
)
164-
assert.strictEqual(countRoutesInRouter(router), 3)
165-
})
166-
})
167-
168133
suite("countRouters", () => {
169134
test("returns 0 for empty apps", () => {
170135
assert.strictEqual(countRouters([]), 0)

src/test/layouts.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Parser } from "../core/parser"
33
import { findProjectRoot } from "../core/pathUtils"
44
import { buildRouterGraph } from "../core/routerResolver"
55
import { routerNodeToAppDefinition } from "../core/transformer"
6-
import { collectAllRoutes } from "../core/treeUtils"
6+
import { collectRoutes } from "../core/treeUtils"
77
import { fixtures, nodeFileSystem, wasmBinaries } from "./testUtils"
88

99
suite("Project Layouts", () => {
@@ -34,7 +34,7 @@ suite("Project Layouts", () => {
3434
assert.ok(graph, "Should find FastAPI app")
3535

3636
const appDef = routerNodeToAppDefinition(graph, fixtures.standard.root)
37-
const allRoutes = collectAllRoutes([appDef])
37+
const allRoutes = collectRoutes([appDef])
3838

3939
// Should have: GET /, GET /health, GET /users/, GET /users/{user_id}, POST /users/, GET /items/, GET /items/{item_id}
4040
assert.strictEqual(
@@ -78,7 +78,7 @@ suite("Project Layouts", () => {
7878
assert.ok(graph, "Should find FastAPI app")
7979

8080
const appDef = routerNodeToAppDefinition(graph, fixtures.flat.root)
81-
const allRoutes = collectAllRoutes([appDef])
81+
const allRoutes = collectRoutes([appDef])
8282

8383
// Should have: GET /, GET /api/users, GET /api/items
8484
assert.strictEqual(
@@ -118,7 +118,7 @@ suite("Project Layouts", () => {
118118
assert.ok(graph, "Should find FastAPI app")
119119

120120
const appDef = routerNodeToAppDefinition(graph, fixtures.namespace.root)
121-
const allRoutes = collectAllRoutes([appDef])
121+
const allRoutes = collectRoutes([appDef])
122122

123123
// Should have: GET /, GET /users/, GET /users/{user_id}, GET /items/
124124
assert.strictEqual(
@@ -158,7 +158,7 @@ suite("Project Layouts", () => {
158158
assert.ok(graph, "Should find FastAPI app")
159159

160160
const appDef = routerNodeToAppDefinition(graph, fixtures.reexport.root)
161-
const allRoutes = collectAllRoutes([appDef])
161+
const allRoutes = collectRoutes([appDef])
162162

163163
// Should have: GET /, GET /integrations/github, GET /integrations/slack, POST /integrations/webhook,
164164
// and nested neon routes: GET /integrations/neon/, POST /integrations/neon/connect

0 commit comments

Comments
 (0)