Home / Function/ isSafeTypeofExpression() — react Function Reference

isSafeTypeofExpression() — react Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  e479b5fd_1471_c173_d79b_ab0430063101["isSafeTypeofExpression()"]
  ff8e0a4b_26fd_d20a_1091_80a32dbd865a["safe-string-coercion.js"]
  e479b5fd_1471_c173_d79b_ab0430063101 -->|defined in| ff8e0a4b_26fd_d20a_1091_80a32dbd865a
  bb433535_26f9_2f73_b786_8db6fe70ef11["isInSafeTypeofBlock()"]
  bb433535_26f9_2f73_b786_8db6fe70ef11 -->|calls| e479b5fd_1471_c173_d79b_ab0430063101
  ce919cf3_ad88_d635_6f38_41d29648c681["isEquivalentCode()"]
  e479b5fd_1471_c173_d79b_ab0430063101 -->|calls| ce919cf3_ad88_d635_6f38_41d29648c681
  style e479b5fd_1471_c173_d79b_ab0430063101 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

scripts/eslint-rules/safe-string-coercion.js lines 92–130

function isSafeTypeofExpression(originalValueNode, node) {
  if (node.type === 'BinaryExpression') {
    // Example: typeof foo === 'string'
    if (node.operator !== '===') {
      return false;
    }
    const {left, right} = node;

    // left must be `typeof original`
    if (left.type !== 'UnaryExpression' || left.operator !== 'typeof') {
      return false;
    }
    if (!isEquivalentCode(left.argument, originalValueNode)) {
      return false;
    }
    // right must be a literal value of a safe type
    const safeTypes = ['string', 'number', 'boolean', 'undefined', 'bigint'];
    if (right.type !== 'Literal' || !safeTypes.includes(right.value)) {
      return false;
    }
    return true;
  } else if (node.type === 'LogicalExpression') {
    // Examples:
    // * typeof foo === 'string' && typeof foo === 'number
    // * typeof foo === 'string' && someOtherTest
    if (node.operator === '&&') {
      return (
        isSafeTypeofExpression(originalValueNode, node.left) ||
        isSafeTypeofExpression(originalValueNode, node.right)
      );
    } else if (node.operator === '||') {
      return (
        isSafeTypeofExpression(originalValueNode, node.left) &&
        isSafeTypeofExpression(originalValueNode, node.right)
      );
    }
  }
  return false;
}

Domain

Subdomains

Frequently Asked Questions

What does isSafeTypeofExpression() do?
isSafeTypeofExpression() is a function in the react codebase, defined in scripts/eslint-rules/safe-string-coercion.js.
Where is isSafeTypeofExpression() defined?
isSafeTypeofExpression() is defined in scripts/eslint-rules/safe-string-coercion.js at line 92.
What does isSafeTypeofExpression() call?
isSafeTypeofExpression() calls 1 function(s): isEquivalentCode.
What calls isSafeTypeofExpression()?
isSafeTypeofExpression() is called by 1 function(s): isInSafeTypeofBlock.

Analyze Your Own Codebase

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

Try Supermodel Free