main
ts 117 lines 2.82 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7 import type {Linter, Rule} from 'eslint';
8
9 import ExhaustiveDeps from './rules/ExhaustiveDeps';
10 import {
11 allRules,
12 mapErrorSeverityToESlint,
13 recommendedRules,
14 recommendedLatestRules,
15 } from './shared/ReactCompiler';
16 import RulesOfHooks from './rules/RulesOfHooks';
17
18 function makeDeprecatedRule(version: string): Rule.RuleModule {
19 return {
20 meta: {
21 type: 'suggestion',
22 docs: {
23 description: `Deprecated: this rule has been removed in ${version}.`,
24 },
25 schema: [],
26 deprecated: true,
27 },
28 create() {
29 return {};
30 },
31 };
32 }
33
34 const rules = {
35 'exhaustive-deps': ExhaustiveDeps,
36 'rules-of-hooks': RulesOfHooks,
37 ...Object.fromEntries(
38 Object.entries(allRules).map(([name, config]) => [name, config.rule]),
39 ),
40 'component-hook-factories': makeDeprecatedRule('7.1.0'),
41 } satisfies Record<string, Rule.RuleModule>;
42
43 const basicRuleConfigs = {
44 'react-hooks/rules-of-hooks': 'error',
45 'react-hooks/exhaustive-deps': 'warn',
46 } as const satisfies Linter.RulesRecord;
47
48 const recommendedCompilerRuleConfigs = Object.fromEntries(
49 Object.entries(recommendedRules).map(([name, ruleConfig]) => {
50 return [
51 `react-hooks/${name}` as const,
52 mapErrorSeverityToESlint(ruleConfig.severity),
53 ] as const;
54 }),
55 ) as Record<`react-hooks/${string}`, Linter.RuleEntry>;
56
57 const recommendedLatestCompilerRuleConfigs = Object.fromEntries(
58 Object.entries(recommendedLatestRules).map(([name, ruleConfig]) => {
59 return [
60 `react-hooks/${name}` as const,
61 mapErrorSeverityToESlint(ruleConfig.severity),
62 ] as const;
63 }),
64 ) as Record<`react-hooks/${string}`, Linter.RuleEntry>;
65
66 const recommendedRuleConfigs: Linter.RulesRecord = {
67 ...basicRuleConfigs,
68 ...recommendedCompilerRuleConfigs,
69 };
70 const recommendedLatestRuleConfigs: Linter.RulesRecord = {
71 ...basicRuleConfigs,
72 ...recommendedLatestCompilerRuleConfigs,
73 };
74
75 const plugins = ['react-hooks'];
76
77 type ReactHooksFlatConfig = {
78 plugins: {react: any};
79 rules: Linter.RulesRecord;
80 };
81
82 const configs = {
83 recommended: {
84 plugins,
85 rules: recommendedRuleConfigs,
86 },
87 'recommended-latest': {
88 plugins,
89 rules: recommendedLatestRuleConfigs,
90 },
91 flat: {} as {
92 recommended: ReactHooksFlatConfig;
93 'recommended-latest': ReactHooksFlatConfig;
94 },
95 };
96
97 const plugin = {
98 meta: {
99 name: 'eslint-plugin-react-hooks',
100 version: '7.0.0',
101 },
102 rules,
103 configs,
104 };
105
106 Object.assign(configs.flat, {
107 'recommended-latest': {
108 plugins: {'react-hooks': plugin},
109 rules: configs['recommended-latest'].rules,
110 },
111 recommended: {
112 plugins: {'react-hooks': plugin},
113 rules: configs.recommended.rules,
114 },
115 });
116
117 export default plugin;