Angular Modernizer
    Preparing search index...

    Dependency graph builder

    Builds a directed graph of file dependencies by parsing import statements and export ... from re-exports, and computes transitive relationships with the Floyd-Warshall algorithm. DependencyGraphBuilder.getImpact offers a name-aware, type-only-aware impact computation.

    Template edges (a host component using a component, directive or pipe by selector or pipe name, see collectTemplateEdges in api) are opt-in and kept apart from the import edges: they never appear in dependencies / dependents, cycle detection or transitive dependents.

    const builder = new DependencyGraphBuilder(project);
    await builder.buildGraph();
    const dependents = builder.getTransitiveDependents('src/core/service.ts');
    console.info(`${dependents.length} files depend on this service`);
    Index

    Constructors

    Methods

    • Build dependency graph for all source files

      Parses import declarations and export ... from re-exports. Module specifiers are resolved through the TypeScript module resolver (tsconfig paths aware) with a relative / index probing fallback.

      Returns Promise<void>

    • Clear the dependency graph

      Returns void

    • Compute transitive dependencies using Floyd-Warshall algorithm

      This computes the transitive closure of the dependency graph, allowing efficient queries for all transitive dependents.

      Time complexity: O(n³) where n is number of files Space complexity: O(n²)

      Note: Only needs to be computed once after building graph.

      Returns void

    • Detect circular dependencies

      Returns sets of files that form circular dependency chains.

      Returns string[][]

      Array of circular dependency chains

    • Find the files consuming specific exported symbols of a file (TypeScript findReferences).

      Parameters

      • filePath: string
      • symbols: string[]

      Returns SymbolConsumers[]

    • Get direct dependencies of a file

      Returns files that this file directly imports.

      Parameters

      • filePath: string

      Returns string[]

    • Get direct dependents of a file

      Returns files that directly import this file.

      Parameters

      • filePath: string

      Returns string[]

    • Compute the build impact of changing the given files.

      Unlike getTransitiveDependents, propagation is name-aware:

      • importers are only affected if they import a name the changed file (or a barrel in between) actually provides, so a barrel re-export does not make every importer of the barrel depend on every file behind it;
      • importers whose affected bindings are all type-only (import type, import { type X }, or names that resolve to interfaces / type aliases) are reported as typeOnlyDependents (type-check only, no JS rebuild); files reached only through type-only edges stay type-only;
      • an importer propagates further only with the exports whose type surface names an affected binding (heritage, annotations, inferred types, @NgModule exports; not bodies with a stated return type or decorators), see export-surface. Side-effect imports propagate with all exports.

      Requires buildGraph to have run.

      Parameters

      • changedFiles: string[]

        Absolute file paths that change

      • options: { symbols?: string[] } = {}
        • Optionalsymbols?: string[]

          Restrict the seed to these exported symbols of the changed files (symbol-level impact)

      Returns DependencyImpact

    • Files whose classes the templates of this file use without importing them. Empty unless template edges are enabled.

      Parameters

      • filePath: string

      Returns string[]

    • Get all transitive dependents of a file

      Returns all files that directly or indirectly depend on the given file. Automatically computes transitive dependencies if not already computed.

      Parameters

      • filePath: string

        The file to find dependents for

      Returns string[]

      Array of file paths that depend on this file

      // If A imports B, and B imports C, then C's transitive dependents are [B, A]
      const dependents = builder.getTransitiveDependents('src/c.ts');
      // Returns: ['src/b.ts', 'src/a.ts']
    • Check if there's a dependency path from source to target

      Parameters

      • source: string

        Source file path

      • target: string

        Target file path

      Returns boolean

      True if source depends on target (directly or transitively)