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