Home / File/ CheckStringCoercion.js — react Source File

CheckStringCoercion.js — react Source File

Architecture documentation for CheckStringCoercion.js, a javascript file in the react codebase.

Entity Profile

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.
 *
 * @flow
 */

/*
 * The `'' + value` pattern (used in perf-sensitive code) throws for Symbol
 * and Temporal.* types. See https://github.com/facebook/react/pull/22064.
 *
 * The functions in this module will throw an easier-to-understand,
 * easier-to-debug exception with a clear errors message message explaining the
 * problem. (Instead of a confusing exception thrown inside the implementation
 * of the `value` object).
 */

// $FlowFixMe[incompatible-return] only called in DEV, so void return is not possible.
function typeName(value: mixed): string {
  if (__DEV__) {
    // toStringTag is needed for namespaced types like Temporal.Instant
    const hasToStringTag = typeof Symbol === 'function' && Symbol.toStringTag;
    const type =
      (hasToStringTag && (value: any)[Symbol.toStringTag]) ||
      (value: any).constructor.name ||
      'Object';
    // $FlowFixMe[incompatible-return]
    return type;
  }
}

// $FlowFixMe[incompatible-return] only called in DEV, so void return is not possible.
function willCoercionThrow(value: mixed): boolean {
  if (__DEV__) {
    try {
      testStringCoercion(value);
      return false;
    } catch (e) {
      return true;
    }
  }
}

/** @noinline */
function testStringCoercion(value: mixed) {
  // If you ended up here by following an exception call stack, here's what's
  // happened: you supplied an object or symbol value to React (as a prop, key,
  // DOM attribute, CSS property, string ref, etc.) and when React tried to
  // coerce it to a string using `'' + value`, an exception was thrown.
  //
  // The most common types that will cause this exception are `Symbol` instances
  // and Temporal objects like `Temporal.Instant`. But any object that has a
  // `valueOf` or `[Symbol.toPrimitive]` method that throws will also cause this
  // exception. (Library authors do this to prevent users from using built-in
  // numeric operators like `+` or comparison operators like `>=` because custom
  // methods are needed to perform accurate arithmetic or comparison.)
  //
  // To fix the problem, coerce this object or symbol value to a string before
// ... (121 more lines)

Domain

Subdomains

Frequently Asked Questions

What does CheckStringCoercion.js do?
CheckStringCoercion.js is a source file in the react codebase, written in javascript. It belongs to the BabelCompiler domain, Optimization subdomain.
What functions are defined in CheckStringCoercion.js?
CheckStringCoercion.js defines 10 function(s): checkAttributeStringCoercion, checkCSSPropertyStringCoercion, checkFormFieldValueStringCoercion, checkHtmlStringCoercion, checkKeyStringCoercion, checkOptionStringCoercion, checkPropStringCoercion, testStringCoercion, typeName, willCoercionThrow.
Where is CheckStringCoercion.js in the architecture?
CheckStringCoercion.js is located at packages/shared/CheckStringCoercion.js (domain: BabelCompiler, subdomain: Optimization, directory: packages/shared).

Analyze Your Own Codebase

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

Try Supermodel Free