Home / File/ ValidateNoImpureFunctionsInRender.ts — react Source File

ValidateNoImpureFunctionsInRender.ts — react Source File

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

File typescript BabelCompiler Validation 8 imports 1 dependents 1 functions

Entity Profile

Dependency Diagram

graph LR
  350d48bf_14c4_ad84_872f_09fa76881478["ValidateNoImpureFunctionsInRender.ts"]
  e96f281e_f381_272d_2359_3e6a091c9a1d["CompilerError.ts"]
  350d48bf_14c4_ad84_872f_09fa76881478 --> e96f281e_f381_272d_2359_3e6a091c9a1d
  a2b91621_58d3_1d04_4663_00cd808f1034["ErrorCategory"]
  350d48bf_14c4_ad84_872f_09fa76881478 --> a2b91621_58d3_1d04_4663_00cd808f1034
  0423f759_97e0_9101_4634_ed555abc5ca9["index.ts"]
  350d48bf_14c4_ad84_872f_09fa76881478 --> 0423f759_97e0_9101_4634_ed555abc5ca9
  d24875c3_c045_4414_2cc9_16f96d59c629["InferMutationAliasingEffects.ts"]
  350d48bf_14c4_ad84_872f_09fa76881478 --> d24875c3_c045_4414_2cc9_16f96d59c629
  d2c89465_144e_76da_505e_4a2de182f6f4["getFunctionCallSignature"]
  350d48bf_14c4_ad84_872f_09fa76881478 --> d2c89465_144e_76da_505e_4a2de182f6f4
  494e3425_0b47_293a_1ea4_d4670b0fc0e7["Result.ts"]
  350d48bf_14c4_ad84_872f_09fa76881478 --> 494e3425_0b47_293a_1ea4_d4670b0fc0e7
  7aace723_0ee1_cff5_b263_aec8e06dd79e["Result"]
  350d48bf_14c4_ad84_872f_09fa76881478 --> 7aace723_0ee1_cff5_b263_aec8e06dd79e
  2ed45bcd_6c82_3ccd_0e20_fa96b5111055[".."]
  350d48bf_14c4_ad84_872f_09fa76881478 --> 2ed45bcd_6c82_3ccd_0e20_fa96b5111055
  e3cfc07a_10c8_5dcd_e270_e8e14c29309b["Pipeline.ts"]
  e3cfc07a_10c8_5dcd_e270_e8e14c29309b --> 350d48bf_14c4_ad84_872f_09fa76881478
  style 350d48bf_14c4_ad84_872f_09fa76881478 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} from '..';
import {ErrorCategory} from '../CompilerError';
import {HIRFunction} from '../HIR';
import {getFunctionCallSignature} from '../Inference/InferMutationAliasingEffects';
import {Result} from '../Utils/Result';

/**
 * Checks that known-impure functions are not called during render. Examples of invalid functions to
 * call during render are `Math.random()` and `Date.now()`. Users may extend this set of
 * impure functions via a module type provider and specifying functions with `impure: true`.
 *
 * TODO: add best-effort analysis of functions which are called during render. We have variations of
 * this in several of our validation passes and should unify those analyses into a reusable helper
 * and use it here.
 */
export function validateNoImpureFunctionsInRender(
  fn: HIRFunction,
): Result<void, CompilerError> {
  const errors = new CompilerError();
  for (const [, block] of fn.body.blocks) {
    for (const instr of block.instructions) {
      const value = instr.value;
      if (value.kind === 'MethodCall' || value.kind == 'CallExpression') {
        const callee =
          value.kind === 'MethodCall' ? value.property : value.callee;
        const signature = getFunctionCallSignature(
          fn.env,
          callee.identifier.type,
        );
        if (signature != null && signature.impure === true) {
          errors.pushDiagnostic(
            CompilerDiagnostic.create({
              category: ErrorCategory.Purity,
              reason: 'Cannot call impure function during render',
              description:
                (signature.canonicalName != null
                  ? `\`${signature.canonicalName}\` is an impure function. `
                  : '') +
                'Calling an impure function can produce unstable results that update unpredictably when the component happens to re-render. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#components-and-hooks-must-be-idempotent)',
              suggestions: null,
            }).withDetails({
              kind: 'error',
              loc: callee.loc,
              message: 'Cannot call impure function',
            }),
          );
        }
      }
    }
  }
  return errors.asResult();
}

Domain

Subdomains

Frequently Asked Questions

What does ValidateNoImpureFunctionsInRender.ts do?
ValidateNoImpureFunctionsInRender.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 ValidateNoImpureFunctionsInRender.ts?
ValidateNoImpureFunctionsInRender.ts defines 1 function(s): validateNoImpureFunctionsInRender.
What does ValidateNoImpureFunctionsInRender.ts depend on?
ValidateNoImpureFunctionsInRender.ts imports 8 module(s): .., CompilerError.ts, ErrorCategory, InferMutationAliasingEffects.ts, Result, Result.ts, getFunctionCallSignature, index.ts.
What files import ValidateNoImpureFunctionsInRender.ts?
ValidateNoImpureFunctionsInRender.ts is imported by 1 file(s): Pipeline.ts.
Where is ValidateNoImpureFunctionsInRender.ts in the architecture?
ValidateNoImpureFunctionsInRender.ts is located at compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoImpureFunctionsInRender.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