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

Function postExtract

src/resolution/frameworks/express.ts:303–372  ·  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

301 * every sync, exactly as the NestJS `RouterModule` pass is.
302 */
303 postExtract(context: ResolutionContext): Node[] {
304 const files = context.getAllFiles().filter((f) => /\.(m?js|tsx?|cjs)$/.test(f));
305 const mounts = new Map<string, Array<{ prefix: string; target: string }>>();
306 for (const file of files) {
307 // Most files mount nothing: look for `.use(` before decoding the file.
308 if (context.fileContains && !context.fileContains(file, '.use(')) continue;
309 const content = context.readFile(file);
310 if (!content || !content.includes('.use(')) continue;
311 const lang = detectLanguage(file);
312 const safe = stripCommentsForRegex(content, lang);
313 const mount = /\b[A-Za-z_$][\w$]*\.use\s*\(\s*(['"`])(\/[^'"`]*)\1\s*,/g;
314 let m: RegExpExecArray | null;
315 while ((m = mount.exec(safe)) !== null) {
316 const open = safe.indexOf('(', m.index);
317 const call = open >= 0 ? scanCall(safe, open) : null;
318 if (!call) continue;
319 // Split where the blanked structure says, but read the target from
320 // `safe`: a `require('./users')` target needs its string.
321 const parts = topLevelArgs(call.structure.slice(1, -1));
322 const lastPart = parts[parts.length - 1];
323 const last = lastPart ? safe.slice(open + 1 + lastPart.start, call.close).trim() : '';
324 const target = mountTarget(last, safe, file, lang, context);
325 if (!target || target === file) continue;
326 const list = mounts.get(file) ?? [];
327 list.push({ prefix: m[2]!, target });
328 mounts.set(file, list);
329 }
330 }
331 if (mounts.size === 0) return [];
332
333 // Compose prefixes down the mount tree until nothing changes; a file
334 // reached at two different paths is ambiguous and dropped.
335 let prefixOf = new Map<string, string>();
336 for (let round = 0; round < 8; round++) {
337 const next = new Map<string, string>();
338 const ambiguous = new Set<string>();
339 for (const [file, list] of mounts) {
340 const base = prefixOf.get(file) ?? '';
341 for (const { prefix, target } of list) {
342 const full = joinPaths(base, prefix);
343 const seen = next.get(target);
344 if (seen !== undefined && seen !== full) ambiguous.add(target);
345 else next.set(target, full);
346 }
347 }
348 for (const a of ambiguous) next.delete(a);
349 let changed = next.size !== prefixOf.size;
350 if (!changed) for (const [k, v] of next) if (prefixOf.get(k) !== v) changed = true;
351 prefixOf = next;
352 if (!changed) break;
353 }
354
355 const updates: Node[] = [];
356 for (const [file, prefix] of prefixOf) {
357 if (prefix === '' || prefix === '/') continue;
358 for (const route of context.getNodesInFile(file)) {
359 if (route.kind !== 'route') continue;
360 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