1 | 'use strict';
|
---|
2 |
|
---|
3 | const fromEntries = require('object.fromentries');
|
---|
4 | const entries = require('object.entries');
|
---|
5 |
|
---|
6 | const allRules = require('./lib/rules');
|
---|
7 |
|
---|
8 | function filterRules(rules, predicate) {
|
---|
9 | return fromEntries(entries(rules).filter((entry) => predicate(entry[1])));
|
---|
10 | }
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * @param {object} rules - rules object mapping rule name to rule module
|
---|
14 | * @returns {Record<string, 2 | 'error'>}
|
---|
15 | */
|
---|
16 | function configureAsError(rules) {
|
---|
17 | return fromEntries(Object.keys(rules).map((key) => [`react/${key}`, 2]));
|
---|
18 | }
|
---|
19 |
|
---|
20 | /** @type {Partial<typeof allRules>} */
|
---|
21 | const activeRules = filterRules(allRules, (rule) => !rule.meta.deprecated);
|
---|
22 | /** @type {Record<keyof typeof activeRules, 2 | 'error'>} */
|
---|
23 | const activeRulesConfig = configureAsError(activeRules);
|
---|
24 |
|
---|
25 | /** @type {Partial<typeof allRules>} */
|
---|
26 | const deprecatedRules = filterRules(allRules, (rule) => rule.meta.deprecated);
|
---|
27 |
|
---|
28 | /** @type {['react']} */
|
---|
29 | // for legacy config system
|
---|
30 | const plugins = [
|
---|
31 | 'react',
|
---|
32 | ];
|
---|
33 |
|
---|
34 | const configs = {
|
---|
35 | recommended: {
|
---|
36 | plugins,
|
---|
37 | parserOptions: {
|
---|
38 | ecmaFeatures: {
|
---|
39 | jsx: true,
|
---|
40 | },
|
---|
41 | },
|
---|
42 | rules: {
|
---|
43 | 'react/display-name': 2,
|
---|
44 | 'react/jsx-key': 2,
|
---|
45 | 'react/jsx-no-comment-textnodes': 2,
|
---|
46 | 'react/jsx-no-duplicate-props': 2,
|
---|
47 | 'react/jsx-no-target-blank': 2,
|
---|
48 | 'react/jsx-no-undef': 2,
|
---|
49 | 'react/jsx-uses-react': 2,
|
---|
50 | 'react/jsx-uses-vars': 2,
|
---|
51 | 'react/no-children-prop': 2,
|
---|
52 | 'react/no-danger-with-children': 2,
|
---|
53 | 'react/no-deprecated': 2,
|
---|
54 | 'react/no-direct-mutation-state': 2,
|
---|
55 | 'react/no-find-dom-node': 2,
|
---|
56 | 'react/no-is-mounted': 2,
|
---|
57 | 'react/no-render-return-value': 2,
|
---|
58 | 'react/no-string-refs': 2,
|
---|
59 | 'react/no-unescaped-entities': 2,
|
---|
60 | 'react/no-unknown-property': 2,
|
---|
61 | 'react/no-unsafe': 0,
|
---|
62 | 'react/prop-types': 2,
|
---|
63 | 'react/react-in-jsx-scope': 2,
|
---|
64 | 'react/require-render-return': 2,
|
---|
65 | },
|
---|
66 | },
|
---|
67 | all: {
|
---|
68 | plugins,
|
---|
69 | parserOptions: {
|
---|
70 | ecmaFeatures: {
|
---|
71 | jsx: true,
|
---|
72 | },
|
---|
73 | },
|
---|
74 | rules: activeRulesConfig,
|
---|
75 | },
|
---|
76 | 'jsx-runtime': {
|
---|
77 | plugins,
|
---|
78 | parserOptions: {
|
---|
79 | ecmaFeatures: {
|
---|
80 | jsx: true,
|
---|
81 | },
|
---|
82 | jsxPragma: null, // for @typescript/eslint-parser
|
---|
83 | },
|
---|
84 | rules: {
|
---|
85 | 'react/react-in-jsx-scope': 0,
|
---|
86 | 'react/jsx-uses-react': 0,
|
---|
87 | },
|
---|
88 | },
|
---|
89 | };
|
---|
90 |
|
---|
91 | /** @typedef {{ plugins: { react: typeof plugin }, rules: import('eslint').Linter.RulesRecord, languageOptions: { parserOptions: import('eslint').Linter.ParserOptions } }} ReactFlatConfig */
|
---|
92 |
|
---|
93 | /** @type {{ deprecatedRules: typeof deprecatedRules, rules: typeof allRules, configs: typeof configs & { flat?: Record<string, ReactFlatConfig> }}} */
|
---|
94 | const plugin = {
|
---|
95 | deprecatedRules,
|
---|
96 | rules: allRules,
|
---|
97 | configs,
|
---|
98 | };
|
---|
99 |
|
---|
100 | /** @type {Record<string, ReactFlatConfig>} */
|
---|
101 | configs.flat = {
|
---|
102 | recommended: {
|
---|
103 | plugins: { react: plugin },
|
---|
104 | rules: configs.recommended.rules,
|
---|
105 | languageOptions: { parserOptions: configs.recommended.parserOptions },
|
---|
106 | },
|
---|
107 | all: {
|
---|
108 | plugins: { react: plugin },
|
---|
109 | rules: configs.all.rules,
|
---|
110 | languageOptions: { parserOptions: configs.all.parserOptions },
|
---|
111 | },
|
---|
112 | 'jsx-runtime': {
|
---|
113 | plugins: { react: plugin },
|
---|
114 | rules: configs['jsx-runtime'].rules,
|
---|
115 | languageOptions: { parserOptions: configs['jsx-runtime'].parserOptions },
|
---|
116 | },
|
---|
117 | };
|
---|
118 |
|
---|
119 | module.exports = plugin;
|
---|