Home / Function/ validateNoCapitalizedCalls() — react Function Reference

validateNoCapitalizedCalls() — react Function Reference

Architecture documentation for the validateNoCapitalizedCalls() function in ValidateNoCapitalizedCalls.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  4f6e0671_34eb_6836_c392_26dfbebca40d["validateNoCapitalizedCalls()"]
  d0da0e57_4f62_7e0c_271a_2fd36446d684["ValidateNoCapitalizedCalls.ts"]
  4f6e0671_34eb_6836_c392_26dfbebca40d -->|defined in| d0da0e57_4f62_7e0c_271a_2fd36446d684
  73346565_783f_1426_1d29_3d461dabc0a1["throwInvalidReact()"]
  4f6e0671_34eb_6836_c392_26dfbebca40d -->|calls| 73346565_783f_1426_1d29_3d461dabc0a1
  073c81a5_c389_d108_5b8f_4d6dc6eece83["push()"]
  4f6e0671_34eb_6836_c392_26dfbebca40d -->|calls| 073c81a5_c389_d108_5b8f_4d6dc6eece83
  531eb985_e192_f9a2_2d7b_5deeb85ba95c["asResult()"]
  4f6e0671_34eb_6836_c392_26dfbebca40d -->|calls| 531eb985_e192_f9a2_2d7b_5deeb85ba95c
  style 4f6e0671_34eb_6836_c392_26dfbebca40d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoCapitalizedCalls.ts lines 14–97

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;
        }
      }
    }

Domain

Subdomains

Frequently Asked Questions

What does validateNoCapitalizedCalls() do?
validateNoCapitalizedCalls() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoCapitalizedCalls.ts.
Where is validateNoCapitalizedCalls() defined?
validateNoCapitalizedCalls() is defined in compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoCapitalizedCalls.ts at line 14.
What does validateNoCapitalizedCalls() call?
validateNoCapitalizedCalls() calls 3 function(s): asResult, push, throwInvalidReact.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free