* The anchor: `anchor= `, or `symbol= ` resolved to the most * screen-like symbol of that name — a route first, then a component or * function, then a method — with the rest reported as `ambiguous`.
(cg: CodeGraph, query: URLSearchParams)
| 1597 | * function, then a method — with the rest reported as `ambiguous`. |
| 1598 | */ |
| 1599 | function resolveAnchor(cg: CodeGraph, query: URLSearchParams): { anchor: Node; ambiguous: WireNodeRef[] } { |
| 1600 | const id = query.get('anchor'); |
| 1601 | if (id !== null && id.trim() !== '') { |
| 1602 | const node = cg.getNode(id); |
| 1603 | if (!node) throw notFound(`No symbol with id "${id}" in this index.`, 'It may have moved in a re-index; open it from search or the Screens view.'); |
| 1604 | return { anchor: node, ambiguous: [] }; |
| 1605 | } |
| 1606 | const name = query.get('symbol'); |
| 1607 | if (name === null || name.trim() === '') throw badRequest('Give the picture an anchor: ?anchor=<node id> or ?symbol=<name>.'); |
| 1608 | const rank: Record<string, number> = { route: 0, component: 1, function: 2, method: 3, class: 4, constant: 5, variable: 6 }; |
| 1609 | const matches = cg |
| 1610 | .getNodesByName(name.trim()) |
| 1611 | .filter((n) => n.kind !== 'file' && n.kind !== 'import' && n.kind !== 'export') |
| 1612 | .sort((a, b) => (rank[a.kind] ?? 9) - (rank[b.kind] ?? 9) || a.filePath.localeCompare(b.filePath) || a.startLine - b.startLine); |
| 1613 | const anchor = matches[0]; |
| 1614 | if (!anchor) throw notFound(`Nothing in this index is named "${name}".`, 'Try the search box; names are matched exactly.'); |
| 1615 | return { anchor, ambiguous: matches.slice(1, 9).map(toNodeRef) }; |
| 1616 | } |
| 1617 | |
| 1618 | /** How many distinct parents render this node as a JSX child. Memoised per request. */ |
| 1619 | function renderParents(cg: CodeGraph, node: Node, memo: Map<string, number>): number { |
no test coverage detected