Home / Function/ scanForConstructions() — react Function Reference

scanForConstructions() — react Function Reference

Architecture documentation for the scanForConstructions() function in ExhaustiveDeps.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  a8ca46d1_1e1d_7275_227b_3492441252b4["scanForConstructions()"]
  ea02b01a_dd46_4b35_fe00_775aec496668["ExhaustiveDeps.ts"]
  a8ca46d1_1e1d_7275_227b_3492441252b4 -->|defined in| ea02b01a_dd46_4b35_fe00_775aec496668
  c0937ffe_5980_6959_3af6_e18a4a23114f["rule.create()"]
  c0937ffe_5980_6959_3af6_e18a4a23114f -->|calls| a8ca46d1_1e1d_7275_227b_3492441252b4
  7a2259f8_1f4c_06e7_2604_f580d5a246f0["getConstructionExpressionType()"]
  a8ca46d1_1e1d_7275_227b_3492441252b4 -->|calls| 7a2259f8_1f4c_06e7_2604_f580d5a246f0
  84d634ed_daf4_049c_839c_c1e7767aaaae["isAncestorNodeOf()"]
  a8ca46d1_1e1d_7275_227b_3492441252b4 -->|calls| 84d634ed_daf4_049c_839c_c1e7767aaaae
  style a8ca46d1_1e1d_7275_227b_3492441252b4 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/eslint-plugin-react-hooks/src/rules/ExhaustiveDeps.ts lines 1767–1856

function scanForConstructions({
  declaredDependencies,
  declaredDependenciesNode,
  componentScope,
  scope,
}: {
  declaredDependencies: Array<DeclaredDependency>;
  declaredDependenciesNode: Node;
  componentScope: Scope.Scope;
  scope: Scope.Scope;
}) {
  const constructions = declaredDependencies
    .map(({key}) => {
      const ref = componentScope.variables.find(v => v.name === key);
      if (ref == null) {
        return null;
      }

      const node = ref.defs[0];
      if (node == null) {
        return null;
      }
      // const handleChange = function () {}
      // const handleChange = () => {}
      // const foo = {}
      // const foo = []
      // etc.
      if (
        node.type === 'Variable' &&
        node.node.type === 'VariableDeclarator' &&
        node.node.id.type === 'Identifier' && // Ensure this is not destructed assignment
        node.node.init != null
      ) {
        const constantExpressionType = getConstructionExpressionType(
          node.node.init,
        );
        if (constantExpressionType) {
          return [ref, constantExpressionType];
        }
      }
      // function handleChange() {}
      if (
        node.type === 'FunctionName' &&
        node.node.type === 'FunctionDeclaration'
      ) {
        return [ref, 'function'];
      }

      // class Foo {}
      if (node.type === 'ClassName' && node.node.type === 'ClassDeclaration') {
        return [ref, 'class'];
      }
      return null;
    })
    .filter(Boolean) as Array<[Scope.Variable, string]>;

  function isUsedOutsideOfHook(ref: Scope.Variable): boolean {
    let foundWriteExpr = false;
    for (const reference of ref.references) {
      if (reference.writeExpr) {
        if (foundWriteExpr) {
          // Two writes to the same function.
          return true;
        } else {
          // Ignore first write as it's not usage.
          foundWriteExpr = true;
          continue;
        }
      }
      let currentScope: Scope.Scope | null = reference.from;
      while (currentScope !== scope && currentScope != null) {
        currentScope = currentScope.upper;
      }
      if (currentScope !== scope) {
        // This reference is outside the Hook callback.
        // It can only be legit if it's the deps array.
        if (!isAncestorNodeOf(declaredDependenciesNode, reference.identifier)) {
          return true;
        }
      }
    }

Domain

Subdomains

Called By

Frequently Asked Questions

What does scanForConstructions() do?
scanForConstructions() is a function in the react codebase, defined in packages/eslint-plugin-react-hooks/src/rules/ExhaustiveDeps.ts.
Where is scanForConstructions() defined?
scanForConstructions() is defined in packages/eslint-plugin-react-hooks/src/rules/ExhaustiveDeps.ts at line 1767.
What does scanForConstructions() call?
scanForConstructions() calls 2 function(s): getConstructionExpressionType, isAncestorNodeOf.
What calls scanForConstructions()?
scanForConstructions() is called by 1 function(s): rule.create.

Analyze Your Own Codebase

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

Try Supermodel Free