Home / Function/ validateNoFreezingKnownMutableFunctions() — react Function Reference

validateNoFreezingKnownMutableFunctions() — react Function Reference

Architecture documentation for the validateNoFreezingKnownMutableFunctions() function in ValidateNoFreezingKnownMutableFunctions.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  58861cab_871f_3d90_099d_42afe0e9f6b3["validateNoFreezingKnownMutableFunctions()"]
  8ecac9ec_c10d_6d28_acad_1f65ff39ebfb["ValidateNoFreezingKnownMutableFunctions.ts"]
  58861cab_871f_3d90_099d_42afe0e9f6b3 -->|defined in| 8ecac9ec_c10d_6d28_acad_1f65ff39ebfb
  c3bc3875_256f_8f5e_7800_2f9c5bae65eb["runWithEnvironment()"]
  c3bc3875_256f_8f5e_7800_2f9c5bae65eb -->|calls| 58861cab_871f_3d90_099d_42afe0e9f6b3
  02303def_636f_c5b3_a751_1cf138fcea69["pushDiagnostic()"]
  58861cab_871f_3d90_099d_42afe0e9f6b3 -->|calls| 02303def_636f_c5b3_a751_1cf138fcea69
  ac13f5c1_be17_dd7a_6bd3_66d91c46aadf["create()"]
  58861cab_871f_3d90_099d_42afe0e9f6b3 -->|calls| ac13f5c1_be17_dd7a_6bd3_66d91c46aadf
  1a2b7047_24c8_62d6_b328_5f07307d27ab["withDetails()"]
  58861cab_871f_3d90_099d_42afe0e9f6b3 -->|calls| 1a2b7047_24c8_62d6_b328_5f07307d27ab
  b2fc2985_a7ba_9865_c2a3_2a7531f27d44["eachInstructionValueOperand()"]
  58861cab_871f_3d90_099d_42afe0e9f6b3 -->|calls| b2fc2985_a7ba_9865_c2a3_2a7531f27d44
  41232a25_deb6_6e83_05a8_ae9f961656f7["eachTerminalOperand()"]
  58861cab_871f_3d90_099d_42afe0e9f6b3 -->|calls| 41232a25_deb6_6e83_05a8_ae9f961656f7
  531eb985_e192_f9a2_2d7b_5deeb85ba95c["asResult()"]
  58861cab_871f_3d90_099d_42afe0e9f6b3 -->|calls| 531eb985_e192_f9a2_2d7b_5deeb85ba95c
  8e8b7ee8_d3c2_f98d_17e1_c5ff5fff1940["map()"]
  58861cab_871f_3d90_099d_42afe0e9f6b3 -->|calls| 8e8b7ee8_d3c2_f98d_17e1_c5ff5fff1940
  style 58861cab_871f_3d90_099d_42afe0e9f6b3 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoFreezingKnownMutableFunctions.ts lines 46–166

export function validateNoFreezingKnownMutableFunctions(
  fn: HIRFunction,
): Result<void, CompilerError> {
  const errors = new CompilerError();
  const contextMutationEffects: Map<
    IdentifierId,
    Extract<AliasingEffect, {kind: 'Mutate'} | {kind: 'MutateTransitive'}>
  > = new Map();

  function visitOperand(operand: Place): void {
    if (operand.effect === Effect.Freeze) {
      const effect = contextMutationEffects.get(operand.identifier.id);
      if (effect != null) {
        const place = effect.value;
        const variable =
          place != null &&
          place.identifier.name != null &&
          place.identifier.name.kind === 'named'
            ? `\`${place.identifier.name.value}\``
            : 'a local variable';
        errors.pushDiagnostic(
          CompilerDiagnostic.create({
            category: ErrorCategory.Immutability,
            reason: 'Cannot modify local variables after render completes',
            description: `This argument is a function which may reassign or mutate ${variable} after render, which can cause inconsistent behavior on subsequent renders. Consider using state instead`,
          })
            .withDetails({
              kind: 'error',
              loc: operand.loc,
              message: `This function may (indirectly) reassign or modify ${variable} after render`,
            })
            .withDetails({
              kind: 'error',
              loc: effect.value.loc,
              message: `This modifies ${variable}`,
            }),
        );
      }
    }
  }

  for (const block of fn.body.blocks.values()) {
    for (const instr of block.instructions) {
      const {lvalue, value} = instr;
      switch (value.kind) {
        case 'LoadLocal': {
          const effect = contextMutationEffects.get(value.place.identifier.id);
          if (effect != null) {
            contextMutationEffects.set(lvalue.identifier.id, effect);
          }
          break;
        }
        case 'StoreLocal': {
          const effect = contextMutationEffects.get(value.value.identifier.id);
          if (effect != null) {
            contextMutationEffects.set(lvalue.identifier.id, effect);
            contextMutationEffects.set(
              value.lvalue.place.identifier.id,
              effect,
            );
          }
          break;
        }
        case 'FunctionExpression': {
          if (value.loweredFunc.func.aliasingEffects != null) {
            const context = new Set(
              value.loweredFunc.func.context.map(p => p.identifier.id),
            );
            effects: for (const effect of value.loweredFunc.func
              .aliasingEffects) {
              switch (effect.kind) {
                case 'Mutate':
                case 'MutateTransitive': {
                  const knownMutation = contextMutationEffects.get(
                    effect.value.identifier.id,
                  );
                  if (knownMutation != null) {
                    contextMutationEffects.set(
                      lvalue.identifier.id,
                      knownMutation,
                    );

Domain

Subdomains

Frequently Asked Questions

What does validateNoFreezingKnownMutableFunctions() do?
validateNoFreezingKnownMutableFunctions() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoFreezingKnownMutableFunctions.ts.
Where is validateNoFreezingKnownMutableFunctions() defined?
validateNoFreezingKnownMutableFunctions() is defined in compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoFreezingKnownMutableFunctions.ts at line 46.
What does validateNoFreezingKnownMutableFunctions() call?
validateNoFreezingKnownMutableFunctions() calls 7 function(s): asResult, create, eachInstructionValueOperand, eachTerminalOperand, map, pushDiagnostic, withDetails.
What calls validateNoFreezingKnownMutableFunctions()?
validateNoFreezingKnownMutableFunctions() is called by 1 function(s): runWithEnvironment.

Analyze Your Own Codebase

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

Try Supermodel Free