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

Function resolveMiddleware

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

Source from the content-addressed store, hash-verified

671const MIDDLEWARE_KINDS: ReadonlySet<string> = new Set(['function', 'method', 'variable', 'constant', 'class']);
672
673function resolveMiddleware(
674 name: string,
675 context: ResolutionContext,
676 fromFile?: string,
677): string | null {
678 // Try exact name first — the calling file's own first. A middleware is a
679 // declaration (a function, a value, a class), never the file's import of a
680 // package (`import cors from 'cors'`) nor a heading or a module node.
681 const candidates = context.getNodesByName(name)
682 .filter((n) => MIDDLEWARE_KINDS.has(n.kind))
683 .sort((a, b) => Number(b.filePath === fromFile) - Number(a.filePath === fromFile));
684 const match = candidates.find((n) =>
685 n.name.toLowerCase() === name.toLowerCase() ||
686 n.name.toLowerCase() === name.replace(/Middleware$/i, '').toLowerCase()
687 );
688 if (match) return match.id;
689
690 // Try without Middleware suffix
691 const baseName = name.replace(/Middleware$/i, '');
692 if (baseName !== name) {
693 const baseCandidates = context.getNodesByName(baseName).filter((n) => MIDDLEWARE_KINDS.has(n.kind));
694 const MIDDLEWARE_DIRS = ['/middleware/', '/middlewares/'];
695 const preferred = baseCandidates.filter((n) =>
696 MIDDLEWARE_DIRS.some((d) => n.filePath.includes(d))
697 );
698 if (preferred.length > 0) return preferred[0]!.id;
699 if (baseCandidates.length > 0) return baseCandidates[0]!.id;
700 }
701
702 return null;
703}
704
705/**
706 * 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