Home / Function/ validateEffect() — react Function Reference

validateEffect() — react Function Reference

Architecture documentation for the validateEffect() function in ValidateNoDerivedComputationsInEffects_exp.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  0a2759e9_eb11_9254_ac2c_0d70b0fced94["validateEffect()"]
  1cc88eb1_0167_fe28_e1ba_78f39be08588["ValidateNoDerivedComputationsInEffects_exp.ts"]
  0a2759e9_eb11_9254_ac2c_0d70b0fced94 -->|defined in| 1cc88eb1_0167_fe28_e1ba_78f39be08588
  50b63a19_f734_67aa_f77b_f1ae98b9cc1a["validateNoDerivedComputationsInEffects_exp()"]
  50b63a19_f734_67aa_f77b_f1ae98b9cc1a -->|calls| 0a2759e9_eb11_9254_ac2c_0d70b0fced94
  562ab41b_bff9_3215_bd10_b814d070fc98["getRootSetState()"]
  0a2759e9_eb11_9254_ac2c_0d70b0fced94 -->|calls| 562ab41b_bff9_3215_bd10_b814d070fc98
  b3466f23_fe2e_2d32_2f7f_ebd616e19d8a["getFnLocalDeps()"]
  0a2759e9_eb11_9254_ac2c_0d70b0fced94 -->|calls| b3466f23_fe2e_2d32_2f7f_ebd616e19d8a
  69c5847a_ade1_89ca_0b76_a32c6081bd63["maybeRecordSetState()"]
  0a2759e9_eb11_9254_ac2c_0d70b0fced94 -->|calls| 69c5847a_ade1_89ca_0b76_a32c6081bd63
  6e1ebdb8_441d_b6d1_1537_2c5ba936ec85["eachInstructionOperand()"]
  0a2759e9_eb11_9254_ac2c_0d70b0fced94 -->|calls| 6e1ebdb8_441d_b6d1_1537_2c5ba936ec85
  5f694d00_db22_ca76_929e_f1ed21313ce8["push()"]
  0a2759e9_eb11_9254_ac2c_0d70b0fced94 -->|calls| 5f694d00_db22_ca76_929e_f1ed21313ce8
  339d9cd8_511d_b95b_e510_8dd0f54b3782["buildTreeNode()"]
  0a2759e9_eb11_9254_ac2c_0d70b0fced94 -->|calls| 339d9cd8_511d_b95b_e510_8dd0f54b3782
  e2bc7c28_00d4_5bf1_a68d_397453223518["renderTree()"]
  0a2759e9_eb11_9254_ac2c_0d70b0fced94 -->|calls| e2bc7c28_00d4_5bf1_a68d_397453223518
  2d45b62f_3ea0_e6ea_47e4_c87fd21c13e3["pushDiagnostic()"]
  0a2759e9_eb11_9254_ac2c_0d70b0fced94 -->|calls| 2d45b62f_3ea0_e6ea_47e4_c87fd21c13e3
  e51b0172_6996_d9d2_caaa_8fd5b1d31c7e["create()"]
  0a2759e9_eb11_9254_ac2c_0d70b0fced94 -->|calls| e51b0172_6996_d9d2_caaa_8fd5b1d31c7e
  74a94732_eaa0_4ef6_8347_8518c71b78b8["withDetails()"]
  0a2759e9_eb11_9254_ac2c_0d70b0fced94 -->|calls| 74a94732_eaa0_4ef6_8347_8518c71b78b8
  28a3684c_b5a5_8d21_af43_08e51dda5f53["map()"]
  0a2759e9_eb11_9254_ac2c_0d70b0fced94 -->|calls| 28a3684c_b5a5_8d21_af43_08e51dda5f53
  style 0a2759e9_eb11_9254_ac2c_0d70b0fced94 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoDerivedComputationsInEffects_exp.ts lines 631–842

function validateEffect(
  effectFunction: HIRFunction,
  dependencies: ArrayExpression,
  context: ValidationContext,
): void {
  const seenBlocks: Set<BlockId> = new Set();

  const effectDerivedSetStateCalls: Array<{
    value: CallExpression;
    id: IdentifierId;
    sourceIds: Set<IdentifierId>;
    typeOfValue: TypeOfValue;
  }> = [];

  const effectSetStateUsages: Map<
    IdentifierId,
    Set<SourceLocation>
  > = new Map();

  // Consider setStates in the effect's dependency array as being part of effectSetStateUsages
  for (const dep of dependencies.elements) {
    if (dep.kind === 'Identifier') {
      const root = getRootSetState(dep.identifier.id, context.setStateLoads);
      if (root !== null) {
        effectSetStateUsages.set(root, new Set([dep.loc]));
      }
    }
  }

  let cleanUpFunctionDeps: Set<IdentifierId> | undefined;

  const globals: Set<IdentifierId> = new Set();
  for (const block of effectFunction.body.blocks.values()) {
    /*
     * if the block is in an effect and is of type return then its an effect's cleanup function
     * if the cleanup function depends on a value from which effect-set state is derived then
     * we can't validate
     */
    if (
      block.terminal.kind === 'return' &&
      block.terminal.returnVariant === 'Explicit'
    ) {
      cleanUpFunctionDeps = getFnLocalDeps(
        context.functions.get(block.terminal.value.identifier.id),
      );
    }
    for (const pred of block.preds) {
      if (!seenBlocks.has(pred)) {
        // skip if block has a back edge
        return;
      }
    }

    for (const instr of block.instructions) {
      // Early return if any instruction is deriving a value from a ref
      if (isUseRefType(instr.lvalue.identifier)) {
        return;
      }

      maybeRecordSetState(instr, context.setStateLoads, effectSetStateUsages);

      for (const operand of eachInstructionOperand(instr)) {
        if (context.setStateLoads.has(operand.identifier.id)) {
          const rootSetStateId = getRootSetState(
            operand.identifier.id,
            context.setStateLoads,
          );
          if (rootSetStateId !== null) {
            effectSetStateUsages.get(rootSetStateId)?.add(operand.loc);
          }
        }
      }

      if (
        instr.value.kind === 'CallExpression' &&
        isSetStateType(instr.value.callee.identifier) &&
        instr.value.args.length === 1 &&
        instr.value.args[0].kind === 'Identifier'
      ) {
        const calleeMetadata = context.derivationCache.cache.get(
          instr.value.callee.identifier.id,

Domain

Subdomains

Frequently Asked Questions

What does validateEffect() do?
validateEffect() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoDerivedComputationsInEffects_exp.ts.
Where is validateEffect() defined?
validateEffect() is defined in compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoDerivedComputationsInEffects_exp.ts at line 631.
What does validateEffect() call?
validateEffect() calls 11 function(s): buildTreeNode, create, eachInstructionOperand, getFnLocalDeps, getRootSetState, map, maybeRecordSetState, push, and 3 more.
What calls validateEffect()?
validateEffect() is called by 1 function(s): validateNoDerivedComputationsInEffects_exp.

Analyze Your Own Codebase

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

Try Supermodel Free