Home / Class/ ScopeBlockTraversal Class — react Architecture

ScopeBlockTraversal Class — react Architecture

Architecture documentation for the ScopeBlockTraversal class in visitors.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  5ffcc39a_a87d_d30c_d2cd_be7a39db81a1["ScopeBlockTraversal"]
  2f3caf55_cc64_415c_55dd_9771ba7dc210["visitors.ts"]
  5ffcc39a_a87d_d30c_d2cd_be7a39db81a1 -->|defined in| 2f3caf55_cc64_415c_55dd_9771ba7dc210
  e905b26b_28fa_658a_742c_b1836921c75b["recordScopes()"]
  5ffcc39a_a87d_d30c_d2cd_be7a39db81a1 -->|method| e905b26b_28fa_658a_742c_b1836921c75b
  04fbfd80_5660_5399_93fe_70bf0d466dcb["isScopeActive()"]
  5ffcc39a_a87d_d30c_d2cd_be7a39db81a1 -->|method| 04fbfd80_5660_5399_93fe_70bf0d466dcb
  10b5b17a_f3c2_cc80_4b16_d9daf5061245["currentScope()"]
  5ffcc39a_a87d_d30c_d2cd_be7a39db81a1 -->|method| 10b5b17a_f3c2_cc80_4b16_d9daf5061245

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/HIR/visitors.ts lines 1238–1310

export class ScopeBlockTraversal {
  // Live stack of active scopes
  #activeScopes: Array<ScopeId> = [];
  blockInfos: Map<
    BlockId,
    | {
        kind: 'end';
        scope: ReactiveScope;
        pruned: boolean;
      }
    | {
        kind: 'begin';
        scope: ReactiveScope;
        pruned: boolean;
        fallthrough: BlockId;
      }
  > = new Map();

  recordScopes(block: BasicBlock): void {
    const blockInfo = this.blockInfos.get(block.id);
    if (blockInfo?.kind === 'begin') {
      this.#activeScopes.push(blockInfo.scope.id);
    } else if (blockInfo?.kind === 'end') {
      const top = this.#activeScopes.at(-1);
      CompilerError.invariant(blockInfo.scope.id === top, {
        reason:
          'Expected traversed block fallthrough to match top-most active scope',
        loc: block.instructions[0]?.loc ?? block.terminal.loc,
      });
      this.#activeScopes.pop();
    }

    if (
      block.terminal.kind === 'scope' ||
      block.terminal.kind === 'pruned-scope'
    ) {
      CompilerError.invariant(
        !this.blockInfos.has(block.terminal.block) &&
          !this.blockInfos.has(block.terminal.fallthrough),
        {
          reason: 'Expected unique scope blocks and fallthroughs',
          loc: block.terminal.loc,
        },
      );
      this.blockInfos.set(block.terminal.block, {
        kind: 'begin',
        scope: block.terminal.scope,
        pruned: block.terminal.kind === 'pruned-scope',
        fallthrough: block.terminal.fallthrough,
      });
      this.blockInfos.set(block.terminal.fallthrough, {
        kind: 'end',
        scope: block.terminal.scope,
        pruned: block.terminal.kind === 'pruned-scope',
      });
    }
  }

  /**
   * @returns if the given scope is currently 'active', i.e. if the scope start
   * block but not the scope fallthrough has been recorded.
   */
  isScopeActive(scopeId: ScopeId): boolean {
    return this.#activeScopes.indexOf(scopeId) !== -1;
  }

  /**
   * The current, innermost active scope.
   */
  get currentScope(): ScopeId | null {
    return this.#activeScopes.at(-1) ?? null;
  }
}

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free