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

Function postExtract

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

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

Tested by

no test coverage detected