Home / File/ ValidateNoSetStateInRender.ts — react Source File

ValidateNoSetStateInRender.ts — react Source File

Architecture documentation for ValidateNoSetStateInRender.ts, a typescript file in the react codebase. 11 imports, 0 dependents.

File typescript BabelCompiler Validation 11 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  b4e382c8_ed3f_36e3_497f_a485571ec5ea["ValidateNoSetStateInRender.ts"]
  e96f281e_f381_272d_2359_3e6a091c9a1d["CompilerError.ts"]
  b4e382c8_ed3f_36e3_497f_a485571ec5ea --> e96f281e_f381_272d_2359_3e6a091c9a1d
  0fda7f86_b7a3_c1f2_f0d9_8d13eed4f042["CompilerDiagnostic"]
  b4e382c8_ed3f_36e3_497f_a485571ec5ea --> 0fda7f86_b7a3_c1f2_f0d9_8d13eed4f042
  e51fd0d2_bb38_cc97_7763_efe37f300a47["CompilerError"]
  b4e382c8_ed3f_36e3_497f_a485571ec5ea --> e51fd0d2_bb38_cc97_7763_efe37f300a47
  a2b91621_58d3_1d04_4663_00cd808f1034["ErrorCategory"]
  b4e382c8_ed3f_36e3_497f_a485571ec5ea --> a2b91621_58d3_1d04_4663_00cd808f1034
  0423f759_97e0_9101_4634_ed555abc5ca9["index.ts"]
  b4e382c8_ed3f_36e3_497f_a485571ec5ea --> 0423f759_97e0_9101_4634_ed555abc5ca9
  c0d1dc03_8683_01ed_b02a_b10aae366514["ComputeUnconditionalBlocks.ts"]
  b4e382c8_ed3f_36e3_497f_a485571ec5ea --> c0d1dc03_8683_01ed_b02a_b10aae366514
  cc92da6a_36c2_147e_624d_3b9a7d1999b0["computeUnconditionalBlocks"]
  b4e382c8_ed3f_36e3_497f_a485571ec5ea --> cc92da6a_36c2_147e_624d_3b9a7d1999b0
  2f3caf55_cc64_415c_55dd_9771ba7dc210["visitors.ts"]
  b4e382c8_ed3f_36e3_497f_a485571ec5ea --> 2f3caf55_cc64_415c_55dd_9771ba7dc210
  b2fc2985_a7ba_9865_c2a3_2a7531f27d44["eachInstructionValueOperand"]
  b4e382c8_ed3f_36e3_497f_a485571ec5ea --> b2fc2985_a7ba_9865_c2a3_2a7531f27d44
  494e3425_0b47_293a_1ea4_d4670b0fc0e7["Result.ts"]
  b4e382c8_ed3f_36e3_497f_a485571ec5ea --> 494e3425_0b47_293a_1ea4_d4670b0fc0e7
  7aace723_0ee1_cff5_b263_aec8e06dd79e["Result"]
  b4e382c8_ed3f_36e3_497f_a485571ec5ea --> 7aace723_0ee1_cff5_b263_aec8e06dd79e
  style b4e382c8_ed3f_36e3_497f_a485571ec5ea 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,
  ErrorCategory,
} from '../CompilerError';
import {HIRFunction, IdentifierId, isSetStateType} from '../HIR';
import {computeUnconditionalBlocks} from '../HIR/ComputeUnconditionalBlocks';
import {eachInstructionValueOperand} from '../HIR/visitors';
import {Result} from '../Utils/Result';

/**
 * Validates that the given function does not have an infinite update loop
 * caused by unconditionally calling setState during render. This validation
 * is conservative and cannot catch all cases of unconditional setState in
 * render, but avoids false positives. Examples of cases that are caught:
 *
 * ```javascript
 * // Direct call of setState:
 * const [state, setState] = useState(false);
 * setState(true);
 *
 * // Indirect via a function:
 * const [state, setState] = useState(false);
 * const setTrue = () => setState(true);
 * setTrue();
 * ```
 *
 * However, storing setState inside another value and accessing it is not yet
 * validated:
 *
 * ```
 * // false negative, not detected but will cause an infinite render loop
 * const [state, setState] = useState(false);
 * const x = [setState];
 * const y = x.pop();
 * y();
 * ```
 */
export function validateNoSetStateInRender(
  fn: HIRFunction,
): Result<void, CompilerError> {
  const unconditionalSetStateFunctions: Set<IdentifierId> = new Set();
  return validateNoSetStateInRenderImpl(fn, unconditionalSetStateFunctions);
}

function validateNoSetStateInRenderImpl(
  fn: HIRFunction,
  unconditionalSetStateFunctions: Set<IdentifierId>,
): Result<void, CompilerError> {
  const unconditionalBlocks = computeUnconditionalBlocks(fn);
  let activeManualMemoId: number | null = null;
  const errors = new CompilerError();
  for (const [, block] of fn.body.blocks) {
// ... (128 more lines)

Domain

Subdomains

Frequently Asked Questions

What does ValidateNoSetStateInRender.ts do?
ValidateNoSetStateInRender.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 ValidateNoSetStateInRender.ts?
ValidateNoSetStateInRender.ts defines 2 function(s): validateNoSetStateInRender, validateNoSetStateInRenderImpl.
What does ValidateNoSetStateInRender.ts depend on?
ValidateNoSetStateInRender.ts imports 11 module(s): CompilerDiagnostic, CompilerError, CompilerError.ts, ComputeUnconditionalBlocks.ts, ErrorCategory, Result, Result.ts, computeUnconditionalBlocks, and 3 more.
Where is ValidateNoSetStateInRender.ts in the architecture?
ValidateNoSetStateInRender.ts is located at compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoSetStateInRender.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