Home / Function/ codegenForInit() — react Function Reference

codegenForInit() — react Function Reference

Architecture documentation for the codegenForInit() function in CodegenReactiveFunction.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  ba08546d_2fde_b751_b6d4_89a52ff181a5["codegenForInit()"]
  dc7f10c2_c914_a162_d02b_a10a15c64a5f["CodegenReactiveFunction.ts"]
  ba08546d_2fde_b751_b6d4_89a52ff181a5 -->|defined in| dc7f10c2_c914_a162_d02b_a10a15c64a5f
  4f146c25_f9b5_fd01_865f_04b2fc3071a8["codegenTerminal()"]
  4f146c25_f9b5_fd01_865f_04b2fc3071a8 -->|calls| ba08546d_2fde_b751_b6d4_89a52ff181a5
  b8125226_b7ac_716f_9ed0_db7e7d00571c["codegenBlock()"]
  ba08546d_2fde_b751_b6d4_89a52ff181a5 -->|calls| b8125226_b7ac_716f_9ed0_db7e7d00571c
  041ca752_10c1_3cda_1f5c_02f44a01310e["invariant()"]
  ba08546d_2fde_b751_b6d4_89a52ff181a5 -->|calls| 041ca752_10c1_3cda_1f5c_02f44a01310e
  073c81a5_c389_d108_5b8f_4d6dc6eece83["push()"]
  ba08546d_2fde_b751_b6d4_89a52ff181a5 -->|calls| 073c81a5_c389_d108_5b8f_4d6dc6eece83
  d2920090_42bc_7919_897f_42680a08ace6["codegenInstructionValueToExpression()"]
  ba08546d_2fde_b751_b6d4_89a52ff181a5 -->|calls| d2920090_42bc_7919_897f_42680a08ace6
  style ba08546d_2fde_b751_b6d4_89a52ff181a5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts lines 1382–1432

function codegenForInit(
  cx: Context,
  init: ReactiveValue,
): t.Expression | t.VariableDeclaration | null {
  if (init.kind === 'SequenceExpression') {
    const body = codegenBlock(
      cx,
      init.instructions.map(instruction => ({
        kind: 'instruction',
        instruction,
      })),
    ).body;
    const declarators: Array<t.VariableDeclarator> = [];
    let kind: 'let' | 'const' = 'const';
    body.forEach(instr => {
      let top: undefined | t.VariableDeclarator = undefined;
      if (
        instr.type === 'ExpressionStatement' &&
        instr.expression.type === 'AssignmentExpression' &&
        instr.expression.operator === '=' &&
        instr.expression.left.type === 'Identifier' &&
        (top = declarators.at(-1))?.id.type === 'Identifier' &&
        top?.id.name === instr.expression.left.name &&
        top?.init == null
      ) {
        top.init = instr.expression.right;
      } else {
        CompilerError.invariant(
          instr.type === 'VariableDeclaration' &&
            (instr.kind === 'let' || instr.kind === 'const'),
          {
            reason: 'Expected a variable declaration',
            description: `Got ${instr.type}`,
            loc: init.loc,
          },
        );
        if (instr.kind === 'let') {
          kind = 'let';
        }
        declarators.push(...instr.declarations);
      }
    });
    CompilerError.invariant(declarators.length > 0, {
      reason: 'Expected a variable declaration',
      loc: init.loc,
    });
    return t.variableDeclaration(kind, declarators);
  } else {
    return codegenInstructionValueToExpression(cx, init);
  }
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does codegenForInit() do?
codegenForInit() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts.
Where is codegenForInit() defined?
codegenForInit() is defined in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts at line 1382.
What does codegenForInit() call?
codegenForInit() calls 4 function(s): codegenBlock, codegenInstructionValueToExpression, invariant, push.
What calls codegenForInit()?
codegenForInit() is called by 1 function(s): codegenTerminal.

Analyze Your Own Codebase

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

Try Supermodel Free