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

Function handlerRefs

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

531 * `argsStart` is where `args` begins in `safe`, for the reply-call offsets.
532 */
533export function handlerRefs(
534 safe: string,
535 argsStart: number,
536 args: string,
537 fromNodeId: string,
538 line: number,
539 filePath: string,
540 language: ScriptLanguage
541): UnresolvedRef[] {
542 // A trailing options object (`…, handler, { cache: true }`) is not the
543 // handler; a trailing array of handlers (`[validate, ctrl.create]`) is read
544 // as its last element.
545 const parts = topLevelArgs(args).filter((a) => a.text.trim());
546 while (parts.length > 1 && /^\s*\{/.test(parts[parts.length - 1]!.text)) parts.pop();
547 const lastPart = parts[parts.length - 1];
548 const arrayAt = lastPart ? lastPart.text.search(/\S/) : -1;
549 if (lastPart && lastPart.text[arrayAt] === '[') {
550 const close = matchDelim(lastPart.text, arrayAt, '[', ']');
551 const inner = close > arrayAt ? topLevelArgs(lastPart.text.slice(arrayAt + 1, close)).filter((a) => a.text.trim()).pop() : undefined;
552 if (inner) parts[parts.length - 1] = { text: inner.text, start: lastPart.start + arrayAt + 1 + inner.start };
553 else parts.pop();
554 }
555 const handler = parts[parts.length - 1];
556 if (!handler) return [];
557 const text = handler.text;
558 // A `function` is the handler's own only when it opens the argument —
559 // directly, or inside wrapper calls (`asyncHandler(function …)`); then its
560 // body starts after its parameter list, not after an arrow inside it
561 // (`items.map((i) => …)`). A `function` anywhere else (a default parameter,
562 // a wrapper's options object) is not the handler.
563 let arrowAt = text.indexOf('=>');
564 const opening = /^\s*(?:[A-Za-z_$][\w$.]*\s*\(\s*)*(?:async\s+)?function\b[^(]*\(/.exec(text);
565 let fnAt = opening ? opening[0].lastIndexOf('function') : -1;
566 if (fnAt >= 0) {
567 // Inside a wrapper, a `function` followed by an arrow after its body is a
568 // callback argument (`withErrors(function onErr(e) {…}, async (req, res) => …)`):
569 // the arrow is the handler.
570 const fnBodyEnd = functionBodyEnd(text, fnAt);
571 const insideWrapper = opening![0].slice(0, fnAt).includes('(');
572 const laterArrow = fnBodyEnd >= 0 && insideWrapper ? text.indexOf('=>', fnBodyEnd) : -1;
573 if (laterArrow >= 0) {
574 arrowAt = laterArrow;
575 fnAt = -1;
576 } else if (insideWrapper && fnBodyEnd >= 0 && !/^[\s)]*$/.test(text.slice(fnBodyEnd + 1))) {
577 // A callback followed by more arguments (`withErrors(function onErr(e)
578 // {…}, ctrl.list)`) is not the handler.
579 arrowAt = -1;
580 fnAt = -1;
581 } else {
582 arrowAt = -1;
583 }
584 }
585
586 if (arrowAt < 0 && fnAt < 0) {
587 const handlerName = extractTailIdent(text.trim()) ?? extractTailIdent(text.slice(text.lastIndexOf(',') + 1).trim());
588 return handlerName
589 ? [{ fromNodeId, referenceName: handlerName, referenceKind: 'references', line, column: 0, filePath, language }]
590 : [];

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