-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathindex.js
158 lines (143 loc) · 4.92 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
'use strict';
const fromEntries = require('object.fromentries');
const entries = require('object.entries');
const packageMeta = require('./package.json');
const allRules = require('./lib/rules');
const name = packageMeta.name;
const version = packageMeta.version;
function filterRules(rules, predicate) {
return fromEntries(entries(rules).filter((entry) => predicate(entry[1])));
}
/**
* @param {object} rules - rules object mapping rule name to rule module
* @returns {Record<string, SEVERITY_ERROR | 'error'>}
*/
function configureAsError(rules) {
return fromEntries(Object.keys(rules).map((key) => [`react/${key}`, 2]));
}
/** @type {Partial<typeof allRules>} */
const activeRules = filterRules(allRules, (rule) => !rule.meta.deprecated);
/** @type {Record<keyof typeof activeRules, 2 | 'error'>} */
const activeRulesConfig = configureAsError(activeRules);
/** @type {Partial<typeof allRules>} */
const deprecatedRules = filterRules(allRules, (rule) => rule.meta.deprecated);
/** @type {['react']} */
// for legacy config system
const plugins = ['react'];
// TODO: with TS 4.5+, inline this
const SEVERITY_ERROR = /** @type {2} */ (2);
const SEVERITY_OFF = /** @type {0} */ (0);
const configs = {
recommended: {
plugins,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
rules: {
'react/display-name': SEVERITY_ERROR,
'react/jsx-key': SEVERITY_ERROR,
'react/jsx-no-comment-textnodes': SEVERITY_ERROR,
'react/jsx-no-duplicate-props': SEVERITY_ERROR,
'react/jsx-no-target-blank': SEVERITY_ERROR,
'react/jsx-no-undef': SEVERITY_ERROR,
'react/jsx-uses-react': SEVERITY_ERROR,
'react/jsx-uses-vars': SEVERITY_ERROR,
'react/no-children-prop': SEVERITY_ERROR,
'react/no-danger-with-children': SEVERITY_ERROR,
'react/no-deprecated': SEVERITY_ERROR,
'react/no-direct-mutation-state': SEVERITY_ERROR,
'react/no-find-dom-node': SEVERITY_ERROR,
'react/no-is-mounted': SEVERITY_ERROR,
'react/no-render-return-value': SEVERITY_ERROR,
'react/no-string-refs': SEVERITY_ERROR,
'react/no-unescaped-entities': SEVERITY_ERROR,
'react/no-unknown-property': SEVERITY_ERROR,
'react/no-unsafe': SEVERITY_OFF,
'react/prop-types': SEVERITY_ERROR,
'react/react-in-jsx-scope': SEVERITY_ERROR,
'react/require-render-return': SEVERITY_ERROR,
},
},
all: {
plugins,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
rules: activeRulesConfig,
},
'jsx-runtime': {
plugins,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
jsxPragma: null, // for @typescript/eslint-parser
},
rules: {
'react/react-in-jsx-scope': SEVERITY_OFF,
'react/jsx-uses-react': SEVERITY_OFF,
},
},
/**
* @deprecated Use reactPlugin.flatConfigs instead
*/
flat: /** @type {Record<string, ReactFlatConfig>} */ ({
__proto__: null,
}),
};
/** @typedef {{ plugins: { react: typeof plugin }, rules: import('eslint').Linter.RulesRecord, languageOptions: { parserOptions: import('eslint').Linter.ParserOptions } }} ReactFlatConfig */
/** @type {{ deprecatedRules: typeof deprecatedRules, rules: typeof allRules, configs: typeof configs & { flat: Record<string, ReactFlatConfig> }}} */
const plugin = {
deprecatedRules,
rules: allRules,
configs,
};
Object.assign(configs.flat, {
recommended: {
plugins: { react: plugin },
rules: configs.recommended.rules,
languageOptions: { parserOptions: configs.recommended.parserOptions },
},
all: {
plugins: { react: plugin },
rules: configs.all.rules,
languageOptions: { parserOptions: configs.all.parserOptions },
},
'jsx-runtime': {
plugins: { react: plugin },
rules: configs['jsx-runtime'].rules,
languageOptions: { parserOptions: configs['jsx-runtime'].parserOptions },
},
});
/** @type {{ meta: {name: string, version: string}, rules: typeof allRules}} */
const reactPlugin = {
meta: { name, version },
rules: allRules,
};
/** @typedef {'recommended' | 'all' | 'jsx-runtime'} AvailableFlatConfigs */
/** @type {Record<AvailableFlatConfigs, {name: string, plugins: { react: typeof reactPlugin }, rules: import('eslint').Linter.RulesRecord, languageOptions: { parserOptions: import('eslint').Linter.ParserOptions }}>} */
const flatConfigs = {
recommended: {
name: 'react/recommended',
plugins: { react: reactPlugin },
rules: configs.recommended.rules,
languageOptions: { parserOptions: configs.recommended.parserOptions },
},
all: {
name: 'react/all',
plugins: { react: reactPlugin },
rules: configs.all.rules,
languageOptions: { parserOptions: configs.all.parserOptions },
},
'jsx-runtime': {
name: 'react/jsx-runtime',
plugins: { react: reactPlugin },
rules: configs['jsx-runtime'].rules,
languageOptions: { parserOptions: configs['jsx-runtime'].parserOptions },
},
};
module.exports = Object.assign(plugin, { flatConfigs });