Home / Class/ DependencyCollectionContext Class — react Architecture

DependencyCollectionContext Class — react Architecture

Architecture documentation for the DependencyCollectionContext class in PropagateScopeDependenciesHIR.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  1be3b542_2ecf_364c_adef_6e5675b7385e["DependencyCollectionContext"]
  76832af2_c0a7_f31c_e448_af5664da4b88["PropagateScopeDependenciesHIR.ts"]
  1be3b542_2ecf_364c_adef_6e5675b7385e -->|defined in| 76832af2_c0a7_f31c_e448_af5664da4b88
  f9e0d3ee_99a8_8620_73ee_4fbda050d54c["constructor()"]
  1be3b542_2ecf_364c_adef_6e5675b7385e -->|method| f9e0d3ee_99a8_8620_73ee_4fbda050d54c
  ad463e10_0394_9f8b_1e38_c1e95a00ff97["enterScope()"]
  1be3b542_2ecf_364c_adef_6e5675b7385e -->|method| ad463e10_0394_9f8b_1e38_c1e95a00ff97
  fa34629f_c89e_a9bf_4c20_ec6a2b54a55c["exitScope()"]
  1be3b542_2ecf_364c_adef_6e5675b7385e -->|method| fa34629f_c89e_a9bf_4c20_ec6a2b54a55c
  ded1f8c0_1fe0_2f60_1757_ce46724d6994["isUsedOutsideDeclaringScope()"]
  1be3b542_2ecf_364c_adef_6e5675b7385e -->|method| ded1f8c0_1fe0_2f60_1757_ce46724d6994
  97736ed1_9768_bb5a_276e_d5b04a417908["declare()"]
  1be3b542_2ecf_364c_adef_6e5675b7385e -->|method| 97736ed1_9768_bb5a_276e_d5b04a417908
  a9451a62_0379_c394_befb_99acd7445bd9["hasDeclared()"]
  1be3b542_2ecf_364c_adef_6e5675b7385e -->|method| a9451a62_0379_c394_befb_99acd7445bd9
  e434c5d6_d7c8_9e0b_09fc_dc6523d5ef77["maybeDependency()"]
  1be3b542_2ecf_364c_adef_6e5675b7385e -->|method| e434c5d6_d7c8_9e0b_09fc_dc6523d5ef77
  3a794155_ff06_85f0_8e9a_496a5d3f6133["scope()"]
  1be3b542_2ecf_364c_adef_6e5675b7385e -->|method| 3a794155_ff06_85f0_8e9a_496a5d3f6133
  2f0c247d_1518_0cc0_2e7e_dbaee77d4552["currentScope()"]
  1be3b542_2ecf_364c_adef_6e5675b7385e -->|method| 2f0c247d_1518_0cc0_2e7e_dbaee77d4552
  b9659720_c932_ebd9_335f_682c49188a34["visitOperand()"]
  1be3b542_2ecf_364c_adef_6e5675b7385e -->|method| b9659720_c932_ebd9_335f_682c49188a34
  8d8b17be_69f6_c901_0c65_675b2f52ffc0["visitProperty()"]
  1be3b542_2ecf_364c_adef_6e5675b7385e -->|method| 8d8b17be_69f6_c901_0c65_675b2f52ffc0
  b1bdda46_c31a_9557_b610_d2ed7c3ceaed["visitDependency()"]
  1be3b542_2ecf_364c_adef_6e5675b7385e -->|method| b1bdda46_c31a_9557_b610_d2ed7c3ceaed
  0b9af218_0f18_832f_3f27_3909263220a0["visitReassignment()"]
  1be3b542_2ecf_364c_adef_6e5675b7385e -->|method| 0b9af218_0f18_832f_3f27_3909263220a0

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/HIR/PropagateScopeDependenciesHIR.ts lines 391–661

export class DependencyCollectionContext {
  #declarations: Map<DeclarationId, Decl> = new Map();
  #reassignments: Map<Identifier, Decl> = new Map();

  #scopes: Stack<ReactiveScope> = empty();
  // Reactive dependencies used in the current reactive scope.
  #dependencies: Stack<Array<ReactiveScopeDependency>> = empty();
  deps: Map<ReactiveScope, Array<ReactiveScopeDependency>> = new Map();

  #temporaries: ReadonlyMap<IdentifierId, ReactiveScopeDependency>;
  #temporariesUsedOutsideScope: ReadonlySet<DeclarationId>;
  #processedInstrsInOptional: ReadonlySet<Instruction | Terminal>;

  /**
   * Tracks the traversal state. See Context.declare for explanation of why this
   * is needed.
   */
  #innerFnContext: {outerInstrId: InstructionId} | null = null;

  constructor(
    temporariesUsedOutsideScope: ReadonlySet<DeclarationId>,
    temporaries: ReadonlyMap<IdentifierId, ReactiveScopeDependency>,
    processedInstrsInOptional: ReadonlySet<Instruction | Terminal>,
  ) {
    this.#temporariesUsedOutsideScope = temporariesUsedOutsideScope;
    this.#temporaries = temporaries;
    this.#processedInstrsInOptional = processedInstrsInOptional;
  }

  enterScope(scope: ReactiveScope): void {
    // Set context for new scope
    this.#dependencies = this.#dependencies.push([]);
    this.#scopes = this.#scopes.push(scope);
  }

  exitScope(scope: ReactiveScope, pruned: boolean): void {
    // Save dependencies we collected from the exiting scope
    const scopedDependencies = this.#dependencies.value;
    CompilerError.invariant(scopedDependencies != null, {
      reason: '[PropagateScopeDeps]: Unexpected scope mismatch',
      loc: scope.loc,
    });

    // Restore context of previous scope
    this.#scopes = this.#scopes.pop();
    this.#dependencies = this.#dependencies.pop();

    /*
     * Collect dependencies we recorded for the exiting scope and propagate
     * them upward using the same rules as normal dependency collection.
     * Child scopes may have dependencies on values created within the outer
     * scope, which necessarily cannot be dependencies of the outer scope.
     */
    for (const dep of scopedDependencies) {
      if (this.#checkValidDependency(dep)) {
        this.#dependencies.value?.push(dep);
      }
    }

    if (!pruned) {
      this.deps.set(scope, scopedDependencies);
    }
  }

  isUsedOutsideDeclaringScope(place: Place): boolean {
    return this.#temporariesUsedOutsideScope.has(
      place.identifier.declarationId,
    );
  }

  /*
   * Records where a value was declared, and optionally, the scope where the
   * value originated from. This is later used to determine if a dependency
   * should be added to a scope; if the current scope we are visiting is the
   * same scope where the value originates, it can't be a dependency on itself.
   *
   * Note that we do not track declarations or reassignments within inner
   * functions for the following reasons:
   *   - inner functions cannot be split by scope boundaries and are guaranteed
   *     to consume their own declarations
   *   - reassignments within inner functions are tracked as context variables,

Frequently Asked Questions

What is the DependencyCollectionContext class?
DependencyCollectionContext is a class in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/HIR/PropagateScopeDependenciesHIR.ts.
Where is DependencyCollectionContext defined?
DependencyCollectionContext is defined in compiler/packages/babel-plugin-react-compiler/src/HIR/PropagateScopeDependenciesHIR.ts at line 391.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free