Home / Function/ isInSafeTypeofBlock() — react Function Reference

isInSafeTypeofBlock() — react Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

scripts/eslint-rules/safe-string-coercion.js lines 144–176

function isInSafeTypeofBlock(originalValueNode, node) {
  if (!node) {
    node = originalValueNode;
  }
  let parent = node.parent;
  while (parent) {
    if (!parent) {
      return false;
    }
    // Normally, if the parent block is inside a type-safe `if` statement,
    // then all child code is also type-safe. But there's a quirky case we
    // need to defend against:
    //   if (typeof obj === 'string') { } else if (typeof obj === 'object') {'' + obj}
    //   if (typeof obj === 'string') { } else {'' + obj}
    // In that code above, the `if` block is safe, but the `else` block is
    // unsafe and should report. But the AST parent of the `else` clause is the
    // `if` statement. This is the one case where the parent doesn't confer
    // safety onto the child. The code below identifies that case and keeps
    // moving up the tree until we get out of the `else`'s parent `if` block.
    // This ensures that we don't use any of these "parents" (really siblings)
    // to confer safety onto the current node.
    if (
      parent.type === 'IfStatement' &&
      !isDescendant(originalValueNode, parent.alternate)
    ) {
      const test = parent.test;
      if (isSafeTypeofExpression(originalValueNode, test)) {
        return true;
      }
    }
    parent = parent.parent;
  }
}

Domain

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free