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

Function resolveMiddleware

src/resolution/frameworks/express.ts:659–689  ·  view source on GitHub ↗
(
  name: string,
  context: ResolutionContext,
  fromFile?: string,
)

Source from the content-addressed store, hash-verified

657const MIDDLEWARE_KINDS: ReadonlySet<string> = new Set(['function', 'method', 'variable', 'constant', 'class']);
658
659function resolveMiddleware(
660 name: string,
661 context: ResolutionContext,
662 fromFile?: string,
663): string | null {
664 // Try exact name first — the calling file's own first. A middleware is a
665 // declaration (a function, a value, a class), never the file's import of a
666 // package (`import cors from 'cors'`) nor a heading or a module node.
667 const candidates = context.getNodesByName(name)
668 .filter((n) => MIDDLEWARE_KINDS.has(n.kind))
669 .sort((a, b) => Number(b.filePath === fromFile) - Number(a.filePath === fromFile));
670 const match = candidates.find((n) =>
671 n.name.toLowerCase() === name.toLowerCase() ||
672 n.name.toLowerCase() === name.replace(/Middleware$/i, '').toLowerCase()
673 );
674 if (match) return match.id;
675
676 // Try without Middleware suffix
677 const baseName = name.replace(/Middleware$/i, '');
678 if (baseName !== name) {
679 const baseCandidates = context.getNodesByName(baseName).filter((n) => MIDDLEWARE_KINDS.has(n.kind));
680 const MIDDLEWARE_DIRS = ['/middleware/', '/middlewares/'];
681 const preferred = baseCandidates.filter((n) =>
682 MIDDLEWARE_DIRS.some((d) => n.filePath.includes(d))
683 );
684 if (preferred.length > 0) return preferred[0]!.id;
685 if (baseCandidates.length > 0) return baseCandidates[0]!.id;
686 }
687
688 return null;
689}
690
691/**
692 * Resolve controller method using name-based lookup

Callers 1

resolveFunction · 0.85

Calls 3

NumberInterface · 0.85
hasMethod · 0.80
getNodesByNameMethod · 0.65

Tested by

no test coverage detected