The call node a site belongs to: climb from the callee to the call. A few levels cover a member chain.
(root: SyntaxNode, source: string, line: number, column: number | null, callee?: string)
| 503 | |
| 504 | /** The call node a site belongs to: climb from the callee to the call. A few levels cover a member chain. */ |
| 505 | function callAt(root: SyntaxNode, source: string, line: number, column: number | null, callee?: string): SyntaxNode | null { |
| 506 | const row = line - 1; |
| 507 | // The recorded column may sit a character off the callee (a 1-based |
| 508 | // column, the space before `prisma`): a near miss is tried before giving up. |
| 509 | const columns = column === null ? [firstNonBlankColumn(source, row)] : [column, column + 1, Math.max(0, column - 1), firstNonBlankColumn(source, row)]; |
| 510 | const want = callee ? callee.split(/[.:]/).pop() ?? callee : null; |
| 511 | for (const col of columns) { |
| 512 | const start = innermostAt(root, row, col); |
| 513 | if (!start) continue; |
| 514 | let node: SyntaxNode | null = start; |
| 515 | let first: SyntaxNode | null = null; |
| 516 | for (let up = 0; node && up < 10; up++, node = node.parent) { |
| 517 | if (!CALL_TYPES.has(node.type)) continue; |
| 518 | if (!first) first = node; |
| 519 | if (want === null) return node; |
| 520 | // A chain's position is its start: `res.status(201).json(…)` at `res` |
| 521 | // meets `res.status(…)` first; the call that is THIS one names `json`. |
| 522 | const container = argumentsOf(node); |
| 523 | const text = container ? calleeChainText(node, container) : ''; |
| 524 | if ((text.replace(/\([^()]*\)/g, '').split(/[.:]/).pop() ?? '') === want) return node; |
| 525 | } |
| 526 | if (first) return first; |
| 527 | } |
| 528 | return null; |
| 529 | } |
| 530 | |
| 531 | export function callSiteInTree(root: SyntaxNode, source: string, line: number, column: number | null, want?: string): CallSiteText | null { |
| 532 | const call = callAt(root, source, line, column, want); |
no test coverage detected