MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / postExtract

Function postExtract

src/resolution/frameworks/nestjs.ts:221–297  ·  view source on GitHub ↗

* Cross-file finalization for `RouterModule.register([...])`. The per-file * extract() above only sees `@Controller(prefix) + @Get(path)` — it can't * learn about the route prefix supplied by a sibling `app.module.ts` like: * * RouterModule.register([ * { path: 'admin', module:

(context: ResolutionContext)

Source from the content-addressed store, hash-verified

219 * the same input regardless of how many times it has already prefixed).
220 */
221 postExtract(context: ResolutionContext): Node[] {
222 const moduleToPrefix = new Map<string, string>();
223 const controllerToModule = new Map<string, string>();
224 const bootstraps: NestBootstrap[] = [];
225
226 for (const filePath of context.getAllFiles()) {
227 if (!/\.(m?[jt]s|cjs)$/.test(filePath)) continue;
228 const isModule = /\.module\.(m?[jt]s|cjs)$/.test(filePath);
229 if (!isModule && !(context.fileContains?.(filePath, 'NestFactory') ?? true)) continue;
230 const content = context.readFile(filePath);
231 if (!content) continue;
232 const safe = stripCommentsForRegex(content, detectLanguage(filePath));
233 if (isModule) {
234 collectRouterModuleRegistrations(safe, moduleToPrefix);
235 collectModuleControllers(safe, controllerToModule);
236 }
237 // Every app counts, even one that adds nothing: its routes must not
238 // borrow another app's prefix.
239 if (/\bNestFactory\s*\.\s*create/.test(safe)) bootstraps.push({ root: appRootOf(filePath), config: readBootstrap(safe) });
240 }
241
242 const controllerToPrefix = new Map<string, string>();
243 for (const [controller, module] of controllerToModule) {
244 const prefix = moduleToPrefix.get(module);
245 // `''` and `'/'` are no-op prefixes; skip them so we don't run updates
246 // that would set name to the value it already has.
247 if (prefix && prefix !== '' && prefix !== '/') {
248 controllerToPrefix.set(controller, prefix);
249 }
250 }
251
252 const adds = bootstraps.some((b) => b.config.prefix !== '' || b.config.uri);
253 if (controllerToPrefix.size === 0 && !adds) return [];
254
255 // Route → what the app adds to it. A RouterModule prefix binds through the
256 // controller class's line range; the bootstrap's prefix and versioning
257 // only to a route its source shows is in a `@Controller` — an Express
258 // route has the same qualified-name shape.
259 const plan = new Map<string, { route: Node; modulePrefix: string; config: NestAppConfig | null; version: string | null | undefined }>();
260 for (const [controllerName, prefix] of controllerToPrefix) {
261 for (const cls of context.getNodesByName(controllerName).filter((n) => n.kind === 'class')) {
262 for (const route of context.getNodesInFile(cls.filePath)) {
263 if (route.kind !== 'route' || route.startLine < cls.startLine || route.startLine > cls.endLine) continue;
264 plan.set(route.id, { route, modulePrefix: prefix, config: null, version: undefined });
265 }
266 }
267 }
268 if (adds) {
269 for (const filePath of context.getAllFiles()) {
270 if (!/\.(m?[jt]s|cjs)$/.test(filePath)) continue;
271 if (!(context.fileContains?.(filePath, '@Controller') ?? true)) continue;
272 const config = bootstrapFor(bootstraps, filePath);
273 if (!config || (config.prefix === '' && !config.uri)) continue;
274 const routes = context.getNodesInFile(filePath).filter((n) => n.kind === 'route' && HTTP_ROUTE_QN.test(n.qualifiedName));
275 if (routes.length === 0) continue;
276 const content = context.readFile(filePath);
277 if (!content) continue;
278 const safe = stripCommentsForRegex(content, detectLanguage(filePath));

Callers

nothing calls this directly

Calls 15

stripCommentsForRegexFunction · 0.90
collectModuleControllersFunction · 0.85
appRootOfFunction · 0.85
readBootstrapFunction · 0.85
bootstrapForFunction · 0.85
buildClassScopesFunction · 0.85
findDecoratorsFunction · 0.85
scopeForFunction · 0.85
routeVersionFunction · 0.85
applyAppConfigFunction · 0.85
pushMethod · 0.80

Tested by

no test coverage detected