* Find an import path from the `start` SourceFile to the `end` SourceFile. * * This function implements a breadth first search that results in finding the * shortest path between the `start` and `end` points. * * @param start the starting point of the path. * @param end the ending
(start: ts.SourceFile, end: ts.SourceFile)
| 48 | * no path could be found. |
| 49 | */ |
| 50 | findPath(start: ts.SourceFile, end: ts.SourceFile): ts.SourceFile[] | null { |
| 51 | if (start === end) { |
| 52 | // Escape early for the case where `start` and `end` are the same. |
| 53 | return [start]; |
| 54 | } |
| 55 | |
| 56 | const found = new Set<ts.SourceFile>([start]); |
| 57 | const queue: Found[] = [new Found(start, null)]; |
| 58 | |
| 59 | while (queue.length > 0) { |
| 60 | const current = queue.shift()!; |
| 61 | const imports = this.importsOf(current.sourceFile); |
| 62 | for (const importedFile of imports) { |
| 63 | if (!found.has(importedFile)) { |
| 64 | const next = new Found(importedFile, current); |
| 65 | if (next.sourceFile === end) { |
| 66 | // We have hit the target `end` path so we can stop here. |
| 67 | return next.toPath(); |
| 68 | } |
| 69 | found.add(importedFile); |
| 70 | queue.push(next); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | return null; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Add a record of an import from `sf` to `imported`, that's not present in the original |