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

Function handlerRefs

src/resolution/frameworks/express.ts:518–604  ·  view source on GitHub ↗

* What a route call's handler contributes. The handler is the LAST argument — * the ones before it are middleware, and an arrow inside one of those * (`rateLimit({ keyGenerator: (req) => req.ip })`, `(req, res, next) => next()`) * says nothing about the handler. A handler that is a function — an

(
  safe: string,
  argsStart: number,
  args: string,
  fromNodeId: string,
  line: number,
  filePath: string,
  language: 'typescript' | 'javascript'
)

Source from the content-addressed store, hash-verified

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

Callers 1

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