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