(
index: BugPathIndex,
project: string,
target: BugPathNode,
depth: number,
maxChains: number,
)
| 264 | * one index produce the same document. |
| 265 | */ |
| 266 | export async function staticChains( |
| 267 | index: BugPathIndex, |
| 268 | project: string, |
| 269 | target: BugPathNode, |
| 270 | depth: number, |
| 271 | maxChains: number, |
| 272 | ): Promise<{ chains: BugPathNode[][]; truncated: boolean }> { |
| 273 | const complete: BugPathNode[][] = []; |
| 274 | let open: BugPathNode[][] = [[target]]; |
| 275 | let truncated = false; |
| 276 | |
| 277 | for (let level = 0; level < depth && open.length > 0; level++) { |
| 278 | const next: BugPathNode[][] = []; |
| 279 | for (const chain of open) { |
| 280 | const head = chain[0]; |
| 281 | if (head.qualifiedName === undefined) { |
| 282 | // Nothing to ask with. The chain ends and says nothing about |
| 283 | // whether anything calls it. |
| 284 | complete.push(chain); |
| 285 | continue; |
| 286 | } |
| 287 | const callers = await callersOf(index, project, head.qualifiedName); |
| 288 | if (callers.length === 0) { |
| 289 | complete.push([{ ...head, entryPoint: true }, ...chain.slice(1)]); |
| 290 | continue; |
| 291 | } |
| 292 | let extended = false; |
| 293 | for (const caller of callers) { |
| 294 | // A caller already on the chain is a cycle. It is not followed, |
| 295 | // and the chain it closes is kept as it stands: the call back |
| 296 | // into it is still a call the index records, and the divergence |
| 297 | // lists see it through the chain it is on. |
| 298 | if (chain.some((node) => nodeKey(node) === nodeKey(caller))) { |
| 299 | continue; |
| 300 | } |
| 301 | extended = true; |
| 302 | next.push([caller, ...chain]); |
| 303 | } |
| 304 | if (!extended) { |
| 305 | complete.push(chain); |
| 306 | } |
| 307 | } |
| 308 | const room = Math.max(0, maxChains - complete.length); |
| 309 | if (next.length > room) { |
| 310 | truncated = true; |
| 311 | } |
| 312 | open = next.slice(0, room); |
| 313 | if (level + 1 === depth && open.length > 0) { |
| 314 | truncated = true; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | const chains = [...complete, ...open].slice(0, maxChains); |
| 319 | if (complete.length + open.length > maxChains) { |
| 320 | truncated = true; |
| 321 | } |
| 322 | chains.sort((a, b) => compareText(a.map(nodeKey).join('>'), b.map(nodeKey).join('>'))); |
| 323 | return { chains, truncated }; |
no test coverage detected