Home / Class/ State Class — react Architecture

State Class — react Architecture

Architecture documentation for the State class in PruneNonEscapingScopes.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  f90e2ba3_ca16_9654_f6d1_9282625ea4a4["State"]
  c4112963_95fe_d8ed_3bfd_f6d45887acb7["PruneNonEscapingScopes.ts"]
  f90e2ba3_ca16_9654_f6d1_9282625ea4a4 -->|defined in| c4112963_95fe_d8ed_3bfd_f6d45887acb7
  2397beef_7276_4515_b563_9d031be256d5["constructor()"]
  f90e2ba3_ca16_9654_f6d1_9282625ea4a4 -->|method| 2397beef_7276_4515_b563_9d031be256d5
  73ad0fca_8aa6_a337_83a0_3ddb7277f89c["declare()"]
  f90e2ba3_ca16_9654_f6d1_9282625ea4a4 -->|method| 73ad0fca_8aa6_a337_83a0_3ddb7277f89c
  5efb04f8_fe7e_23f5_ada1_07f7e110af62["visitOperand()"]
  f90e2ba3_ca16_9654_f6d1_9282625ea4a4 -->|method| 5efb04f8_fe7e_23f5_ada1_07f7e110af62

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PruneNonEscapingScopes.ts lines 205–273

class State {
  env: Environment;
  /*
   * Maps lvalues for LoadLocal to the identifier being loaded, to resolve indirections
   * in subsequent lvalues/rvalues.
   *
   * NOTE: this pass uses DeclarationId rather than IdentifierId because the pass is not
   * aware of control-flow, only data flow via mutation. Instead of precisely modeling
   * control flow, we analyze all values that may flow into a particular program variable,
   * and then whether that program variable may escape (if so, the values flowing in may
   * escape too). Thus we use DeclarationId to captures all values that may flow into
   * a particular program variable, regardless of control flow paths.
   *
   * In the future when we convert to HIR everywhere this pass can account for control
   * flow and use SSA ids.
   */
  definitions: Map<DeclarationId, DeclarationId> = new Map();

  identifiers: Map<DeclarationId, IdentifierNode> = new Map();
  scopes: Map<ScopeId, ScopeNode> = new Map();
  escapingValues: Set<DeclarationId> = new Set();

  constructor(env: Environment) {
    this.env = env;
  }

  // Declare a new identifier, used for function id and params
  declare(id: DeclarationId): void {
    this.identifiers.set(id, {
      level: MemoizationLevel.Never,
      memoized: false,
      dependencies: new Set(),
      scopes: new Set(),
      seen: false,
    });
  }

  /*
   * Associates the identifier with its scope, if there is one and it is active for the given instruction id:
   * - Records the scope and its dependencies
   * - Associates the identifier with this scope
   */
  visitOperand(
    id: InstructionId,
    place: Place,
    identifier: DeclarationId,
  ): void {
    const scope = getPlaceScope(id, place);
    if (scope !== null) {
      let node = this.scopes.get(scope.id);
      if (node === undefined) {
        node = {
          dependencies: [...scope.dependencies].map(
            dep => dep.identifier.declarationId,
          ),
          seen: false,
        };
        this.scopes.set(scope.id, node);
      }
      const identifierNode = this.identifiers.get(identifier);
      CompilerError.invariant(identifierNode !== undefined, {
        reason: 'Expected identifier to be initialized',
        description: `[${id}] operand=${printPlace(place)} for identifier declaration ${identifier}`,
        loc: place.loc,
      });
      identifierNode.scopes.add(scope.id);
    }
  }
}

Domain

Frequently Asked Questions

What is the State class?
State is a class in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PruneNonEscapingScopes.ts.
Where is State defined?
State is defined in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PruneNonEscapingScopes.ts at line 205.

Analyze Your Own Codebase

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

Try Supermodel Free