Home / Function/ hasCoercionCheck() — react Function Reference

hasCoercionCheck() — react Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  320e2cfd_90b4_bf81_705a_4fdc5ef12ba6["hasCoercionCheck()"]
  ff8e0a4b_26fd_d20a_1091_80a32dbd865a["safe-string-coercion.js"]
  320e2cfd_90b4_bf81_705a_4fdc5ef12ba6 -->|defined in| ff8e0a4b_26fd_d20a_1091_80a32dbd865a
  c98dc723_2588_d555_56ce_30caf79dca6f["checkBinaryExpression()"]
  c98dc723_2588_d555_56ce_30caf79dca6f -->|calls| 320e2cfd_90b4_bf81_705a_4fdc5ef12ba6
  ce919cf3_ad88_d635_6f38_41d29648c681["isEquivalentCode()"]
  320e2cfd_90b4_bf81_705a_4fdc5ef12ba6 -->|calls| ce919cf3_ad88_d635_6f38_41d29648c681
  style 320e2cfd_90b4_bf81_705a_4fdc5ef12ba6 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

scripts/eslint-rules/safe-string-coercion.js lines 195–269

function hasCoercionCheck(node) {
  // find the containing statement
  let topOfExpression = node;
  while (!topOfExpression.parent.body) {
    topOfExpression = topOfExpression.parent;
    if (!topOfExpression) {
      return 'Cannot find top of expression.';
    }
  }
  const containingBlock = topOfExpression.parent.body;
  const index = containingBlock.indexOf(topOfExpression);
  if (index <= 0) {
    return missingDevCheckMessage;
  }
  const prev = containingBlock[index - 1];

  // The previous statement is expected to be like this:
  //   if (__DEV__) {
  //     checkFormFieldValueStringCoercion(foo);
  //   }
  // where `foo` must be equivalent to `node` (which is the
  // mixed value being coerced to a string).
  if (
    prev.type !== 'IfStatement' ||
    prev.test.type !== 'Identifier' ||
    prev.test.name !== '__DEV__'
  ) {
    return prevStatementNotDevCheckMessage;
  }
  let maybeCheckNode = prev.consequent;
  if (maybeCheckNode.type === 'BlockStatement') {
    const body = maybeCheckNode.body;
    if (body.length === 0) {
      return prevStatementNotDevCheckMessage;
    }
    if (body.length !== 1) {
      return (
        'Too many statements in DEV block before this coercion.' +
        ' Expected only one (the check function call). ' +
        prevStatementNotDevCheckMessage
      );
    }
    maybeCheckNode = body[0];
  }

  if (maybeCheckNode.type !== 'ExpressionStatement') {
    return (
      'The DEV block before this coercion must only contain an expression. ' +
      prevStatementNotDevCheckMessage
    );
  }

  const call = maybeCheckNode.expression;
  if (
    call.type !== 'CallExpression' ||
    call.callee.type !== 'Identifier' ||
    !/^check(\w+?)StringCoercion$/.test(call.callee.name) ||
    !call.arguments.length
  ) {
    // `maybeCheckNode` should be a call of a function named checkXXXStringCoercion
    return (
      'Missing or invalid check function call before this coercion.' +
      ' Expected: call of a function like checkXXXStringCoercion. ' +
      prevStatementNotDevCheckMessage
    );
  }

  const same = isEquivalentCode(call.arguments[0], node);
  if (!same) {
    return (
      'Value passed to the check function before this coercion' +
      ' must match the value being coerced.'
    );
  }
}

Domain

Subdomains

Frequently Asked Questions

What does hasCoercionCheck() do?
hasCoercionCheck() is a function in the react codebase, defined in scripts/eslint-rules/safe-string-coercion.js.
Where is hasCoercionCheck() defined?
hasCoercionCheck() is defined in scripts/eslint-rules/safe-string-coercion.js at line 195.
What does hasCoercionCheck() call?
hasCoercionCheck() calls 1 function(s): isEquivalentCode.
What calls hasCoercionCheck()?
hasCoercionCheck() 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