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

Function postExtract

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

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