Home / File/ ValidateStaticComponents.ts — react Source File

ValidateStaticComponents.ts — react Source File

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

File typescript BabelCompiler Validation 7 imports 1 dependents 1 functions

Entity Profile

Dependency Diagram

graph LR
  65f4975b_1de3_0e31_4cd0_c510c19d5da5["ValidateStaticComponents.ts"]
  e96f281e_f381_272d_2359_3e6a091c9a1d["CompilerError.ts"]
  65f4975b_1de3_0e31_4cd0_c510c19d5da5 --> e96f281e_f381_272d_2359_3e6a091c9a1d
  0fda7f86_b7a3_c1f2_f0d9_8d13eed4f042["CompilerDiagnostic"]
  65f4975b_1de3_0e31_4cd0_c510c19d5da5 --> 0fda7f86_b7a3_c1f2_f0d9_8d13eed4f042
  e51fd0d2_bb38_cc97_7763_efe37f300a47["CompilerError"]
  65f4975b_1de3_0e31_4cd0_c510c19d5da5 --> e51fd0d2_bb38_cc97_7763_efe37f300a47
  a2b91621_58d3_1d04_4663_00cd808f1034["ErrorCategory"]
  65f4975b_1de3_0e31_4cd0_c510c19d5da5 --> a2b91621_58d3_1d04_4663_00cd808f1034
  0423f759_97e0_9101_4634_ed555abc5ca9["index.ts"]
  65f4975b_1de3_0e31_4cd0_c510c19d5da5 --> 0423f759_97e0_9101_4634_ed555abc5ca9
  494e3425_0b47_293a_1ea4_d4670b0fc0e7["Result.ts"]
  65f4975b_1de3_0e31_4cd0_c510c19d5da5 --> 494e3425_0b47_293a_1ea4_d4670b0fc0e7
  7aace723_0ee1_cff5_b263_aec8e06dd79e["Result"]
  65f4975b_1de3_0e31_4cd0_c510c19d5da5 --> 7aace723_0ee1_cff5_b263_aec8e06dd79e
  e3cfc07a_10c8_5dcd_e270_e8e14c29309b["Pipeline.ts"]
  e3cfc07a_10c8_5dcd_e270_e8e14c29309b --> 65f4975b_1de3_0e31_4cd0_c510c19d5da5
  style 65f4975b_1de3_0e31_4cd0_c510c19d5da5 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, SourceLocation} from '../HIR';
import {Result} from '../Utils/Result';

/**
 * Validates against components that are created dynamically and whose identity is not guaranteed
 * to be stable (which would cause the component to reset on each re-render).
 */
export function validateStaticComponents(
  fn: HIRFunction,
): Result<void, CompilerError> {
  const error = new CompilerError();
  const knownDynamicComponents = new Map<IdentifierId, SourceLocation>();
  for (const block of fn.body.blocks.values()) {
    phis: for (const phi of block.phis) {
      for (const operand of phi.operands.values()) {
        const loc = knownDynamicComponents.get(operand.identifier.id);
        if (loc != null) {
          knownDynamicComponents.set(phi.place.identifier.id, loc);
          continue phis;
        }
      }
    }
    for (const instr of block.instructions) {
      const {lvalue, value} = instr;
      switch (value.kind) {
        case 'FunctionExpression':
        case 'NewExpression':
        case 'MethodCall':
        case 'CallExpression': {
          knownDynamicComponents.set(lvalue.identifier.id, value.loc);
          break;
        }
        case 'LoadLocal': {
          const loc = knownDynamicComponents.get(value.place.identifier.id);
          if (loc != null) {
            knownDynamicComponents.set(lvalue.identifier.id, loc);
          }
          break;
        }
        case 'StoreLocal': {
          const loc = knownDynamicComponents.get(value.value.identifier.id);
          if (loc != null) {
            knownDynamicComponents.set(lvalue.identifier.id, loc);
            knownDynamicComponents.set(value.lvalue.place.identifier.id, loc);
          }
          break;
        }
        case 'JsxExpression': {
          if (value.tag.kind === 'Identifier') {
            const location = knownDynamicComponents.get(
              value.tag.identifier.id,
            );
            if (location != null) {
              error.pushDiagnostic(
                CompilerDiagnostic.create({
                  category: ErrorCategory.StaticComponents,
                  reason: 'Cannot create components during render',
                  description: `Components created during render will reset their state each time they are created. Declare components outside of render`,
                })
                  .withDetails({
                    kind: 'error',
                    loc: value.tag.loc,
                    message: 'This component is created during render',
                  })
                  .withDetails({
                    kind: 'error',
                    loc: location,
                    message: 'The component is created during render here',
                  }),
              );
            }
          }
        }
      }
    }
  }
  return error.asResult();
}

Domain

Subdomains

Frequently Asked Questions

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