From cd3665a9a410f7706daa9a8b11eceff4de46b577 Mon Sep 17 00:00:00 2001 From: Henry Cai Date: Fri, 24 Jan 2025 16:01:22 -0800 Subject: [PATCH] fix(types): fix types of flat configs Fixes #3878 --- README.md | 16 ++++++++-------- index.js | 43 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 3c3cd24e3f..db4c98909a 100644 --- a/README.md +++ b/README.md @@ -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). @@ -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+ … ]; ``` @@ -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, @@ -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}'], diff --git a/index.js b/index.js index 64ff9bbdc4..e072fea902 100644 --- a/index.js +++ b/index.js @@ -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]))); } @@ -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); @@ -90,6 +91,9 @@ const configs = { 'react/jsx-uses-react': SEVERITY_OFF, }, }, + /** + * @deprecated Use reactPlugin.flatConfigs instead + */ flat: /** @type {Record} */ ({ __proto__: null, }), @@ -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} */ +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 });