Home / File/ ValidateLocalsNotReassignedAfterRender.ts — react Source File

ValidateLocalsNotReassignedAfterRender.ts — react Source File

Architecture documentation for ValidateLocalsNotReassignedAfterRender.ts, a typescript file in the react codebase. 10 imports, 1 dependents.

File typescript BabelCompiler Validation 10 imports 1 dependents 2 functions

Entity Profile

Dependency Diagram

graph LR
  5b745296_c60b_00ed_f4d9_4badd89a2f2b["ValidateLocalsNotReassignedAfterRender.ts"]
  e96f281e_f381_272d_2359_3e6a091c9a1d["CompilerError.ts"]
  5b745296_c60b_00ed_f4d9_4badd89a2f2b --> e96f281e_f381_272d_2359_3e6a091c9a1d
  a2b91621_58d3_1d04_4663_00cd808f1034["ErrorCategory"]
  5b745296_c60b_00ed_f4d9_4badd89a2f2b --> a2b91621_58d3_1d04_4663_00cd808f1034
  0423f759_97e0_9101_4634_ed555abc5ca9["index.ts"]
  5b745296_c60b_00ed_f4d9_4badd89a2f2b --> 0423f759_97e0_9101_4634_ed555abc5ca9
  2f3caf55_cc64_415c_55dd_9771ba7dc210["visitors.ts"]
  5b745296_c60b_00ed_f4d9_4badd89a2f2b --> 2f3caf55_cc64_415c_55dd_9771ba7dc210
  10043bf1_f7ee_9ed9_307a_fe3edfd02b09["eachInstructionLValue"]
  5b745296_c60b_00ed_f4d9_4badd89a2f2b --> 10043bf1_f7ee_9ed9_307a_fe3edfd02b09
  b2fc2985_a7ba_9865_c2a3_2a7531f27d44["eachInstructionValueOperand"]
  5b745296_c60b_00ed_f4d9_4badd89a2f2b --> b2fc2985_a7ba_9865_c2a3_2a7531f27d44
  41232a25_deb6_6e83_05a8_ae9f961656f7["eachTerminalOperand"]
  5b745296_c60b_00ed_f4d9_4badd89a2f2b --> 41232a25_deb6_6e83_05a8_ae9f961656f7
  d24875c3_c045_4414_2cc9_16f96d59c629["InferMutationAliasingEffects.ts"]
  5b745296_c60b_00ed_f4d9_4badd89a2f2b --> d24875c3_c045_4414_2cc9_16f96d59c629
  d2c89465_144e_76da_505e_4a2de182f6f4["getFunctionCallSignature"]
  5b745296_c60b_00ed_f4d9_4badd89a2f2b --> d2c89465_144e_76da_505e_4a2de182f6f4
  2ed45bcd_6c82_3ccd_0e20_fa96b5111055[".."]
  5b745296_c60b_00ed_f4d9_4badd89a2f2b --> 2ed45bcd_6c82_3ccd_0e20_fa96b5111055
  e3cfc07a_10c8_5dcd_e270_e8e14c29309b["Pipeline.ts"]
  e3cfc07a_10c8_5dcd_e270_e8e14c29309b --> 5b745296_c60b_00ed_f4d9_4badd89a2f2b
  style 5b745296_c60b_00ed_f4d9_4badd89a2f2b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import {CompilerDiagnostic, CompilerError, Effect} from '..';
import {ErrorCategory} from '../CompilerError';
import {HIRFunction, IdentifierId, Place} from '../HIR';
import {
  eachInstructionLValue,
  eachInstructionValueOperand,
  eachTerminalOperand,
} from '../HIR/visitors';
import {getFunctionCallSignature} from '../Inference/InferMutationAliasingEffects';

/**
 * Validates that local variables cannot be reassigned after render.
 * This prevents a category of bugs in which a closure captures a
 * binding from one render but does not update
 */
export function validateLocalsNotReassignedAfterRender(fn: HIRFunction): void {
  const contextVariables = new Set<IdentifierId>();
  const reassignment = getContextReassignment(
    fn,
    contextVariables,
    false,
    false,
  );
  if (reassignment !== null) {
    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 after render completes',
        description: `Reassigning ${variable} after render has completed can cause inconsistent behavior on subsequent renders. Consider using state instead`,
      }).withDetails({
        kind: 'error',
        loc: reassignment.loc,
        message: `Cannot reassign ${variable} after render completes`,
      }),
    );
    throw errors;
  }
}

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) {
// ... (170 more lines)

Domain

Subdomains

Frequently Asked Questions

What does ValidateLocalsNotReassignedAfterRender.ts do?
ValidateLocalsNotReassignedAfterRender.ts is a source file in the react codebase, written in typescript. It belongs to the BabelCompiler domain, Validation subdomain.
What functions are defined in ValidateLocalsNotReassignedAfterRender.ts?
ValidateLocalsNotReassignedAfterRender.ts defines 2 function(s): getContextReassignment, validateLocalsNotReassignedAfterRender.
What does ValidateLocalsNotReassignedAfterRender.ts depend on?
ValidateLocalsNotReassignedAfterRender.ts imports 10 module(s): .., CompilerError.ts, ErrorCategory, InferMutationAliasingEffects.ts, eachInstructionLValue, eachInstructionValueOperand, eachTerminalOperand, getFunctionCallSignature, and 2 more.
What files import ValidateLocalsNotReassignedAfterRender.ts?
ValidateLocalsNotReassignedAfterRender.ts is imported by 1 file(s): Pipeline.ts.
Where is ValidateLocalsNotReassignedAfterRender.ts in the architecture?
ValidateLocalsNotReassignedAfterRender.ts is located at compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateLocalsNotReassignedAfterRender.ts (domain: BabelCompiler, subdomain: Validation, directory: compiler/packages/babel-plugin-react-compiler/src/Validation).

Analyze Your Own Codebase

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

Try Supermodel Free