Home / Function/ writeOptionalDependency() — react Function Reference

writeOptionalDependency() — react Function Reference

Architecture documentation for the writeOptionalDependency() function in ScopeDependencyUtils.ts from the react codebase.

Function typescript MIRInfrastructure HIR calls 10 called by 1

Entity Profile

Dependency Diagram

graph TD
  8e8a7c07_79b4_f2fb_53bd_2e3554679223["writeOptionalDependency()"]
  a4fd6965_224d_0ddc_a6f9_b2f74cfd3994["ScopeDependencyUtils.ts"]
  8e8a7c07_79b4_f2fb_53bd_2e3554679223 -->|defined in| a4fd6965_224d_0ddc_a6f9_b2f74cfd3994
  30907c46_684f_39cf_12ac_900130b902f4["buildDependencyInstructions()"]
  30907c46_684f_39cf_12ac_900130b902f4 -->|calls| 8e8a7c07_79b4_f2fb_53bd_2e3554679223
  4e7c3351_9e52_0196_d1f4_d8371b23d0cb["makeTemporaryIdentifier()"]
  8e8a7c07_79b4_f2fb_53bd_2e3554679223 -->|calls| 4e7c3351_9e52_0196_d1f4_d8371b23d0cb
  2a23449a_1dec_0ba3_8f5a_0e9e83d42add["reserve()"]
  8e8a7c07_79b4_f2fb_53bd_2e3554679223 -->|calls| 2a23449a_1dec_0ba3_8f5a_0e9e83d42add
  012a7acd_73d1_2699_77e8_481232bb47c8["currentBlockKind()"]
  8e8a7c07_79b4_f2fb_53bd_2e3554679223 -->|calls| 012a7acd_73d1_2699_77e8_481232bb47c8
  3db1e7ce_1c6f_24e2_3cbb_86c02c29d285["enter()"]
  8e8a7c07_79b4_f2fb_53bd_2e3554679223 -->|calls| 3db1e7ce_1c6f_24e2_3cbb_86c02c29d285
  f1f0c182_09ad_b29e_b1ca_56f10046adff["lowerValueToTemporary()"]
  8e8a7c07_79b4_f2fb_53bd_2e3554679223 -->|calls| f1f0c182_09ad_b29e_b1ca_56f10046adff
  d0270ab6_a621_bd55_a1b9_a5cad8b406b2["makeInstructionId()"]
  8e8a7c07_79b4_f2fb_53bd_2e3554679223 -->|calls| d0270ab6_a621_bd55_a1b9_a5cad8b406b2
  041ca752_10c1_3cda_1f5c_02f44a01310e["invariant()"]
  8e8a7c07_79b4_f2fb_53bd_2e3554679223 -->|calls| 041ca752_10c1_3cda_1f5c_02f44a01310e
  d0800e5a_2d5f_1c1b_169c_b6c5cc895bee["writeNonOptionalDependency()"]
  8e8a7c07_79b4_f2fb_53bd_2e3554679223 -->|calls| d0800e5a_2d5f_1c1b_169c_b6c5cc895bee
  10c63c7e_e339_2c92_cb9e_11d13186c6a7["enterReserved()"]
  8e8a7c07_79b4_f2fb_53bd_2e3554679223 -->|calls| 10c63c7e_e339_2c92_cb9e_11d13186c6a7
  43f67b81_ddfd_4289_903f_8e9e60469eb7["terminateWithContinuation()"]
  8e8a7c07_79b4_f2fb_53bd_2e3554679223 -->|calls| 43f67b81_ddfd_4289_903f_8e9e60469eb7
  style 8e8a7c07_79b4_f2fb_53bd_2e3554679223 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/HIR/ScopeDependencyUtils.ts lines 135–281

function writeOptionalDependency(
  dep: ReactiveScopeDependency,
  builder: HIRBuilder,
  parentAlternate: BlockId | null,
): Identifier {
  const env = builder.environment;
  /**
   * Reserve an identifier which will be used to store the result of this
   * dependency.
   */
  const dependencyValue: Place = {
    kind: 'Identifier',
    identifier: makeTemporaryIdentifier(env.nextIdentifierId, GeneratedSource),
    effect: Effect.Mutate,
    reactive: dep.reactive,
    loc: GeneratedSource,
  };

  /**
   * Reserve a block which is the fallthrough (and transitive successor) of this
   * optional chain.
   */
  const continuationBlock = builder.reserve(builder.currentBlockKind());
  let alternate;
  if (parentAlternate != null) {
    alternate = parentAlternate;
  } else {
    /**
     * If an outermost alternate block has not been reserved, write one
     *
     * $N = Primitive undefined
     * $M = StoreLocal $OptionalResult = $N
     * goto fallthrough
     */
    alternate = builder.enter('value', () => {
      const temp = lowerValueToTemporary(builder, {
        kind: 'Primitive',
        value: undefined,
        loc: GeneratedSource,
      });
      lowerValueToTemporary(builder, {
        kind: 'StoreLocal',
        lvalue: {kind: InstructionKind.Const, place: {...dependencyValue}},
        value: {...temp},
        type: null,
        loc: GeneratedSource,
      });
      return {
        kind: 'goto',
        variant: GotoVariant.Break,
        block: continuationBlock.id,
        id: makeInstructionId(0),
        loc: GeneratedSource,
      };
    });
  }

  // Reserve the consequent block, which is the successor of the test block
  const consequent = builder.reserve('value');

  let testIdentifier: Identifier | null = null;
  const testBlock = builder.enter('value', () => {
    const testDependency = {
      ...dep,
      path: dep.path.slice(0, dep.path.length - 1),
    };
    const firstOptional = dep.path.findIndex(path => path.optional);
    CompilerError.invariant(firstOptional !== -1, {
      reason:
        '[ScopeDependencyUtils] Internal invariant broken: expected optional path',
      loc: dep.identifier.loc,
    });
    if (firstOptional === dep.path.length - 1) {
      // Base case: the test block is simple
      testIdentifier = writeNonOptionalDependency(testDependency, env, builder);
    } else {
      // Otherwise, the test block is a nested optional chain
      testIdentifier = writeOptionalDependency(
        testDependency,
        builder,
        alternate,

Subdomains

Frequently Asked Questions

What does writeOptionalDependency() do?
writeOptionalDependency() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/HIR/ScopeDependencyUtils.ts.
Where is writeOptionalDependency() defined?
writeOptionalDependency() is defined in compiler/packages/babel-plugin-react-compiler/src/HIR/ScopeDependencyUtils.ts at line 135.
What does writeOptionalDependency() call?
writeOptionalDependency() calls 10 function(s): currentBlockKind, enter, enterReserved, invariant, lowerValueToTemporary, makeInstructionId, makeTemporaryIdentifier, reserve, and 2 more.
What calls writeOptionalDependency()?
writeOptionalDependency() is called by 1 function(s): buildDependencyInstructions.

Analyze Your Own Codebase

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

Try Supermodel Free