Home / Function/ checkBinaryExpression() — react Function Reference

checkBinaryExpression() — react Function Reference

Architecture documentation for the checkBinaryExpression() function in safe-string-coercion.js from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  c98dc723_2588_d555_56ce_30caf79dca6f["checkBinaryExpression()"]
  ff8e0a4b_26fd_d20a_1091_80a32dbd865a["safe-string-coercion.js"]
  c98dc723_2588_d555_56ce_30caf79dca6f -->|defined in| ff8e0a4b_26fd_d20a_1091_80a32dbd865a
  265d5364_66e6_357e_6944_f9bb83cfb31a["isOnlyAddingStrings()"]
  c98dc723_2588_d555_56ce_30caf79dca6f -->|calls| 265d5364_66e6_357e_6944_f9bb83cfb31a
  1e269210_19f2_2623_9b64_fc7befe0e397["isEmptyLiteral()"]
  c98dc723_2588_d555_56ce_30caf79dca6f -->|calls| 1e269210_19f2_2623_9b64_fc7befe0e397
  bb433535_26f9_2f73_b786_8db6fe70ef11["isInSafeTypeofBlock()"]
  c98dc723_2588_d555_56ce_30caf79dca6f -->|calls| bb433535_26f9_2f73_b786_8db6fe70ef11
  320e2cfd_90b4_bf81_705a_4fdc5ef12ba6["hasCoercionCheck()"]
  c98dc723_2588_d555_56ce_30caf79dca6f -->|calls| 320e2cfd_90b4_bf81_705a_4fdc5ef12ba6
  style c98dc723_2588_d555_56ce_30caf79dca6f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

scripts/eslint-rules/safe-string-coercion.js lines 284–337

function checkBinaryExpression(context, node) {
  if (isOnlyAddingStrings(node)) {
    return;
  }

  if (
    node.operator === '+' &&
    (isEmptyLiteral(node.left) || isEmptyLiteral(node.right))
  ) {
    let valueToTest = isEmptyLiteral(node.left) ? node.right : node.left;
    if (
      (valueToTest.type === 'TypeCastExpression' ||
        valueToTest.type === 'AsExpression') &&
      valueToTest.expression
    ) {
      valueToTest = valueToTest.expression;
    }

    if (
      valueToTest.type === 'Identifier' &&
      ['i', 'idx', 'lineNumber'].includes(valueToTest.name)
    ) {
      // Common non-object variable names are assumed to be safe
      return;
    }
    if (
      valueToTest.type === 'UnaryExpression' ||
      valueToTest.type === 'UpdateExpression'
    ) {
      // Any unary expression will return a non-object, non-symbol type.
      return;
    }
    if (isInSafeTypeofBlock(valueToTest)) {
      // The value is inside an if (typeof...) block that ensures it's safe
      return;
    }
    const coercionCheckMessage = hasCoercionCheck(valueToTest);
    if (!coercionCheckMessage) {
      // The previous statement is a correct check function call, so no report.
      return;
    }

    context.report({
      node,
      message:
        coercionCheckMessage +
        '\n' +
        "Using `'' + value` or `value + ''` is fast to coerce strings, but may throw." +
        ' For prod code, add a DEV check from shared/CheckStringCoercion immediately' +
        ' before this coercion.' +
        ' For non-prod code and prod error handling, use `String(value)` instead.',
    });
  }
}

Domain

Subdomains

Frequently Asked Questions

What does checkBinaryExpression() do?
checkBinaryExpression() is a function in the react codebase, defined in scripts/eslint-rules/safe-string-coercion.js.
Where is checkBinaryExpression() defined?
checkBinaryExpression() is defined in scripts/eslint-rules/safe-string-coercion.js at line 284.
What does checkBinaryExpression() call?
checkBinaryExpression() calls 4 function(s): hasCoercionCheck, isEmptyLiteral, isInSafeTypeofBlock, isOnlyAddingStrings.

Analyze Your Own Codebase

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

Try Supermodel Free