Finds all cycles in the specified source file.
(
sf: ts.SourceFile,
visited = new WeakSet<ts.SourceFile>(),
path: ReferenceChain = [],
)
| 48 | |
| 49 | /** Finds all cycles in the specified source file. */ |
| 50 | findCycles( |
| 51 | sf: ts.SourceFile, |
| 52 | visited = new WeakSet<ts.SourceFile>(), |
| 53 | path: ReferenceChain = [], |
| 54 | ): ReferenceChain[] { |
| 55 | const previousIndex = path.indexOf(sf); |
| 56 | // If the given node is already part of the current path, then a cycle has |
| 57 | // been found. Add the reference chain which represents the cycle to the results. |
| 58 | if (previousIndex !== -1) { |
| 59 | return [path.slice(previousIndex)]; |
| 60 | } |
| 61 | // If the node has already been visited, then it's not necessary to go check its edges |
| 62 | // again. Cycles would have been already detected and collected in the first check. |
| 63 | if (visited.has(sf)) { |
| 64 | return []; |
| 65 | } |
| 66 | path.push(sf); |
| 67 | visited.add(sf); |
| 68 | // Go through all edges, which are determined through import/exports, and collect cycles. |
| 69 | const result: ReferenceChain[] = []; |
| 70 | for (const ref of getModuleReferences(sf, this._ignoreTypeOnlyChecks)) { |
| 71 | const targetFile = this._resolveImport(ref, sf.fileName); |
| 72 | if (targetFile !== null) { |
| 73 | result.push(...this.findCycles(this.getSourceFile(targetFile), visited, path.slice())); |
| 74 | } |
| 75 | } |
| 76 | return result; |
| 77 | } |
| 78 | |
| 79 | /** Gets the TypeScript source file of the specified path. */ |
| 80 | getSourceFile(filePath: string): ts.SourceFile { |
no test coverage detected