Home / Function/ rule.create() — react Function Reference

rule.create() — react Function Reference

Architecture documentation for the rule.create() function in RulesOfHooks.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  be7612d8_31aa_c40a_35d5_89fd476ab28a["rule.create()"]
  47fb9f02_49dd_1d0d_5027_9353a6a77f1e["RulesOfHooks.ts"]
  be7612d8_31aa_c40a_35d5_89fd476ab28a -->|defined in| 47fb9f02_49dd_1d0d_5027_9353a6a77f1e
  1a929da5_96c8_bcab_86db_7fe06b23af38["getAdditionalEffectHooksFromSettings()"]
  be7612d8_31aa_c40a_35d5_89fd476ab28a -->|calls| 1a929da5_96c8_bcab_86db_7fe06b23af38
  657a7d96_15bd_ec3f_b63e_8adf3adf4aeb["isUseEffectEventIdentifier()"]
  be7612d8_31aa_c40a_35d5_89fd476ab28a -->|calls| 657a7d96_15bd_ec3f_b63e_8adf3adf4aeb
  8602459e_8564_b68b_f89f_016bed3c8ea4["getFunctionName()"]
  be7612d8_31aa_c40a_35d5_89fd476ab28a -->|calls| 8602459e_8564_b68b_f89f_016bed3c8ea4
  e33d7f9c_2333_764f_8ca1_79868b065cbf["isInsideComponentOrHook()"]
  be7612d8_31aa_c40a_35d5_89fd476ab28a -->|calls| e33d7f9c_2333_764f_8ca1_79868b065cbf
  89d4f39b_deac_2a1d_4844_63ce247622ec["isComponentName()"]
  be7612d8_31aa_c40a_35d5_89fd476ab28a -->|calls| 89d4f39b_deac_2a1d_4844_63ce247622ec
  3fc9f24c_035f_b907_304f_45903ee4e5d7["isHook()"]
  be7612d8_31aa_c40a_35d5_89fd476ab28a -->|calls| 3fc9f24c_035f_b907_304f_45903ee4e5d7
  570b7e73_2c19_4f41_d9ef_4540eb990f99["isForwardRefCallback()"]
  be7612d8_31aa_c40a_35d5_89fd476ab28a -->|calls| 570b7e73_2c19_4f41_d9ef_4540eb990f99
  c846db86_f5e0_0014_473e_35406d2b6173["isMemoCallback()"]
  be7612d8_31aa_c40a_35d5_89fd476ab28a -->|calls| c846db86_f5e0_0014_473e_35406d2b6173
  076803a2_9180_1797_745f_04e71e55caf2["isUseIdentifier()"]
  be7612d8_31aa_c40a_35d5_89fd476ab28a -->|calls| 076803a2_9180_1797_745f_04e71e55caf2
  8240b6ea_7cbc_f6ae_5fca_c6ad357ef7a8["isInsideTryCatch()"]
  be7612d8_31aa_c40a_35d5_89fd476ab28a -->|calls| 8240b6ea_7cbc_f6ae_5fca_c6ad357ef7a8
  ea57d7b9_b059_54cc_f0c0_014a77807638["isInsideDoWhileLoop()"]
  be7612d8_31aa_c40a_35d5_89fd476ab28a -->|calls| ea57d7b9_b059_54cc_f0c0_014a77807638
  d082f1e7_4eef_c1db_f99e_4cfec174190f["enterNode()"]
  be7612d8_31aa_c40a_35d5_89fd476ab28a -->|calls| d082f1e7_4eef_c1db_f99e_4cfec174190f
  dc411858_ea9d_ab2f_49db_a3428829fbde["leaveNode()"]
  be7612d8_31aa_c40a_35d5_89fd476ab28a -->|calls| dc411858_ea9d_ab2f_49db_a3428829fbde
  style be7612d8_31aa_c40a_35d5_89fd476ab28a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/eslint-plugin-react-hooks/src/rules/RulesOfHooks.ts lines 214–849

  create(context: Rule.RuleContext) {
    const settings = context.settings || {};

    const additionalEffectHooks =
      getAdditionalEffectHooksFromSettings(settings);

    let lastEffect: CallExpression | null = null;
    const codePathReactHooksMapStack: Array<
      Map<Rule.CodePathSegment, Array<Node>>
    > = [];
    const codePathSegmentStack: Array<Rule.CodePathSegment> = [];
    const useEffectEventFunctions = new WeakSet();

    // For a given scope, iterate through the references and add all useEffectEvent definitions. We can
    // do this in non-Program nodes because we can rely on the assumption that useEffectEvent functions
    // can only be declared within a component or hook at its top level.
    function recordAllUseEffectEventFunctions(scope: Scope.Scope): void {
      for (const reference of scope.references) {
        const parent = reference.identifier.parent;
        if (
          parent?.type === 'VariableDeclarator' &&
          parent.init &&
          parent.init.type === 'CallExpression' &&
          parent.init.callee &&
          isUseEffectEventIdentifier(parent.init.callee)
        ) {
          if (reference.resolved === null) {
            throw new Error('Unexpected null reference.resolved');
          }
          for (const ref of reference.resolved.references) {
            if (ref !== reference) {
              useEffectEventFunctions.add(ref.identifier);
            }
          }
        }
      }
    }

    /**
     * SourceCode that also works down to ESLint 3.0.0
     */
    const getSourceCode =
      typeof context.getSourceCode === 'function'
        ? () => {
            return context.getSourceCode();
          }
        : () => {
            return context.sourceCode;
          };
    /**
     * SourceCode#getScope that also works down to ESLint 3.0.0
     */
    const getScope =
      typeof context.getScope === 'function'
        ? (): Scope.Scope => {
            return context.getScope();
          }
        : (node: Node): Scope.Scope => {
            return getSourceCode().getScope(node);
          };

    function hasFlowSuppression(node: Node, suppression: string) {
      const sourceCode = getSourceCode();
      const comments = sourceCode.getAllComments();
      const flowSuppressionRegex = new RegExp(
        '\\$FlowFixMe\\[' + suppression + '\\]',
      );
      return comments.some(
        commentNode =>
          flowSuppressionRegex.test(commentNode.value) &&
          commentNode.loc != null &&
          node.loc != null &&
          commentNode.loc.end.line === node.loc.start.line - 1,
      );
    }

    const analyzer = new CodePathAnalyzer({
      // Maintain code segment path stack as we traverse.
      onCodePathSegmentStart: (segment: Rule.CodePathSegment) =>
        codePathSegmentStack.push(segment),
      onCodePathSegmentEnd: () => codePathSegmentStack.pop(),

Domain

Subdomains

Frequently Asked Questions

What does rule.create() do?
rule.create() is a function in the react codebase, defined in packages/eslint-plugin-react-hooks/src/rules/RulesOfHooks.ts.
Where is rule.create() defined?
rule.create() is defined in packages/eslint-plugin-react-hooks/src/rules/RulesOfHooks.ts at line 214.
What does rule.create() call?
rule.create() calls 17 function(s): enterNode, getAdditionalEffectHooksFromSettings, getFunctionName, getNodeWithoutReactNamespace, isComponentName, isEffectIdentifier, isForwardRefCallback, isHook, and 9 more.

Analyze Your Own Codebase

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

Try Supermodel Free