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

Function postExtract

src/resolution/frameworks/express.ts:540–609  ·  view source on GitHub ↗

* Cross-file finalization for mounts. A router's routes are written * relative to where it is mounted — * * app.use('/api', routes) // app.js * router.use('/users', usersRouter) // routes/index.js * router.post('/', createUser) // routes/users.js → POST /api/u

(context: ResolutionContext)

Source from the content-addressed store, hash-verified

538 * every sync, exactly as the NestJS `RouterModule` pass is.
539 */
540 postExtract(context: ResolutionContext): Node[] {
541 const files = context.getAllFiles().filter((f) => /\.(m?js|tsx?|cjs)$/.test(f));
542 const mounts = new Map<string, Array<{ prefix: string; target: string }>>();
543 for (const file of files) {
544 // Most files mount nothing: look for `.use(` before decoding the file.
545 if (context.fileContains && !context.fileContains(file, '.use(')) continue;
546 const content = context.readFile(file);
547 if (!content || !content.includes('.use(')) continue;
548 const lang = detectLanguage(file);
549 const safe = stripCommentsForRegex(content, lang);
550 const mount = /\b[A-Za-z_$][\w$]*\.use\s*\(\s*(['"`])(\/[^'"`]*)\1\s*,/g;
551 let m: RegExpExecArray | null;
552 while ((m = mount.exec(safe)) !== null) {
553 const open = safe.indexOf('(', m.index);
554 const call = open >= 0 ? scanCall(safe, open) : null;
555 if (!call) continue;
556 // Split where the blanked structure says, but read the target from
557 // `safe`: a `require('./users')` target needs its string.
558 const parts = topLevelArgs(call.structure.slice(1, -1));
559 const lastPart = parts[parts.length - 1];
560 const last = lastPart ? safe.slice(open + 1 + lastPart.start, call.close).trim() : '';
561 const target = mountTarget(last, safe, file, lang, context);
562 if (!target || target === file) continue;
563 const list = mounts.get(file) ?? [];
564 list.push({ prefix: m[2]!, target });
565 mounts.set(file, list);
566 }
567 }
568 if (mounts.size === 0) return [];
569
570 // Compose prefixes down the mount tree until nothing changes; a file
571 // reached at two different paths is ambiguous and dropped.
572 let prefixOf = new Map<string, string>();
573 for (let round = 0; round < 8; round++) {
574 const next = new Map<string, string>();
575 const ambiguous = new Set<string>();
576 for (const [file, list] of mounts) {
577 const base = prefixOf.get(file) ?? '';
578 for (const { prefix, target } of list) {
579 const full = joinPaths(base, prefix);
580 const seen = next.get(target);
581 if (seen !== undefined && seen !== full) ambiguous.add(target);
582 else next.set(target, full);
583 }
584 }
585 for (const a of ambiguous) next.delete(a);
586 let changed = next.size !== prefixOf.size;
587 if (!changed) for (const [k, v] of next) if (prefixOf.get(k) !== v) changed = true;
588 prefixOf = next;
589 if (!changed) break;
590 }
591
592 const updates: Node[] = [];
593 for (const [file, prefix] of prefixOf) {
594 if (prefix === '' || prefix === '/') continue;
595 for (const route of context.getNodesInFile(file)) {
596 if (route.kind !== 'route') continue;
597 const sep = route.qualifiedName.indexOf('::');

Callers

nothing calls this directly

Calls 15

stripCommentsForRegexFunction · 0.90
scanCallFunction · 0.85
topLevelArgsFunction · 0.85
mountTargetFunction · 0.85
joinPathsFunction · 0.85
pushMethod · 0.80
detectLanguageFunction · 0.70
getAllFilesMethod · 0.65
fileContainsMethod · 0.65
readFileMethod · 0.65
execMethod · 0.65
getMethod · 0.65

Tested by

no test coverage detected