Home / File/ ValidateNoDerivedComputationsInEffects.ts — react Source File

ValidateNoDerivedComputationsInEffects.ts — react Source File

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

File typescript BabelCompiler Validation 7 imports 1 dependents 2 functions

Entity Profile

Dependency Diagram

graph LR
  a3af5b5e_3d98_91f8_296b_f84c4c65a51c["ValidateNoDerivedComputationsInEffects.ts"]
  e96f281e_f381_272d_2359_3e6a091c9a1d["CompilerError.ts"]
  a3af5b5e_3d98_91f8_296b_f84c4c65a51c --> e96f281e_f381_272d_2359_3e6a091c9a1d
  a2b91621_58d3_1d04_4663_00cd808f1034["ErrorCategory"]
  a3af5b5e_3d98_91f8_296b_f84c4c65a51c --> a2b91621_58d3_1d04_4663_00cd808f1034
  0423f759_97e0_9101_4634_ed555abc5ca9["index.ts"]
  a3af5b5e_3d98_91f8_296b_f84c4c65a51c --> 0423f759_97e0_9101_4634_ed555abc5ca9
  2f3caf55_cc64_415c_55dd_9771ba7dc210["visitors.ts"]
  a3af5b5e_3d98_91f8_296b_f84c4c65a51c --> 2f3caf55_cc64_415c_55dd_9771ba7dc210
  b2fc2985_a7ba_9865_c2a3_2a7531f27d44["eachInstructionValueOperand"]
  a3af5b5e_3d98_91f8_296b_f84c4c65a51c --> b2fc2985_a7ba_9865_c2a3_2a7531f27d44
  41232a25_deb6_6e83_05a8_ae9f961656f7["eachTerminalOperand"]
  a3af5b5e_3d98_91f8_296b_f84c4c65a51c --> 41232a25_deb6_6e83_05a8_ae9f961656f7
  2ed45bcd_6c82_3ccd_0e20_fa96b5111055[".."]
  a3af5b5e_3d98_91f8_296b_f84c4c65a51c --> 2ed45bcd_6c82_3ccd_0e20_fa96b5111055
  e3cfc07a_10c8_5dcd_e270_e8e14c29309b["Pipeline.ts"]
  e3cfc07a_10c8_5dcd_e270_e8e14c29309b --> a3af5b5e_3d98_91f8_296b_f84c4c65a51c
  style a3af5b5e_3d98_91f8_296b_f84c4c65a51c 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 {CompilerError, SourceLocation} from '..';
import {ErrorCategory} from '../CompilerError';
import {
  ArrayExpression,
  BlockId,
  FunctionExpression,
  HIRFunction,
  IdentifierId,
  isSetStateType,
  isUseEffectHookType,
} from '../HIR';
import {
  eachInstructionValueOperand,
  eachTerminalOperand,
} from '../HIR/visitors';

/**
 * Validates that useEffect is not used for derived computations which could/should
 * be performed in render.
 *
 * See https://react.dev/learn/you-might-not-need-an-effect#updating-state-based-on-props-or-state
 *
 * Example:
 *
 * ```
 * // 🔴 Avoid: redundant state and unnecessary Effect
 * const [fullName, setFullName] = useState('');
 * useEffect(() => {
 *   setFullName(firstName + ' ' + lastName);
 * }, [firstName, lastName]);
 * ```
 *
 * Instead use:
 *
 * ```
 * // ✅ Good: calculated during rendering
 * const fullName = firstName + ' ' + lastName;
 * ```
 */
export function validateNoDerivedComputationsInEffects(fn: HIRFunction): void {
  const candidateDependencies: Map<IdentifierId, ArrayExpression> = new Map();
  const functions: Map<IdentifierId, FunctionExpression> = new Map();
  const locals: Map<IdentifierId, IdentifierId> = new Map();

  const errors = new CompilerError();

  for (const block of fn.body.blocks.values()) {
    for (const instr of block.instructions) {
      const {lvalue, value} = instr;
      if (value.kind === 'LoadLocal') {
        locals.set(lvalue.identifier.id, value.place.identifier.id);
      } else if (value.kind === 'ArrayExpression') {
        candidateDependencies.set(lvalue.identifier.id, value);
// ... (172 more lines)

Domain

Subdomains

Frequently Asked Questions

What does ValidateNoDerivedComputationsInEffects.ts do?
ValidateNoDerivedComputationsInEffects.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 ValidateNoDerivedComputationsInEffects.ts?
ValidateNoDerivedComputationsInEffects.ts defines 2 function(s): validateEffect, validateNoDerivedComputationsInEffects.
What does ValidateNoDerivedComputationsInEffects.ts depend on?
ValidateNoDerivedComputationsInEffects.ts imports 7 module(s): .., CompilerError.ts, ErrorCategory, eachInstructionValueOperand, eachTerminalOperand, index.ts, visitors.ts.
What files import ValidateNoDerivedComputationsInEffects.ts?
ValidateNoDerivedComputationsInEffects.ts is imported by 1 file(s): Pipeline.ts.
Where is ValidateNoDerivedComputationsInEffects.ts in the architecture?
ValidateNoDerivedComputationsInEffects.ts is located at compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoDerivedComputationsInEffects.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