| 430 | } |
| 431 | |
| 432 | export async function buildSteps(cg: CodeGraph, projectRoot: string, query: URLSearchParams): Promise<WireStepsPayload> { |
| 433 | const started = Date.now(); |
| 434 | const depthCap = intParam(query, 'depth', { min: 1, max: MAX_DEPTH, default: DEFAULT_DEPTH }); |
| 435 | const limit = intParam(query, 'limit', { min: 20, max: MAX_LIMIT, default: DEFAULT_LIMIT }); |
| 436 | const through = query.get('through') === '1'; |
| 437 | const stats = cg.getStats(); |
| 438 | const index = { lastIndexedAt: cg.getLastIndexedAt() ?? null, edges: stats.edgeCount, files: stats.fileCount }; |
| 439 | |
| 440 | const { anchor, ambiguous } = resolveAnchor(cg, query); |
| 441 | |
| 442 | // Route → where its code starts: the handler a resolver named, the page a |
| 443 | // screen file exports, or the route itself standing in for an inline |
| 444 | // handler (`route-roots.ts`) — and what kind of project this is, for the |
| 445 | // words the viewer uses. |
| 446 | const routes = cg.getNodesByKind('route'); |
| 447 | const roots = routeRoots(cg, routes); |
| 448 | const project = projectKind(routes, stats.edgesByKind?.navigates ?? 0); |
| 449 | |
| 450 | const reader = createSiteReader(cg, projectRoot, MAX_WHEN_SITES); |
| 451 | const calls = createSiteReader(cg, projectRoot, MAX_CALL_SITES); |
| 452 | /** The conditions a site runs under, structured — one read, joined where a string is wanted. */ |
| 453 | const guardsAt = (caller: Node, site: { line?: number; column?: number }) => reader.guards(caller, site); |
| 454 | /** The loops a site is written inside — a run of calls that happens once per item. */ |
| 455 | const loopsAt = (caller: Node, site: { line?: number; column?: number }) => reader.loops(caller, site); |
| 456 | const argsAt = (caller: Node, site: { line?: number; column?: number }) => reader.args(caller, site); |
| 457 | const withArgs = async (site: WireStepSite, caller: Node, at: { line?: number; column?: number }): Promise<WireStepSite> => { |
| 458 | const args = await argsAt(caller, at); |
| 459 | return args === null ? site : { ...site, args }; |
| 460 | }; |
| 461 | /** The call as written at a site, and what it passes — one read for both. */ |
| 462 | const callAt = (caller: Node, site: { line?: number; column?: number; callee?: string }) => calls.callSite(caller, site); |
| 463 | /** A hop's position with its call's span and enclosing call, read from the tree; the bare position when unreadable. */ |
| 464 | const hopAt = async (caller: Node, at: { line?: number; column?: number }, callee?: string): Promise<HopSite> => { |
| 465 | const line = at.line ?? caller.startLine; |
| 466 | const column = at.column ?? 0; |
| 467 | const read = at.line ? await callAt(caller, { ...at, ...(callee ? { callee } : {}) }) : null; |
| 468 | // The read must be THIS call. An inline handler's edges carry the ROUTE's |
| 469 | // line (`router.post('/users/login', async (req, res) => {`), where the |
| 470 | // only call is the registration itself — and taking its span would make |
| 471 | // every call in the handler read as written inside it, and so as running |
| 472 | // first. A miss is a bare position: no span to nest by, no `within`. |
| 473 | const last = (n: string) => n.replace(/\([^()]*\)/g, '').split(/[.:]/).pop() ?? n; |
| 474 | const usable = read !== null && (!callee || last(read.callee) === last(callee)); |
| 475 | if (!usable) return { file: caller.filePath, line, column, end: { line, column }, within: null }; |
| 476 | return { |
| 477 | file: caller.filePath, |
| 478 | line: read.span?.start.line ?? line, |
| 479 | column: read.span?.start.column ?? column, |
| 480 | end: read.span?.end ?? { line, column }, |
| 481 | within: read.within ?? null, |
| 482 | }; |
| 483 | }; |
| 484 | const pointHop = (caller: Node, at: { line?: number; column?: number }): HopSite => { |
| 485 | const line = at.line ?? caller.startLine; |
| 486 | const column = at.column ?? 0; |
| 487 | return { file: caller.filePath, line, column, end: { line, column }, within: null }; |
| 488 | }; |
| 489 | |