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

Function handlerRefs

src/resolution/frameworks/express.ts:769–855  ·  view source on GitHub ↗
(
  safe: string,
  argsStart: number,
  args: string,
  fromNodeId: string,
  line: number,
  filePath: string,
  language: ScriptLanguage
)

Source from the content-addressed store, hash-verified

767 * `argsStart` is where `args` begins in `safe`, for the reply-call offsets.
768 */
769export function handlerRefs(
770 safe: string,
771 argsStart: number,
772 args: string,
773 fromNodeId: string,
774 line: number,
775 filePath: string,
776 language: ScriptLanguage
777): UnresolvedRef[] {
778 // A trailing options object (`…, handler, { cache: true }`) is not the
779 // handler; a trailing array of handlers (`[validate, ctrl.create]`) is read
780 // as its last element.
781 const parts = topLevelArgs(args).filter((a) => a.text.trim());
782 while (parts.length > 1 && /^\s*\{/.test(parts[parts.length - 1]!.text)) parts.pop();
783 const lastPart = parts[parts.length - 1];
784 const arrayAt = lastPart ? lastPart.text.search(/\S/) : -1;
785 if (lastPart && lastPart.text[arrayAt] === '[') {
786 const close = matchDelim(lastPart.text, arrayAt, '[', ']');
787 const inner = close > arrayAt ? topLevelArgs(lastPart.text.slice(arrayAt + 1, close)).filter((a) => a.text.trim()).pop() : undefined;
788 if (inner) parts[parts.length - 1] = { text: inner.text, start: lastPart.start + arrayAt + 1 + inner.start };
789 else parts.pop();
790 }
791 const handler = parts[parts.length - 1];
792 if (!handler) return [];
793 const text = handler.text;
794 // A `function` is the handler's own only when it opens the argument —
795 // directly, or inside wrapper calls (`asyncHandler(function …)`); then its
796 // body starts after its parameter list, not after an arrow inside it
797 // (`items.map((i) => …)`). A `function` anywhere else (a default parameter,
798 // a wrapper's options object) is not the handler.
799 let arrowAt = text.indexOf('=>');
800 const opening = /^\s*(?:[A-Za-z_$][\w$.]*\s*\(\s*)*(?:async\s+)?function\b[^(]*\(/.exec(text);
801 let fnAt = opening ? opening[0].lastIndexOf('function') : -1;
802 if (fnAt >= 0) {
803 // Inside a wrapper, a `function` followed by an arrow after its body is a
804 // callback argument (`withErrors(function onErr(e) {…}, async (req, res) => …)`):
805 // the arrow is the handler.
806 const fnBodyEnd = functionBodyEnd(text, fnAt);
807 const insideWrapper = opening![0].slice(0, fnAt).includes('(');
808 const laterArrow = fnBodyEnd >= 0 && insideWrapper ? text.indexOf('=>', fnBodyEnd) : -1;
809 if (laterArrow >= 0) {
810 arrowAt = laterArrow;
811 fnAt = -1;
812 } else if (insideWrapper && fnBodyEnd >= 0 && !/^[\s)]*$/.test(text.slice(fnBodyEnd + 1))) {
813 // A callback followed by more arguments (`withErrors(function onErr(e)
814 // {…}, ctrl.list)`) is not the handler.
815 arrowAt = -1;
816 fnAt = -1;
817 } else {
818 arrowAt = -1;
819 }
820 }
821
822 if (arrowAt < 0 && fnAt < 0) {
823 const handlerName = extractTailIdent(text.trim()) ?? extractTailIdent(text.slice(text.lastIndexOf(',') + 1).trim());
824 return handlerName
825 ? [{ fromNodeId, referenceName: handlerName, referenceKind: 'references', line, column: 0, filePath, language }]
826 : [];

Callers 2

routeRefsFunction · 0.90
extractFunction · 0.85

Calls 10

topLevelArgsFunction · 0.85
matchDelimFunction · 0.85
functionBodyEndFunction · 0.85
extractTailIdentFunction · 0.85
handlerCallNamesFunction · 0.85
replyRefsFunction · 0.85
searchMethod · 0.80
mapMethod · 0.80
pushMethod · 0.80
execMethod · 0.65

Tested by

no test coverage detected