Home / Function/ inferReactiveScopeVariables() — react Function Reference

inferReactiveScopeVariables() — react Function Reference

Architecture documentation for the inferReactiveScopeVariables() function in InferReactiveScopeVariables.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  956ca4bb_7b0f_7eb2_4fbb_bda123ddbc98["inferReactiveScopeVariables()"]
  f041318d_301f_daad_4198_91d141b3039d["InferReactiveScopeVariables.ts"]
  956ca4bb_7b0f_7eb2_4fbb_bda123ddbc98 -->|defined in| f041318d_301f_daad_4198_91d141b3039d
  b474edef_a60d_b132_2ae0_98f6768ec241["findDisjointMutableValues()"]
  956ca4bb_7b0f_7eb2_4fbb_bda123ddbc98 -->|calls| b474edef_a60d_b132_2ae0_98f6768ec241
  4c434fb2_75ab_6c2c_c237_b1d38a1b0141["forEach()"]
  956ca4bb_7b0f_7eb2_4fbb_bda123ddbc98 -->|calls| 4c434fb2_75ab_6c2c_c237_b1d38a1b0141
  d0270ab6_a621_bd55_a1b9_a5cad8b406b2["makeInstructionId()"]
  956ca4bb_7b0f_7eb2_4fbb_bda123ddbc98 -->|calls| d0270ab6_a621_bd55_a1b9_a5cad8b406b2
  67fd32f8_58dc_29cf_d2ca_7d0f11d2763f["mergeLocation()"]
  956ca4bb_7b0f_7eb2_4fbb_bda123ddbc98 -->|calls| 67fd32f8_58dc_29cf_d2ca_7d0f11d2763f
  style 956ca4bb_7b0f_7eb2_4fbb_bda123ddbc98 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/InferReactiveScopeVariables.ts lines 86–172

export function inferReactiveScopeVariables(fn: HIRFunction): void {
  /*
   * Represents the set of reactive scopes as disjoint sets of identifiers
   * that mutate together.
   */
  const scopeIdentifiers = findDisjointMutableValues(fn);

  // Maps each scope (by its identifying member) to a ScopeId value
  const scopes: Map<Identifier, ReactiveScope> = new Map();

  /*
   * Iterate over all the identifiers and assign a unique ScopeId
   * for each scope (based on the set identifier).
   *
   * At the same time, group the identifiers in each scope and
   * build a MutableRange that describes the span of mutations
   * across all identifiers in each scope.
   */
  scopeIdentifiers.forEach((identifier, groupIdentifier) => {
    let scope = scopes.get(groupIdentifier);
    if (scope === undefined) {
      scope = {
        id: fn.env.nextScopeId,
        range: identifier.mutableRange,
        dependencies: new Set(),
        declarations: new Map(),
        reassignments: new Set(),
        earlyReturnValue: null,
        merged: new Set(),
        loc: identifier.loc,
      };
      scopes.set(groupIdentifier, scope);
    } else {
      if (scope.range.start === 0) {
        scope.range.start = identifier.mutableRange.start;
      } else if (identifier.mutableRange.start !== 0) {
        scope.range.start = makeInstructionId(
          Math.min(scope.range.start, identifier.mutableRange.start),
        );
      }
      scope.range.end = makeInstructionId(
        Math.max(scope.range.end, identifier.mutableRange.end),
      );
      scope.loc = mergeLocation(scope.loc, identifier.loc);
    }
    identifier.scope = scope;
    identifier.mutableRange = scope.range;
  });

  let maxInstruction = 0;
  for (const [, block] of fn.body.blocks) {
    for (const instr of block.instructions) {
      maxInstruction = makeInstructionId(Math.max(maxInstruction, instr.id));
    }
    maxInstruction = makeInstructionId(
      Math.max(maxInstruction, block.terminal.id),
    );
  }

  /*
   * Validate that all scopes have properly intialized, valid mutable ranges
   * within the span of instructions for this function, ie from 1 to 1 past
   * the last instruction id.
   */
  for (const [, scope] of scopes) {
    if (
      scope.range.start === 0 ||
      scope.range.end === 0 ||
      maxInstruction === 0 ||
      scope.range.end > maxInstruction + 1
    ) {
      // Make it easier to debug why the error occurred
      fn.env.logger?.debugLogIRs?.({
        kind: 'hir',
        name: 'InferReactiveScopeVariables (invalid scope)',
        value: fn,
      });
      CompilerError.invariant(false, {
        reason: `Invalid mutable range for scope`,
        description: `Scope @${scope.id} has range [${scope.range.start}:${
          scope.range.end

Domain

Subdomains

Frequently Asked Questions

What does inferReactiveScopeVariables() do?
inferReactiveScopeVariables() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/InferReactiveScopeVariables.ts.
Where is inferReactiveScopeVariables() defined?
inferReactiveScopeVariables() is defined in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/InferReactiveScopeVariables.ts at line 86.
What does inferReactiveScopeVariables() call?
inferReactiveScopeVariables() calls 4 function(s): findDisjointMutableValues, forEach, makeInstructionId, mergeLocation.

Analyze Your Own Codebase

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

Try Supermodel Free