Home / File/ ValidateMemoizedEffectDependencies.ts — react Source File

ValidateMemoizedEffectDependencies.ts — react Source File

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

File typescript BabelCompiler Validation 11 imports 1 dependents 3 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  4f3dd743_0a09_5d8d_bf24_d727c39bc26f["ValidateMemoizedEffectDependencies.ts"]
  e96f281e_f381_272d_2359_3e6a091c9a1d["CompilerError.ts"]
  4f3dd743_0a09_5d8d_bf24_d727c39bc26f --> e96f281e_f381_272d_2359_3e6a091c9a1d
  a2b91621_58d3_1d04_4663_00cd808f1034["ErrorCategory"]
  4f3dd743_0a09_5d8d_bf24_d727c39bc26f --> a2b91621_58d3_1d04_4663_00cd808f1034
  0423f759_97e0_9101_4634_ed555abc5ca9["index.ts"]
  4f3dd743_0a09_5d8d_bf24_d727c39bc26f --> 0423f759_97e0_9101_4634_ed555abc5ca9
  f041318d_301f_daad_4198_91d141b3039d["InferReactiveScopeVariables.ts"]
  4f3dd743_0a09_5d8d_bf24_d727c39bc26f --> f041318d_301f_daad_4198_91d141b3039d
  11746e9a_2fdf_98bb_bb3c_a63d8b200df2["isMutable"]
  4f3dd743_0a09_5d8d_bf24_d727c39bc26f --> 11746e9a_2fdf_98bb_bb3c_a63d8b200df2
  21609915_b03a_fd75_b58a_4cb86ef9315b["visitors.ts"]
  4f3dd743_0a09_5d8d_bf24_d727c39bc26f --> 21609915_b03a_fd75_b58a_4cb86ef9315b
  171a5d22_bb6b_1c99_05a4_6ad897438a35["ReactiveFunctionVisitor"]
  4f3dd743_0a09_5d8d_bf24_d727c39bc26f --> 171a5d22_bb6b_1c99_05a4_6ad897438a35
  2435b5f8_41a6_0458_ba88_4479b965455f["visitReactiveFunction"]
  4f3dd743_0a09_5d8d_bf24_d727c39bc26f --> 2435b5f8_41a6_0458_ba88_4479b965455f
  494e3425_0b47_293a_1ea4_d4670b0fc0e7["Result.ts"]
  4f3dd743_0a09_5d8d_bf24_d727c39bc26f --> 494e3425_0b47_293a_1ea4_d4670b0fc0e7
  7aace723_0ee1_cff5_b263_aec8e06dd79e["Result"]
  4f3dd743_0a09_5d8d_bf24_d727c39bc26f --> 7aace723_0ee1_cff5_b263_aec8e06dd79e
  2ed45bcd_6c82_3ccd_0e20_fa96b5111055[".."]
  4f3dd743_0a09_5d8d_bf24_d727c39bc26f --> 2ed45bcd_6c82_3ccd_0e20_fa96b5111055
  fe7a7397_dddc_7222_20d4_d5b1015466f1["ValidateExhaustiveDependencies.ts"]
  fe7a7397_dddc_7222_20d4_d5b1015466f1 --> 4f3dd743_0a09_5d8d_bf24_d727c39bc26f
  style 4f3dd743_0a09_5d8d_bf24_d727c39bc26f 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} from '..';
import {ErrorCategory} from '../CompilerError';
import {
  Identifier,
  Instruction,
  ReactiveFunction,
  ReactiveInstruction,
  ReactiveScopeBlock,
  ScopeId,
  isUseEffectHookType,
  isUseInsertionEffectHookType,
  isUseLayoutEffectHookType,
} from '../HIR';
import {isMutable} from '../ReactiveScopes/InferReactiveScopeVariables';
import {
  ReactiveFunctionVisitor,
  visitReactiveFunction,
} from '../ReactiveScopes/visitors';
import {Result} from '../Utils/Result';

/**
 * Validates that all known effect dependencies are memoized. The algorithm checks two things:
 * - Disallow effect dependencies that should be memoized (have a reactive scope assigned) but
 *   where that reactive scope does not exist. This checks for cases where a reactive scope was
 *   pruned for some reason, such as spanning a hook.
 * - Disallow effect dependencies whose a mutable range that encompasses the effect call.
 *
 * This latter check corresponds to any values which Forget knows may be mutable and may be mutated
 * after the effect. Note that it's possible Forget may miss not memoize a value for some other reason,
 * but in general this is a bug. The only reason Forget would _choose_ to skip memoization of an
 * effect dependency is because it's mutated later.
 *
 * Example:
 *
 * ```javascript
 * const object = {}; // mutable range starts here...
 *
 * useEffect(() => {
 *   console.log('hello');
 * }, [object]); // the dependency array picks up the mutable range of its mutable contents
 *
 * mutate(object); // ... mutable range ends here after this mutation
 * ```
 */
export function validateMemoizedEffectDependencies(
  fn: ReactiveFunction,
): Result<void, CompilerError> {
  const errors = new CompilerError();
  visitReactiveFunction(fn, new Visitor(), errors);
  return errors.asResult();
}

class Visitor extends ReactiveFunctionVisitor<CompilerError> {
// ... (75 more lines)

Domain

Subdomains

Classes

Frequently Asked Questions

What does ValidateMemoizedEffectDependencies.ts do?
ValidateMemoizedEffectDependencies.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 ValidateMemoizedEffectDependencies.ts?
ValidateMemoizedEffectDependencies.ts defines 3 function(s): isEffectHook, isUnmemoized, validateMemoizedEffectDependencies.
What does ValidateMemoizedEffectDependencies.ts depend on?
ValidateMemoizedEffectDependencies.ts imports 11 module(s): .., CompilerError.ts, ErrorCategory, InferReactiveScopeVariables.ts, ReactiveFunctionVisitor, Result, Result.ts, index.ts, and 3 more.
What files import ValidateMemoizedEffectDependencies.ts?
ValidateMemoizedEffectDependencies.ts is imported by 1 file(s): ValidateExhaustiveDependencies.ts.
Where is ValidateMemoizedEffectDependencies.ts in the architecture?
ValidateMemoizedEffectDependencies.ts is located at compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateMemoizedEffectDependencies.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