Home / File/ StabilizeBlockIds.ts — react Source File

StabilizeBlockIds.ts — react Source File

Architecture documentation for StabilizeBlockIds.ts, a typescript file in the react codebase. 6 imports, 1 dependents.

File typescript BabelCompiler Validation 6 imports 1 dependents 1 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  cc16bb5e_e9d5_74bd_0764_79875a9ffa5b["StabilizeBlockIds.ts"]
  0423f759_97e0_9101_4634_ed555abc5ca9["index.ts"]
  cc16bb5e_e9d5_74bd_0764_79875a9ffa5b --> 0423f759_97e0_9101_4634_ed555abc5ca9
  eb9d33f9_42c1_205c_93e6_8e1365a31839["utils.ts"]
  cc16bb5e_e9d5_74bd_0764_79875a9ffa5b --> eb9d33f9_42c1_205c_93e6_8e1365a31839
  14f2e51a_d755_814e_2f56_72d3ed119459["getOrInsertDefault"]
  cc16bb5e_e9d5_74bd_0764_79875a9ffa5b --> 14f2e51a_d755_814e_2f56_72d3ed119459
  21609915_b03a_fd75_b58a_4cb86ef9315b["visitors.ts"]
  cc16bb5e_e9d5_74bd_0764_79875a9ffa5b --> 21609915_b03a_fd75_b58a_4cb86ef9315b
  171a5d22_bb6b_1c99_05a4_6ad897438a35["ReactiveFunctionVisitor"]
  cc16bb5e_e9d5_74bd_0764_79875a9ffa5b --> 171a5d22_bb6b_1c99_05a4_6ad897438a35
  2435b5f8_41a6_0458_ba88_4479b965455f["visitReactiveFunction"]
  cc16bb5e_e9d5_74bd_0764_79875a9ffa5b --> 2435b5f8_41a6_0458_ba88_4479b965455f
  e3cfc07a_10c8_5dcd_e270_e8e14c29309b["Pipeline.ts"]
  e3cfc07a_10c8_5dcd_e270_e8e14c29309b --> cc16bb5e_e9d5_74bd_0764_79875a9ffa5b
  style cc16bb5e_e9d5_74bd_0764_79875a9ffa5b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import {
  BlockId,
  ReactiveFunction,
  ReactiveScopeBlock,
  ReactiveTerminalStatement,
  makeBlockId,
} from '../HIR';
import {getOrInsertDefault} from '../Utils/utils';
import {ReactiveFunctionVisitor, visitReactiveFunction} from './visitors';

export function stabilizeBlockIds(fn: ReactiveFunction): void {
  const referenced: Set<BlockId> = new Set();
  visitReactiveFunction(fn, new CollectReferencedLabels(), referenced);

  const mappings = new Map<BlockId, BlockId>();
  for (const blockId of referenced) {
    mappings.set(blockId, makeBlockId(mappings.size));
  }

  visitReactiveFunction(fn, new RewriteBlockIds(), mappings);
}

class CollectReferencedLabels extends ReactiveFunctionVisitor<Set<BlockId>> {
  override visitScope(scope: ReactiveScopeBlock, state: Set<BlockId>): void {
    const {earlyReturnValue} = scope.scope;
    if (earlyReturnValue != null) {
      state.add(earlyReturnValue.label);
    }
    this.traverseScope(scope, state);
  }
  override visitTerminal(
    stmt: ReactiveTerminalStatement,
    state: Set<BlockId>,
  ): void {
    if (stmt.label != null) {
      if (!stmt.label.implicit) {
        state.add(stmt.label.id);
      }
    }
    this.traverseTerminal(stmt, state);
  }
}

class RewriteBlockIds extends ReactiveFunctionVisitor<Map<BlockId, BlockId>> {
  override visitScope(
    scope: ReactiveScopeBlock,
    state: Map<BlockId, BlockId>,
  ): void {
    const {earlyReturnValue} = scope.scope;
    if (earlyReturnValue != null) {
      const rewrittenId = getOrInsertDefault(
        state,
        earlyReturnValue.label,
        state.size,
      );
      earlyReturnValue.label = makeBlockId(rewrittenId);
    }
    this.traverseScope(scope, state);
  }
  override visitTerminal(
    stmt: ReactiveTerminalStatement,
    state: Map<BlockId, BlockId>,
  ): void {
    if (stmt.label != null) {
      const rewrittenId = getOrInsertDefault(state, stmt.label.id, state.size);
      stmt.label.id = makeBlockId(rewrittenId);
    }

    const terminal = stmt.terminal;
    if (terminal.kind === 'break' || terminal.kind === 'continue') {
      const rewrittenId = getOrInsertDefault(
        state,
        terminal.target,
        state.size,
      );
      terminal.target = makeBlockId(rewrittenId);
    }
    this.traverseTerminal(stmt, state);
  }
}

Domain

Subdomains

Frequently Asked Questions

What does StabilizeBlockIds.ts do?
StabilizeBlockIds.ts is a source file in the react codebase, written in typescript. It belongs to the BabelCompiler domain, Validation subdomain.
What functions are defined in StabilizeBlockIds.ts?
StabilizeBlockIds.ts defines 1 function(s): stabilizeBlockIds.
What does StabilizeBlockIds.ts depend on?
StabilizeBlockIds.ts imports 6 module(s): ReactiveFunctionVisitor, getOrInsertDefault, index.ts, utils.ts, visitReactiveFunction, visitors.ts.
What files import StabilizeBlockIds.ts?
StabilizeBlockIds.ts is imported by 1 file(s): Pipeline.ts.
Where is StabilizeBlockIds.ts in the architecture?
StabilizeBlockIds.ts is located at compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/StabilizeBlockIds.ts (domain: BabelCompiler, subdomain: Validation, directory: compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes).

Analyze Your Own Codebase

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

Try Supermodel Free