Home / Function/ matchOptionalTestBlock() — react Function Reference

matchOptionalTestBlock() — react Function Reference

Architecture documentation for the matchOptionalTestBlock() function in CollectOptionalChainDependencies.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  7768e811_2923_bb27_1697_f9458cbb2d02["matchOptionalTestBlock()"]
  fd3023fa_cdd5_e8f0_669a_c459a0f89746["CollectOptionalChainDependencies.ts"]
  7768e811_2923_bb27_1697_f9458cbb2d02 -->|defined in| fd3023fa_cdd5_e8f0_669a_c459a0f89746
  8e720f44_2b97_3d0b_5bc7_8a340dc6f442["traverseOptionalBlock()"]
  8e720f44_2b97_3d0b_5bc7_8a340dc6f442 -->|calls| 7768e811_2923_bb27_1697_f9458cbb2d02
  7cd776c7_0283_e0eb_af1b_5474c371bd16["assertNonNull()"]
  7768e811_2923_bb27_1697_f9458cbb2d02 -->|calls| 7cd776c7_0283_e0eb_af1b_5474c371bd16
  e7a01d7c_c128_be66_a07d_007952a380da["printIdentifier()"]
  7768e811_2923_bb27_1697_f9458cbb2d02 -->|calls| e7a01d7c_c128_be66_a07d_007952a380da
  style 7768e811_2923_bb27_1697_f9458cbb2d02 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/HIR/CollectOptionalChainDependencies.ts lines 163–227

function matchOptionalTestBlock(
  terminal: BranchTerminal,
  blocks: ReadonlyMap<BlockId, BasicBlock>,
): {
  consequentId: IdentifierId;
  property: PropertyLiteral;
  propertyId: IdentifierId;
  storeLocalInstr: Instruction;
  consequentGoto: BlockId;
} | null {
  const consequentBlock = assertNonNull(blocks.get(terminal.consequent));
  if (
    consequentBlock.instructions.length === 2 &&
    consequentBlock.instructions[0].value.kind === 'PropertyLoad' &&
    consequentBlock.instructions[1].value.kind === 'StoreLocal'
  ) {
    const propertyLoad: TInstruction<PropertyLoad> = consequentBlock
      .instructions[0] as TInstruction<PropertyLoad>;
    const storeLocal: StoreLocal = consequentBlock.instructions[1].value;
    const storeLocalInstr = consequentBlock.instructions[1];
    CompilerError.invariant(
      propertyLoad.value.object.identifier.id === terminal.test.identifier.id,
      {
        reason:
          '[OptionalChainDeps] Inconsistent optional chaining property load',
        description: `Test=${printIdentifier(terminal.test.identifier)} PropertyLoad base=${printIdentifier(propertyLoad.value.object.identifier)}`,
        loc: propertyLoad.loc,
      },
    );

    CompilerError.invariant(
      storeLocal.value.identifier.id === propertyLoad.lvalue.identifier.id,
      {
        reason: '[OptionalChainDeps] Unexpected storeLocal',
        loc: propertyLoad.loc,
      },
    );
    if (
      consequentBlock.terminal.kind !== 'goto' ||
      consequentBlock.terminal.variant !== GotoVariant.Break
    ) {
      return null;
    }
    const alternate = assertNonNull(blocks.get(terminal.alternate));

    CompilerError.invariant(
      alternate.instructions.length === 2 &&
        alternate.instructions[0].value.kind === 'Primitive' &&
        alternate.instructions[1].value.kind === 'StoreLocal',
      {
        reason: 'Unexpected alternate structure',
        loc: terminal.loc,
      },
    );

    return {
      consequentId: storeLocal.lvalue.place.identifier.id,
      property: propertyLoad.value.property,
      propertyId: propertyLoad.lvalue.identifier.id,
      storeLocalInstr,
      consequentGoto: consequentBlock.terminal.block,
    };
  }
  return null;
}

Subdomains

Frequently Asked Questions

What does matchOptionalTestBlock() do?
matchOptionalTestBlock() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/HIR/CollectOptionalChainDependencies.ts.
Where is matchOptionalTestBlock() defined?
matchOptionalTestBlock() is defined in compiler/packages/babel-plugin-react-compiler/src/HIR/CollectOptionalChainDependencies.ts at line 163.
What does matchOptionalTestBlock() call?
matchOptionalTestBlock() calls 2 function(s): assertNonNull, printIdentifier.
What calls matchOptionalTestBlock()?
matchOptionalTestBlock() is called by 1 function(s): traverseOptionalBlock.

Analyze Your Own Codebase

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

Try Supermodel Free