Home / Function/ transformInstruction() — react Function Reference

transformInstruction() — react Function Reference

Architecture documentation for the transformInstruction() function in PruneHoistedContexts.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  f1e10939_7d28_07b3_187a_eb50166b9b8c["transformInstruction()"]
  d5fccea7_98aa_783b_f46b_c702031e387a["Visitor"]
  f1e10939_7d28_07b3_187a_eb50166b9b8c -->|defined in| d5fccea7_98aa_783b_f46b_c702031e387a
  af4d3127_0abf_3e44_50a8_d7a9dd0b9b58["visitInstruction()"]
  f1e10939_7d28_07b3_187a_eb50166b9b8c -->|calls| af4d3127_0abf_3e44_50a8_d7a9dd0b9b58
  40802d10_d9f1_10c2_2fff_626026f88fb4["transformInstruction()"]
  f1e10939_7d28_07b3_187a_eb50166b9b8c -->|calls| 40802d10_d9f1_10c2_2fff_626026f88fb4
  637ab5a6_5123_e897_e245_fce1dcb47f78["find()"]
  f1e10939_7d28_07b3_187a_eb50166b9b8c -->|calls| 637ab5a6_5123_e897_e245_fce1dcb47f78
  style f1e10939_7d28_07b3_187a_eb50166b9b8c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PruneHoistedContexts.ts lines 98–169

  override transformInstruction(
    instruction: ReactiveInstruction,
    state: VisitorState,
  ): Transformed<ReactiveStatement> {
    /**
     * Remove hoisted declarations to preserve TDZ
     */
    if (instruction.value.kind === 'DeclareContext') {
      const maybeNonHoisted = convertHoistedLValueKind(
        instruction.value.lvalue.kind,
      );
      if (maybeNonHoisted != null) {
        if (
          maybeNonHoisted === InstructionKind.Function &&
          state.uninitialized.has(instruction.value.lvalue.place.identifier.id)
        ) {
          state.uninitialized.set(
            instruction.value.lvalue.place.identifier.id,
            {
              kind: 'func',
              definition: null,
            },
          );
        }
        return {kind: 'remove'};
      }
    }
    if (
      instruction.value.kind === 'StoreContext' &&
      instruction.value.lvalue.kind !== InstructionKind.Reassign
    ) {
      /**
       * Rewrite StoreContexts let/const that will be pre-declared in
       * codegen to reassignments.
       */
      const lvalueId = instruction.value.lvalue.place.identifier.id;
      const isDeclaredByScope = state.activeScopes.find(scope =>
        scope.has(lvalueId),
      );
      if (isDeclaredByScope) {
        if (
          instruction.value.lvalue.kind === InstructionKind.Let ||
          instruction.value.lvalue.kind === InstructionKind.Const
        ) {
          instruction.value.lvalue.kind = InstructionKind.Reassign;
        } else if (instruction.value.lvalue.kind === InstructionKind.Function) {
          const maybeHoistedFn = state.uninitialized.get(lvalueId);
          if (maybeHoistedFn != null) {
            CompilerError.invariant(maybeHoistedFn.kind === 'func', {
              reason: '[PruneHoistedContexts] Unexpected hoisted function',
              loc: instruction.loc,
            });
            maybeHoistedFn.definition = instruction.value.lvalue.place;
            /**
             * References to hoisted functions are now "safe" as variable assignments
             * have finished.
             */
            state.uninitialized.delete(lvalueId);
          }
        } else {
          CompilerError.throwTodo({
            reason: '[PruneHoistedContexts] Unexpected kind',
            description: `(${instruction.value.lvalue.kind})`,
            loc: instruction.loc,
          });
        }
      }
    }

    this.visitInstruction(instruction, state);
    return {kind: 'keep'};
  }

Domain

Subdomains

Frequently Asked Questions

What does transformInstruction() do?
transformInstruction() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PruneHoistedContexts.ts.
Where is transformInstruction() defined?
transformInstruction() is defined in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PruneHoistedContexts.ts at line 98.
What does transformInstruction() call?
transformInstruction() calls 3 function(s): find, transformInstruction, visitInstruction.

Analyze Your Own Codebase

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

Try Supermodel Free