Home / Class/ Transform Class — react Architecture

Transform Class — react Architecture

Architecture documentation for the Transform class in MergeReactiveScopesThatInvalidateTogether.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  ae117d0c_24d0_3002_1a6e_1e952a421d6e["Transform"]
  006711fe_6949_9be8_4726_2031284f3328["MergeReactiveScopesThatInvalidateTogether.ts"]
  ae117d0c_24d0_3002_1a6e_1e952a421d6e -->|defined in| 006711fe_6949_9be8_4726_2031284f3328
  56837a12_3f46_a94b_add2_4597eb8dc6e3["constructor()"]
  ae117d0c_24d0_3002_1a6e_1e952a421d6e -->|method| 56837a12_3f46_a94b_add2_4597eb8dc6e3
  b2563d99_6456_0163_2dd9_9a9290ad87a8["transformScope()"]
  ae117d0c_24d0_3002_1a6e_1e952a421d6e -->|method| b2563d99_6456_0163_2dd9_9a9290ad87a8
  11690004_c468_3b6b_f07f_36a6501f870a["visitBlock()"]
  ae117d0c_24d0_3002_1a6e_1e952a421d6e -->|method| 11690004_c468_3b6b_f07f_36a6501f870a

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/MergeReactiveScopesThatInvalidateTogether.ts lines 121–399

class Transform extends ReactiveFunctionTransform<ReactiveScopeDependencies | null> {
  lastUsage: Map<DeclarationId, InstructionId>;
  temporaries: Map<DeclarationId, DeclarationId> = new Map();

  constructor(lastUsage: Map<DeclarationId, InstructionId>) {
    super();
    this.lastUsage = lastUsage;
  }

  override transformScope(
    scopeBlock: ReactiveScopeBlock,
    state: ReactiveScopeDependencies | null,
  ): Transformed<ReactiveStatement> {
    this.visitScope(scopeBlock, scopeBlock.scope.dependencies);
    if (
      state !== null &&
      areEqualDependencies(state, scopeBlock.scope.dependencies)
    ) {
      return {kind: 'replace-many', value: scopeBlock.instructions};
    } else {
      return {kind: 'keep'};
    }
  }

  override visitBlock(
    block: ReactiveBlock,
    state: ReactiveScopeDependencies | null,
  ): void {
    // Pass 1: visit nested blocks to potentially merge their scopes
    this.traverseBlock(block, state);

    // Pass 2: identify scopes for merging
    type MergedScope = {
      block: ReactiveScopeBlock;
      from: number;
      to: number;
      lvalues: Set<DeclarationId>;
    };
    let current: MergedScope | null = null;
    const merged: Array<MergedScope> = [];
    function reset(): void {
      CompilerError.invariant(current !== null, {
        reason:
          'MergeConsecutiveScopes: expected current scope to be non-null if reset()',
        loc: GeneratedSource,
      });
      if (current.to > current.from + 1) {
        merged.push(current);
      }
      current = null;
    }
    for (let i = 0; i < block.length; i++) {
      const instr = block[i]!;
      switch (instr.kind) {
        case 'terminal': {
          // For now we don't merge across terminals
          if (current !== null) {
            log(
              `Reset scope @${current.block.scope.id} from terminal [${instr.terminal.id}]`,
            );
            reset();
          }
          break;
        }
        case 'pruned-scope': {
          // For now we don't merge across pruned scopes
          if (current !== null) {
            log(
              `Reset scope @${current.block.scope.id} from pruned scope @${instr.scope.id}`,
            );
            reset();
          }
          break;
        }
        case 'instruction': {
          switch (instr.instruction.value.kind) {
            case 'BinaryExpression':
            case 'ComputedLoad':
            case 'JSXText':
            case 'LoadGlobal':
            case 'LoadLocal':

Domain

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free