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

Function postExtract

src/resolution/frameworks/nestjs.ts:217–266  ·  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

215 * the same input regardless of how many times it has already prefixed).
216 */
217 postExtract(context: ResolutionContext): Node[] {
218 const moduleToPrefix = new Map<string, string>();
219 const controllerToModule = new Map<string, string>();
220
221 for (const filePath of context.getAllFiles()) {
222 if (!/\.module\.(m?[jt]s|cjs)$/.test(filePath)) continue;
223 const content = context.readFile(filePath);
224 if (!content) continue;
225 const safe = stripCommentsForRegex(content, detectLanguage(filePath));
226 collectRouterModuleRegistrations(safe, moduleToPrefix);
227 collectModuleControllers(safe, controllerToModule);
228 }
229
230 const controllerToPrefix = new Map<string, string>();
231 for (const [controller, module] of controllerToModule) {
232 const prefix = moduleToPrefix.get(module);
233 // `''` and `'/'` are no-op prefixes; skip them so we don't run updates
234 // that would set name to the value it already has.
235 if (prefix && prefix !== '' && prefix !== '/') {
236 controllerToPrefix.set(controller, prefix);
237 }
238 }
239
240 if (controllerToPrefix.size === 0) return [];
241
242 const updates: Node[] = [];
243 for (const [controllerName, prefix] of controllerToPrefix) {
244 const classes = context
245 .getNodesByName(controllerName)
246 .filter((n) => n.kind === 'class');
247 for (const cls of classes) {
248 const routes = context
249 .getNodesInFile(cls.filePath)
250 .filter((n) => n.kind === 'route');
251 for (const route of routes) {
252 // Multiple controllers can live in one file (covered by the
253 // existing "attributes methods to the right controller" test);
254 // each route must be associated with the controller whose line
255 // range contains it.
256 if (route.startLine < cls.startLine || route.startLine > cls.endLine) {
257 continue;
258 }
259 const updated = applyModulePrefix(route, prefix);
260 if (updated && updated.name !== route.name) updates.push(updated);
261 }
262 }
263 }
264
265 return updates;
266 },
267};
268
269// ---------------------------------------------------------------------------

Callers

nothing calls this directly

Calls 11

stripCommentsForRegexFunction · 0.90
collectModuleControllersFunction · 0.85
applyModulePrefixFunction · 0.85
readFileMethod · 0.80
detectLanguageFunction · 0.70
getAllFilesMethod · 0.65
getMethod · 0.65
getNodesByNameMethod · 0.65
getNodesInFileMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected