Home / Function/ getContextReassignment() — react Function Reference

getContextReassignment() — react Function Reference

Architecture documentation for the getContextReassignment() function in ValidateLocalsNotReassignedAfterRender.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  176abc6d_b49b_3a9c_1432_7b5b6d45f2ac["getContextReassignment()"]
  5b745296_c60b_00ed_f4d9_4badd89a2f2b["ValidateLocalsNotReassignedAfterRender.ts"]
  176abc6d_b49b_3a9c_1432_7b5b6d45f2ac -->|defined in| 5b745296_c60b_00ed_f4d9_4badd89a2f2b
  42a1b6ad_eddb_6bd3_4831_1d199766f971["validateLocalsNotReassignedAfterRender()"]
  42a1b6ad_eddb_6bd3_4831_1d199766f971 -->|calls| 176abc6d_b49b_3a9c_1432_7b5b6d45f2ac
  b2fc2985_a7ba_9865_c2a3_2a7531f27d44["eachInstructionValueOperand()"]
  176abc6d_b49b_3a9c_1432_7b5b6d45f2ac -->|calls| b2fc2985_a7ba_9865_c2a3_2a7531f27d44
  02303def_636f_c5b3_a751_1cf138fcea69["pushDiagnostic()"]
  176abc6d_b49b_3a9c_1432_7b5b6d45f2ac -->|calls| 02303def_636f_c5b3_a751_1cf138fcea69
  ac13f5c1_be17_dd7a_6bd3_66d91c46aadf["create()"]
  176abc6d_b49b_3a9c_1432_7b5b6d45f2ac -->|calls| ac13f5c1_be17_dd7a_6bd3_66d91c46aadf
  1a2b7047_24c8_62d6_b328_5f07307d27ab["withDetails()"]
  176abc6d_b49b_3a9c_1432_7b5b6d45f2ac -->|calls| 1a2b7047_24c8_62d6_b328_5f07307d27ab
  d2c89465_144e_76da_505e_4a2de182f6f4["getFunctionCallSignature()"]
  176abc6d_b49b_3a9c_1432_7b5b6d45f2ac -->|calls| d2c89465_144e_76da_505e_4a2de182f6f4
  041ca752_10c1_3cda_1f5c_02f44a01310e["invariant()"]
  176abc6d_b49b_3a9c_1432_7b5b6d45f2ac -->|calls| 041ca752_10c1_3cda_1f5c_02f44a01310e
  10043bf1_f7ee_9ed9_307a_fe3edfd02b09["eachInstructionLValue()"]
  176abc6d_b49b_3a9c_1432_7b5b6d45f2ac -->|calls| 10043bf1_f7ee_9ed9_307a_fe3edfd02b09
  41232a25_deb6_6e83_05a8_ae9f961656f7["eachTerminalOperand()"]
  176abc6d_b49b_3a9c_1432_7b5b6d45f2ac -->|calls| 41232a25_deb6_6e83_05a8_ae9f961656f7
  style 176abc6d_b49b_3a9c_1432_7b5b6d45f2ac fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateLocalsNotReassignedAfterRender.ts lines 53–229

function getContextReassignment(
  fn: HIRFunction,
  contextVariables: Set<IdentifierId>,
  isFunctionExpression: boolean,
  isAsync: boolean,
): Place | null {
  const reassigningFunctions = new Map<IdentifierId, Place>();
  for (const [, block] of fn.body.blocks) {
    for (const instr of block.instructions) {
      const {lvalue, value} = instr;
      switch (value.kind) {
        case 'FunctionExpression':
        case 'ObjectMethod': {
          let reassignment = getContextReassignment(
            value.loweredFunc.func,
            contextVariables,
            true,
            isAsync || value.loweredFunc.func.async,
          );
          if (reassignment === null) {
            // If the function itself doesn't reassign, does one of its dependencies?
            for (const operand of eachInstructionValueOperand(value)) {
              const reassignmentFromOperand = reassigningFunctions.get(
                operand.identifier.id,
              );
              if (reassignmentFromOperand !== undefined) {
                reassignment = reassignmentFromOperand;
                break;
              }
            }
          }
          // if the function or its depends reassign, propagate that fact on the lvalue
          if (reassignment !== null) {
            if (isAsync || value.loweredFunc.func.async) {
              const errors = new CompilerError();
              const variable =
                reassignment.identifier.name !== null &&
                reassignment.identifier.name.kind === 'named'
                  ? `\`${reassignment.identifier.name.value}\``
                  : 'variable';
              errors.pushDiagnostic(
                CompilerDiagnostic.create({
                  category: ErrorCategory.Immutability,
                  reason: 'Cannot reassign variable in async function',
                  description:
                    'Reassigning a variable in an async function can cause inconsistent behavior on subsequent renders. Consider using state instead',
                }).withDetails({
                  kind: 'error',
                  loc: reassignment.loc,
                  message: `Cannot reassign ${variable}`,
                }),
              );
              throw errors;
            }
            reassigningFunctions.set(lvalue.identifier.id, reassignment);
          }
          break;
        }
        case 'StoreLocal': {
          const reassignment = reassigningFunctions.get(
            value.value.identifier.id,
          );
          if (reassignment !== undefined) {
            reassigningFunctions.set(
              value.lvalue.place.identifier.id,
              reassignment,
            );
            reassigningFunctions.set(lvalue.identifier.id, reassignment);
          }
          break;
        }
        case 'LoadLocal': {
          const reassignment = reassigningFunctions.get(
            value.place.identifier.id,
          );
          if (reassignment !== undefined) {
            reassigningFunctions.set(lvalue.identifier.id, reassignment);
          }
          break;
        }
        case 'DeclareContext': {

Domain

Subdomains

Frequently Asked Questions

What does getContextReassignment() do?
getContextReassignment() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateLocalsNotReassignedAfterRender.ts.
Where is getContextReassignment() defined?
getContextReassignment() is defined in compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateLocalsNotReassignedAfterRender.ts at line 53.
What does getContextReassignment() call?
getContextReassignment() calls 8 function(s): create, eachInstructionLValue, eachInstructionValueOperand, eachTerminalOperand, getFunctionCallSignature, invariant, pushDiagnostic, withDetails.
What calls getContextReassignment()?
getContextReassignment() is called by 1 function(s): validateLocalsNotReassignedAfterRender.

Analyze Your Own Codebase

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

Try Supermodel Free