Home / Class/ ReactiveScopeDependencyTreeHIR Class — react Architecture

ReactiveScopeDependencyTreeHIR Class — react Architecture

Architecture documentation for the ReactiveScopeDependencyTreeHIR class in DeriveMinimalDependenciesHIR.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  ce8ab5d9_db4b_3be5_3f9c_38fa68bcbaa1["ReactiveScopeDependencyTreeHIR"]
  686094a4_f90a_6b2e_949a_aa03f03b5c66["DeriveMinimalDependenciesHIR.ts"]
  ce8ab5d9_db4b_3be5_3f9c_38fa68bcbaa1 -->|defined in| 686094a4_f90a_6b2e_949a_aa03f03b5c66
  c3c6f6a8_c9a1_6bd6_1e19_d0b4daf01b4a["constructor()"]
  ce8ab5d9_db4b_3be5_3f9c_38fa68bcbaa1 -->|method| c3c6f6a8_c9a1_6bd6_1e19_d0b4daf01b4a
  66e2b974_b614_49df_112b_4ddbd5350177["identifier()"]
  ce8ab5d9_db4b_3be5_3f9c_38fa68bcbaa1 -->|method| 66e2b974_b614_49df_112b_4ddbd5350177
  61f036d0_eb91_7e31_154e_acb3c7c8f23c["addDependency()"]
  ce8ab5d9_db4b_3be5_3f9c_38fa68bcbaa1 -->|method| 61f036d0_eb91_7e31_154e_acb3c7c8f23c
  3007052f_a4b8_b5ea_a419_9f2abc9f4286["deriveMinimalDependencies()"]
  ce8ab5d9_db4b_3be5_3f9c_38fa68bcbaa1 -->|method| 3007052f_a4b8_b5ea_a419_9f2abc9f4286
  a0909a79_f73c_c240_9eb5_d14fc1117570["printDeps()"]
  ce8ab5d9_db4b_3be5_3f9c_38fa68bcbaa1 -->|method| a0909a79_f73c_c240_9eb5_d14fc1117570
  be6dc962_71f6_4266_c409_6238243a005b["debug()"]
  ce8ab5d9_db4b_3be5_3f9c_38fa68bcbaa1 -->|method| be6dc962_71f6_4266_c409_6238243a005b
  a3891b19_1fd5_53e2_4ade_779a02605bc9["buf()"]
  ce8ab5d9_db4b_3be5_3f9c_38fa68bcbaa1 -->|method| a3891b19_1fd5_53e2_4ade_779a02605bc9

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/HIR/DeriveMinimalDependenciesHIR.ts lines 22–235

export class ReactiveScopeDependencyTreeHIR {
  /**
   * Paths from which we can hoist PropertyLoads. If an `identifier`,
   * `identifier.path`, or `identifier?.path` is in this map, it is safe to
   * evaluate (non-optional) PropertyLoads from.
   */
  #hoistableObjects: Map<Identifier, HoistableNode & {reactive: boolean}> =
    new Map();
  #deps: Map<Identifier, DependencyNode & {reactive: boolean}> = new Map();

  /**
   * @param hoistableObjects a set of paths from which we can safely evaluate
   * PropertyLoads. Note that we expect these to not contain duplicates (e.g.
   * both `a?.b` and `a.b`) only because CollectHoistablePropertyLoads merges
   * duplicates when traversing the CFG.
   */
  constructor(hoistableObjects: Iterable<ReactiveScopeDependency>) {
    for (const {path, identifier, reactive} of hoistableObjects) {
      let currNode = ReactiveScopeDependencyTreeHIR.#getOrCreateRoot(
        identifier,
        reactive,
        this.#hoistableObjects,
        path.length > 0 && path[0].optional ? 'Optional' : 'NonNull',
      );

      for (let i = 0; i < path.length; i++) {
        const prevAccessType = currNode.properties.get(
          path[i].property,
        )?.accessType;
        const accessType =
          i + 1 < path.length && path[i + 1].optional ? 'Optional' : 'NonNull';
        CompilerError.invariant(
          prevAccessType == null || prevAccessType === accessType,
          {
            reason: 'Conflicting access types',
            loc: GeneratedSource,
          },
        );
        let nextNode = currNode.properties.get(path[i].property);
        if (nextNode == null) {
          nextNode = {
            properties: new Map(),
            accessType,
          };
          currNode.properties.set(path[i].property, nextNode);
        }
        currNode = nextNode;
      }
    }
  }

  static #getOrCreateRoot<T extends string>(
    identifier: Identifier,
    reactive: boolean,
    roots: Map<Identifier, TreeNode<T> & {reactive: boolean}>,
    defaultAccessType: T,
  ): TreeNode<T> {
    // roots can always be accessed unconditionally in JS
    let rootNode = roots.get(identifier);

    if (rootNode === undefined) {
      rootNode = {
        properties: new Map(),
        reactive,
        accessType: defaultAccessType,
      };
      roots.set(identifier, rootNode);
    } else {
      CompilerError.invariant(reactive === rootNode.reactive, {
        reason: '[DeriveMinimalDependenciesHIR] Conflicting reactive root flag',
        description: `Identifier ${printIdentifier(identifier)}`,
        loc: GeneratedSource,
      });
    }
    return rootNode;
  }

  /**
   * Join a dependency with `#hoistableObjects` to record the hoistable
   * dependency. This effectively truncates @param dep to its maximal
   * safe-to-evaluate subpath

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free