Skip to content

Commit

Permalink
fix(types): fix types of flat configs
Browse files Browse the repository at this point in the history
Fixes #3878
  • Loading branch information
CHC383 committed Jan 25, 2025
1 parent e6b5b41 commit cd3665a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ The schema of the `settings.react` object would be identical to that of what's a

This plugin exports 3 flat configs:

- `flat.all`
- `flat.recommended`
- `flat['jsx-runtime']`
- `flatConfigs.all`
- `flatConfigs.recommended`
- `flatConfigs['jsx-runtime']`

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).

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

module.exports = [
reactPlugin.configs.flat.recommended, // This is not a plugin object, but a shareable config object
reactPlugin.configs.flat['jsx-runtime'], // Add this if you are using React 17+
reactPlugin.flatConfigs.recommended, // This is not a plugin object, but a shareable config object
reactPlugin.flatConfigs['jsx-runtime'], // Add this if you are using React 17+
];
```
Expand All @@ -239,9 +239,9 @@ module.exports = [
{
files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
...reactPlugin.configs.flat.recommended,
...reactPlugin.flatConfigs.recommended,
languageOptions: {
...reactPlugin.configs.flat.recommended.languageOptions,
...reactPlugin.flatConfigs.recommended.languageOptions,
globals: {
...globals.serviceworker,
...globals.browser,
Expand All @@ -262,7 +262,7 @@ module.exports = [
{
files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
...reactPlugin.configs.flat.recommended,
...reactPlugin.flatConfigs.recommended,
},
{
files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
Expand Down
43 changes: 38 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

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])));
}
Expand All @@ -27,9 +30,7 @@ const deprecatedRules = filterRules(allRules, (rule) => rule.meta.deprecated);

/** @type {['react']} */
// for legacy config system
const plugins = [
'react',
];
const plugins = ['react'];

// TODO: with TS 4.5+, inline this
const SEVERITY_ERROR = /** @type {2} */ (2);
Expand Down Expand Up @@ -90,6 +91,9 @@ const configs = {
'react/jsx-uses-react': SEVERITY_OFF,
},
},
/**
* @deprecated Use reactPlugin.flatConfigs instead
*/
flat: /** @type {Record<string, ReactFlatConfig>} */ ({
__proto__: null,
}),
Expand Down Expand Up @@ -122,4 +126,33 @@ Object.assign(configs.flat, {
},
});

module.exports = plugin;
/** @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 });

0 comments on commit cd3665a

Please sign in to comment.