Home / File/ InvalidHooksRule-test.ts — react Source File

InvalidHooksRule-test.ts — react Source File

Architecture documentation for InvalidHooksRule-test.ts, a typescript file in the react codebase. 7 imports, 0 dependents.

Entity Profile

Dependency Diagram

graph LR
  7b06326c_10b5_45ec_3ebb_f8b909c17ef4["InvalidHooksRule-test.ts"]
  799656f0_feaf_2c82_77fc_4a08283ea146["shared-utils.ts"]
  7b06326c_10b5_45ec_3ebb_f8b909c17ef4 --> 799656f0_feaf_2c82_77fc_4a08283ea146
  f9d93ee9_a1c8_932c_1dae_abe7839e287e["normalizeIndent"]
  7b06326c_10b5_45ec_3ebb_f8b909c17ef4 --> f9d93ee9_a1c8_932c_1dae_abe7839e287e
  1ce82843_86af_f383_f6a3_ab648fb0e6be["makeTestCaseError"]
  7b06326c_10b5_45ec_3ebb_f8b909c17ef4 --> 1ce82843_86af_f383_f6a3_ab648fb0e6be
  7cb0395e_ec02_facf_a171_f9c7ce1a86b7["testRule"]
  7b06326c_10b5_45ec_3ebb_f8b909c17ef4 --> 7cb0395e_ec02_facf_a171_f9c7ce1a86b7
  cc8eb92a_b0c5_c461_8a5e_7435585bbc6e["ReactCompilerRule.ts"]
  7b06326c_10b5_45ec_3ebb_f8b909c17ef4 --> cc8eb92a_b0c5_c461_8a5e_7435585bbc6e
  c2fb1111_dc32_da0a_d43a_b38b8d13f4e9["allRules"]
  7b06326c_10b5_45ec_3ebb_f8b909c17ef4 --> c2fb1111_dc32_da0a_d43a_b38b8d13f4e9
  f1417319_0940_9839_f3b2_304147ce4f9d["CompilerError"]
  7b06326c_10b5_45ec_3ebb_f8b909c17ef4 --> f1417319_0940_9839_f3b2_304147ce4f9d
  style 7b06326c_10b5_45ec_3ebb_f8b909c17ef4 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import {
  ErrorCategory,
  getRuleForCategory,
} from 'babel-plugin-react-compiler/src/CompilerError';
import {normalizeIndent, makeTestCaseError, testRule} from './shared-utils';
import {allRules} from '../src/rules/ReactCompilerRule';

testRule(
  'rules-of-hooks',
  allRules[getRuleForCategory(ErrorCategory.Hooks).name].rule,
  {
    valid: [
      {
        name: 'Basic example',
        code: normalizeIndent`
        function Component() {
          useHook();
          return <div>Hello world</div>;
        }
      `,
      },
      {
        name: 'Violation with Flow suppression',
        code: `
      // Valid since error already suppressed with flow.
      function useHook() {
        if (cond) {
          // $FlowFixMe[react-rule-hook]
          useConditionalHook();
        }
      }
    `,
      },
      {
        // OK because invariants are only meant for the compiler team's consumption
        name: '[Invariant] Defined after use',
        code: normalizeIndent`
        function Component(props) {
          let y = function () {
            m(x);
          };

          let x = { a };
          m(x);
          return y;
        }
      `,
      },
      {
        name: "Classes don't throw",
        code: normalizeIndent`
        class Foo {
          #bar() {}
        }
      `,
      },
    ],
    invalid: [
      {
        name: 'Simple violation',
        code: normalizeIndent`
      function useConditional() {
        if (cond) {
          useConditionalHook();
        }
      }
    `,
        errors: [
          makeTestCaseError(
            'Hooks must always be called in a consistent order',
          ),
        ],
      },
      {
        name: 'Multiple diagnostics within the same function are surfaced',
        code: normalizeIndent`
        function useConditional() {
          cond ?? useConditionalHook();
          props.cond && useConditionalHook();
          return <div>Hello world</div>;
        }`,
        errors: [
          makeTestCaseError(
            'Hooks must always be called in a consistent order',
          ),
          makeTestCaseError(
            'Hooks must always be called in a consistent order',
          ),
        ],
      },
    ],
  },
);

Domain

Frequently Asked Questions

What does InvalidHooksRule-test.ts do?
InvalidHooksRule-test.ts is a source file in the react codebase, written in typescript. It belongs to the BabelCompiler domain.
What does InvalidHooksRule-test.ts depend on?
InvalidHooksRule-test.ts imports 7 module(s): CompilerError, ReactCompilerRule.ts, allRules, makeTestCaseError, normalizeIndent, shared-utils.ts, testRule.
Where is InvalidHooksRule-test.ts in the architecture?
InvalidHooksRule-test.ts is located at compiler/packages/eslint-plugin-react-compiler/__tests__/InvalidHooksRule-test.ts (domain: BabelCompiler, directory: compiler/packages/eslint-plugin-react-compiler/__tests__).

Analyze Your Own Codebase

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

Try Supermodel Free