MCPcopy Create free account
hub / github.com/angular/dev-infra / findCycles

Method findCycles

ng-dev/ts-circular-dependencies/analyzer.ts:50–77  ·  view source on GitHub ↗

Finds all cycles in the specified source file.

(
    sf: ts.SourceFile,
    visited = new WeakSet<ts.SourceFile>(),
    path: ReferenceChain = [],
  )

Source from the content-addressed store, hash-verified

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 {

Callers 1

mainFunction · 0.95

Calls 7

_resolveImportMethod · 0.95
getSourceFileMethod · 0.95
getModuleReferencesFunction · 0.85
sliceMethod · 0.45
hasMethod · 0.45
pushMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected