Home / File/ ValidateNoCapitalizedCalls.ts — react Source File

ValidateNoCapitalizedCalls.ts — react Source File

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

File typescript BabelCompiler Validation 7 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  d0da0e57_4f62_7e0c_271a_2fd36446d684["ValidateNoCapitalizedCalls.ts"]
  e96f281e_f381_272d_2359_3e6a091c9a1d["CompilerError.ts"]
  d0da0e57_4f62_7e0c_271a_2fd36446d684 --> e96f281e_f381_272d_2359_3e6a091c9a1d
  a2b91621_58d3_1d04_4663_00cd808f1034["ErrorCategory"]
  d0da0e57_4f62_7e0c_271a_2fd36446d684 --> a2b91621_58d3_1d04_4663_00cd808f1034
  0423f759_97e0_9101_4634_ed555abc5ca9["index.ts"]
  d0da0e57_4f62_7e0c_271a_2fd36446d684 --> 0423f759_97e0_9101_4634_ed555abc5ca9
  38c44267_cdd2_9815_ebad_fa6761ba5934["Globals.ts"]
  d0da0e57_4f62_7e0c_271a_2fd36446d684 --> 38c44267_cdd2_9815_ebad_fa6761ba5934
  494e3425_0b47_293a_1ea4_d4670b0fc0e7["Result.ts"]
  d0da0e57_4f62_7e0c_271a_2fd36446d684 --> 494e3425_0b47_293a_1ea4_d4670b0fc0e7
  7aace723_0ee1_cff5_b263_aec8e06dd79e["Result"]
  d0da0e57_4f62_7e0c_271a_2fd36446d684 --> 7aace723_0ee1_cff5_b263_aec8e06dd79e
  2ed45bcd_6c82_3ccd_0e20_fa96b5111055[".."]
  d0da0e57_4f62_7e0c_271a_2fd36446d684 --> 2ed45bcd_6c82_3ccd_0e20_fa96b5111055
  style d0da0e57_4f62_7e0c_271a_2fd36446d684 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, EnvironmentConfig} from '..';
import {ErrorCategory} from '../CompilerError';
import {HIRFunction, IdentifierId} from '../HIR';
import {DEFAULT_GLOBALS} from '../HIR/Globals';
import {Result} from '../Utils/Result';

export function validateNoCapitalizedCalls(
  fn: HIRFunction,
): Result<void, CompilerError> {
  const envConfig: EnvironmentConfig = fn.env.config;
  const ALLOW_LIST = new Set([
    ...DEFAULT_GLOBALS.keys(),
    ...(envConfig.validateNoCapitalizedCalls ?? []),
  ]);
  /*
   * The hook pattern may allow uppercase names, like React$useState, so we need to be sure that we
   * do not error in those cases
   */
  const hookPattern =
    envConfig.hookPattern != null ? new RegExp(envConfig.hookPattern) : null;
  const isAllowed = (name: string): boolean => {
    return (
      ALLOW_LIST.has(name) || (hookPattern != null && hookPattern.test(name))
    );
  };

  const errors = new CompilerError();
  const capitalLoadGlobals = new Map<IdentifierId, string>();
  const capitalizedProperties = new Map<IdentifierId, string>();
  const reason =
    'Capitalized functions are reserved for components, which must be invoked with JSX. If this is a component, render it with JSX. Otherwise, ensure that it has no hook calls and rename it to begin with a lowercase letter. Alternatively, if you know for a fact that this function is not a component, you can allowlist it via the compiler config';
  for (const [, block] of fn.body.blocks) {
    for (const {lvalue, value} of block.instructions) {
      switch (value.kind) {
        case 'LoadGlobal': {
          if (
            value.binding.name != '' &&
            /^[A-Z]/.test(value.binding.name) &&
            // We don't want to flag CONSTANTS()
            !(value.binding.name.toUpperCase() === value.binding.name) &&
            !isAllowed(value.binding.name)
          ) {
            capitalLoadGlobals.set(lvalue.identifier.id, value.binding.name);
          }

          break;
        }
        case 'CallExpression': {
          const calleeIdentifier = value.callee.identifier.id;
          const calleeName = capitalLoadGlobals.get(calleeIdentifier);
          if (calleeName != null) {
            CompilerError.throwInvalidReact({
              category: ErrorCategory.CapitalizedCalls,
              reason,
              description: `${calleeName} may be a component`,
              loc: value.loc,
              suggestions: null,
            });
          }
          break;
        }
        case 'PropertyLoad': {
          // Start conservative and disallow all capitalized method calls
          if (
            typeof value.property === 'string' &&
            /^[A-Z]/.test(value.property)
          ) {
            capitalizedProperties.set(lvalue.identifier.id, value.property);
          }
          break;
        }
        case 'MethodCall': {
          const propertyIdentifier = value.property.identifier.id;
          const propertyName = capitalizedProperties.get(propertyIdentifier);
          if (propertyName != null) {
            errors.push({
              category: ErrorCategory.CapitalizedCalls,
              reason,
              description: `${propertyName} may be a component`,
              loc: value.loc,
              suggestions: null,
            });
          }
          break;
        }
      }
    }
  }
  return errors.asResult();
}

Domain

Subdomains

Frequently Asked Questions

What does ValidateNoCapitalizedCalls.ts do?
ValidateNoCapitalizedCalls.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 ValidateNoCapitalizedCalls.ts?
ValidateNoCapitalizedCalls.ts defines 1 function(s): validateNoCapitalizedCalls.
What does ValidateNoCapitalizedCalls.ts depend on?
ValidateNoCapitalizedCalls.ts imports 7 module(s): .., CompilerError.ts, ErrorCategory, Globals.ts, Result, Result.ts, index.ts.
Where is ValidateNoCapitalizedCalls.ts in the architecture?
ValidateNoCapitalizedCalls.ts is located at compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoCapitalizedCalls.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