Skip to content

Commit cd3665a

Browse files
committed
fix(types): fix types of flat configs
Fixes jsx-eslint#3878
1 parent e6b5b41 commit cd3665a

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ The schema of the `settings.react` object would be identical to that of what's a
209209

210210
This plugin exports 3 flat configs:
211211

212-
- `flat.all`
213-
- `flat.recommended`
214-
- `flat['jsx-runtime']`
212+
- `flatConfigs.all`
213+
- `flatConfigs.recommended`
214+
- `flatConfigs['jsx-runtime']`
215215

216216
The flat configs are available via the root plugin import. They will configure the plugin under the `react/` namespace and enable JSX in [`languageOptions.parserOptions`](https://eslint.org/docs/latest/use/configure/language-options#specifying-parser-options).
217217

@@ -220,8 +220,8 @@ const reactPlugin = require('eslint-plugin-react');
220220

221221
module.exports = [
222222
223-
reactPlugin.configs.flat.recommended, // This is not a plugin object, but a shareable config object
224-
reactPlugin.configs.flat['jsx-runtime'], // Add this if you are using React 17+
223+
reactPlugin.flatConfigs.recommended, // This is not a plugin object, but a shareable config object
224+
reactPlugin.flatConfigs['jsx-runtime'], // Add this if you are using React 17+
225225
226226
];
227227
```
@@ -239,9 +239,9 @@ module.exports = [
239239
240240
{
241241
files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
242-
...reactPlugin.configs.flat.recommended,
242+
...reactPlugin.flatConfigs.recommended,
243243
languageOptions: {
244-
...reactPlugin.configs.flat.recommended.languageOptions,
244+
...reactPlugin.flatConfigs.recommended.languageOptions,
245245
globals: {
246246
...globals.serviceworker,
247247
...globals.browser,
@@ -262,7 +262,7 @@ module.exports = [
262262
263263
{
264264
files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
265-
...reactPlugin.configs.flat.recommended,
265+
...reactPlugin.flatConfigs.recommended,
266266
},
267267
{
268268
files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],

index.js

+38-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
const fromEntries = require('object.fromentries');
44
const entries = require('object.entries');
5-
5+
const packageMeta = require('./package.json');
66
const allRules = require('./lib/rules');
77

8+
const name = packageMeta.name;
9+
const version = packageMeta.version;
10+
811
function filterRules(rules, predicate) {
912
return fromEntries(entries(rules).filter((entry) => predicate(entry[1])));
1013
}
@@ -27,9 +30,7 @@ const deprecatedRules = filterRules(allRules, (rule) => rule.meta.deprecated);
2730

2831
/** @type {['react']} */
2932
// for legacy config system
30-
const plugins = [
31-
'react',
32-
];
33+
const plugins = ['react'];
3334

3435
// TODO: with TS 4.5+, inline this
3536
const SEVERITY_ERROR = /** @type {2} */ (2);
@@ -90,6 +91,9 @@ const configs = {
9091
'react/jsx-uses-react': SEVERITY_OFF,
9192
},
9293
},
94+
/**
95+
* @deprecated Use reactPlugin.flatConfigs instead
96+
*/
9397
flat: /** @type {Record<string, ReactFlatConfig>} */ ({
9498
__proto__: null,
9599
}),
@@ -122,4 +126,33 @@ Object.assign(configs.flat, {
122126
},
123127
});
124128

125-
module.exports = plugin;
129+
/** @type {{ meta: {name: string, version: string}, rules: typeof allRules}} */
130+
const reactPlugin = {
131+
meta: { name, version },
132+
rules: allRules,
133+
};
134+
135+
/** @typedef {'recommended' | 'all' | 'jsx-runtime'} AvailableFlatConfigs */
136+
/** @type {Record<AvailableFlatConfigs, {name: string, plugins: { react: typeof reactPlugin }, rules: import('eslint').Linter.RulesRecord, languageOptions: { parserOptions: import('eslint').Linter.ParserOptions }}>} */
137+
const flatConfigs = {
138+
recommended: {
139+
name: 'react/recommended',
140+
plugins: { react: reactPlugin },
141+
rules: configs.recommended.rules,
142+
languageOptions: { parserOptions: configs.recommended.parserOptions },
143+
},
144+
all: {
145+
name: 'react/all',
146+
plugins: { react: reactPlugin },
147+
rules: configs.all.rules,
148+
languageOptions: { parserOptions: configs.all.parserOptions },
149+
},
150+
'jsx-runtime': {
151+
name: 'react/jsx-runtime',
152+
plugins: { react: reactPlugin },
153+
rules: configs['jsx-runtime'].rules,
154+
languageOptions: { parserOptions: configs['jsx-runtime'].parserOptions },
155+
},
156+
};
157+
158+
module.exports = Object.assign(plugin, { flatConfigs });

0 commit comments

Comments
 (0)