[d565449] | 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
|
---|
[0c6b92a] | 14 | * @returns {Record<string, 2 | 'error'>}
|
---|
[d565449] | 15 | */
|
---|
| 16 | function configureAsError(rules) {
|
---|
| 17 | return fromEntries(Object.keys(rules).map((key) => [`react/${key}`, 2]));
|
---|
| 18 | }
|
---|
| 19 |
|
---|
[0c6b92a] | 20 | /** @type {Partial<typeof allRules>} */
|
---|
[d565449] | 21 | const activeRules = filterRules(allRules, (rule) => !rule.meta.deprecated);
|
---|
[0c6b92a] | 22 | /** @type {Record<keyof typeof activeRules, 2 | 'error'>} */
|
---|
[d565449] | 23 | const activeRulesConfig = configureAsError(activeRules);
|
---|
| 24 |
|
---|
[0c6b92a] | 25 | /** @type {Partial<typeof allRules>} */
|
---|
[d565449] | 26 | const deprecatedRules = filterRules(allRules, (rule) => rule.meta.deprecated);
|
---|
| 27 |
|
---|
[0c6b92a] | 28 | /** @type {['react']} */
|
---|
[d565449] | 29 | // for legacy config system
|
---|
| 30 | const plugins = [
|
---|
| 31 | 'react',
|
---|
| 32 | ];
|
---|
| 33 |
|
---|
[0c6b92a] | 34 | const configs = {
|
---|
| 35 | recommended: {
|
---|
| 36 | plugins,
|
---|
| 37 | parserOptions: {
|
---|
| 38 | ecmaFeatures: {
|
---|
| 39 | jsx: true,
|
---|
[d565449] | 40 | },
|
---|
| 41 | },
|
---|
[0c6b92a] | 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,
|
---|
[d565449] | 65 | },
|
---|
[0c6b92a] | 66 | },
|
---|
| 67 | all: {
|
---|
| 68 | plugins,
|
---|
| 69 | parserOptions: {
|
---|
| 70 | ecmaFeatures: {
|
---|
| 71 | jsx: true,
|
---|
[d565449] | 72 | },
|
---|
[0c6b92a] | 73 | },
|
---|
| 74 | rules: activeRulesConfig,
|
---|
| 75 | },
|
---|
| 76 | 'jsx-runtime': {
|
---|
| 77 | plugins,
|
---|
| 78 | parserOptions: {
|
---|
| 79 | ecmaFeatures: {
|
---|
| 80 | jsx: true,
|
---|
[d565449] | 81 | },
|
---|
[0c6b92a] | 82 | jsxPragma: null, // for @typescript/eslint-parser
|
---|
| 83 | },
|
---|
| 84 | rules: {
|
---|
| 85 | 'react/react-in-jsx-scope': 0,
|
---|
| 86 | 'react/jsx-uses-react': 0,
|
---|
[d565449] | 87 | },
|
---|
| 88 | },
|
---|
| 89 | };
|
---|
| 90 |
|
---|
[0c6b92a] | 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 = {
|
---|
[d565449] | 102 | recommended: {
|
---|
| 103 | plugins: { react: plugin },
|
---|
[0c6b92a] | 104 | rules: configs.recommended.rules,
|
---|
| 105 | languageOptions: { parserOptions: configs.recommended.parserOptions },
|
---|
[d565449] | 106 | },
|
---|
| 107 | all: {
|
---|
| 108 | plugins: { react: plugin },
|
---|
[0c6b92a] | 109 | rules: configs.all.rules,
|
---|
| 110 | languageOptions: { parserOptions: configs.all.parserOptions },
|
---|
[d565449] | 111 | },
|
---|
| 112 | 'jsx-runtime': {
|
---|
| 113 | plugins: { react: plugin },
|
---|
[0c6b92a] | 114 | rules: configs['jsx-runtime'].rules,
|
---|
| 115 | languageOptions: { parserOptions: configs['jsx-runtime'].parserOptions },
|
---|
[d565449] | 116 | },
|
---|
| 117 | };
|
---|
| 118 |
|
---|
| 119 | module.exports = plugin;
|
---|