ESLint Plugins
Relevant source files
The following files were used as context for generating this wiki page:
- packages/eslint-plugin-react-hooks/src/index.ts
- packages/eslint-plugin-react-hooks/src/rules/ExhaustiveDeps.ts
- packages/eslint-plugin-react-hooks/src/rules/RulesOfHooks.ts
- compiler/packages/eslint-plugin-react-compiler/src/index.ts
- packages/eslint-plugin-react-hooks/README.md
- packages/eslint-plugin-react-hooks/package.json
- compiler/packages/eslint-plugin-react-compiler/package.json
The React project includes ESLint plugins to enforce coding conventions and integrate compiler feedback directly into the development workflow. These plugins help developers write more robust and performant React code by catching common errors and ensuring adherence to best practices, such as the Rules of Hooks.
The primary plugins are eslint-plugin-react-hooks, which enforces hook usage rules, and the experimental eslint-plugin-react-compiler, which displays errors from the React Compiler. These tools are essential for maintaining code quality and leveraging React's features correctly.
eslint-plugin-react-hooks
This plugin enforces the Rules of Hooks, which are critical for writing correct and predictable components using Hooks. It is designed to be used with ESLint and supports various parsers, including Babel and TypeScript.
Sources: packages/eslint-plugin-react-hooks/README.md, packages/eslint-plugin-react-hooks/package.json
Plugin Structure and Rules
The plugin exports a set of rules and a recommended configuration to easily enable them.
Sources: packages/eslint-plugin-react-hooks/src/index.ts:13-23
| Rule | Description |
|---|---|
rules-of-hooks | Enforces that Hooks are only called at the top level of React components or custom Hooks. |
exhaustive-deps | Verifies that all dependencies are correctly specified in the dependency array of Hooks like useEffect and useCallback. |
This diagram shows how the plugin is structured and exposed to ESLint.
Rule: rules-of-hooks
This rule ensures that Hooks are called in the same order on every render. It achieves this by enforcing that Hooks are not called inside loops, conditions, or nested functions.
The rule's logic identifies two types of functions where Hooks can be used:
- React Function Components: Functions whose names start with a capital letter (PascalCase).
- Custom Hooks: Functions whose names start with the word "use" followed by a capital letter.
Any other function is considered a plain JavaScript function where calling a Hook is not allowed.
Sources: packages/eslint-plugin-react-hooks/src/rules/RulesOfHooks.ts:119-141
The validation process can be visualized as follows:
Rule: exhaustive-deps
This rule analyzes Hooks that have a dependency array (e.g., useEffect, useCallback, useMemo) and reports any missing, unnecessary, or duplicate dependencies. This helps prevent stale closures and infinite render loops.
The rule's core logic involves:
- Identifying calls to reactive hooks.
- Traversing the hook's callback function to find all references to variables defined in the component's scope.
- Determining if each referenced variable is "stable" (e.g.,
setStatefunctions,useRefvalues) or dynamic. - Comparing the list of dynamic dependencies with the user-provided dependency array.
- Reporting discrepancies and suggesting a fix.
Sources: packages/eslint-plugin-react-hooks/src/rules/ExhaustiveDeps.ts:208-226, 614-633
This diagram illustrates the dependency analysis flow:
Configuration
The exhaustive-deps rule can be customized with several options.
| Option | Type | Default | Description |
|---|---|---|---|
additionalHooks | string (regex) | undefined | A regex to match the names of custom Hooks that also have dependency arrays. |
enableDangerousAutofixThisMayCauseInfiniteLoops | boolean | false | Enables an autofix for suggestions, which may not always be safe. |
experimental_autoDependenciesHooks | string[] | [] | An array of hook names that do not require a dependency array. |
requireExplicitEffectDeps | boolean | false | Requires useEffect and useLayoutEffect to have a dependency array. |
Sources: packages/eslint-plugin-react-hooks/src/rules/ExhaustiveDeps.ts:71-112
An example configuration from the documentation:
{
"rules": {
// ...
"react-hooks/exhaustive-deps": ["warn", {
"additionalHooks": "(useMyCustomHook|useMyOtherCustomHook)"
}]
}
}
Sources: packages/eslint-plugin-react-hooks/README.md:88-94
eslint-plugin-react-compiler
This is an experimental ESLint plugin designed to surface errors and diagnostics from the React Compiler directly in the editor. The compiler, also known as "Forget", aims to automatically memoize components and hooks, removing the need for manual useMemo and useCallback.
This plugin acts as a bridge between the React Compiler and the ESLint ecosystem.
Sources: compiler/packages/eslint-plugin-react-compiler/package.json:3, compiler/packages/eslint-plugin-react-compiler/src/index.ts:13-17
Architecture
The plugin defines a single rule, react-compiler/react-compiler. When ESLint runs, this rule invokes the React Compiler's Babel plugin on each file. If the compiler encounters any issues or has diagnostics to report, they are formatted and reported as ESLint errors.
This provides developers with real-time feedback from the compiler as they write code.
Sources: compiler/packages/eslint-plugin-react-compiler/src/index.ts:25-41
The data flow is as follows: