From 31ef12a6becd39ef899773f3a0ab64cb0c50d5da Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Sat, 15 Jun 2024 19:59:08 +0200 Subject: [PATCH] [Fix] use same plugin instance in flat configs --- configs/all.js | 46 +++-------------------- configs/jsx-runtime.js | 21 ++++------- configs/recommended.js | 37 ++++--------------- index.js | 84 +++++++++++++++++++++++++++++++++++------- 4 files changed, 92 insertions(+), 96 deletions(-) diff --git a/configs/all.js b/configs/all.js index bcb416e295..1037bc8da1 100644 --- a/configs/all.js +++ b/configs/all.js @@ -1,49 +1,13 @@ 'use strict'; -const fromEntries = require('object.fromentries'); -const entries = require('object.entries'); +const plugin = require('../index'); -const allRules = require('../lib/rules'); - -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} - */ -function configureAsError(rules) { - return fromEntries(Object.keys(rules).map((key) => [`react/${key}`, 2])); -} - -const activeRules = filterRules(allRules, (rule) => !rule.meta.deprecated); -const activeRulesConfig = configureAsError(activeRules); - -const deprecatedRules = filterRules(allRules, (rule) => rule.meta.deprecated); +const legacyConfig = plugin.configs.all; module.exports = { - plugins: { - /** - * @type {{ - * deprecatedRules: Record, - * rules: Record, - * }} - */ - react: { - deprecatedRules, - rules: allRules, - }, - }, - rules: activeRulesConfig, - languageOptions: { - parserOptions: { - ecmaFeatures: { - jsx: true, - }, - }, - }, + plugins: { react: plugin }, + rules: legacyConfig.rules, + languageOptions: { parserOptions: legacyConfig.parserOptions }, }; -// this is so the `languageOptions` property won't be warned in the new config system Object.defineProperty(module.exports, 'languageOptions', { enumerable: false }); diff --git a/configs/jsx-runtime.js b/configs/jsx-runtime.js index a6302be504..764e7aed43 100644 --- a/configs/jsx-runtime.js +++ b/configs/jsx-runtime.js @@ -1,18 +1,13 @@ 'use strict'; -const all = require('./all'); +const plugin = require('../index'); -module.exports = Object.assign({}, all, { - languageOptions: Object.assign({}, all.languageOptions, { - parserOptions: Object.assign({}, all.languageOptions.parserOptions, { - jsxPragma: null, // for @typescript/eslint-parser - }), - }), - rules: { - 'react/react-in-jsx-scope': 0, - 'react/jsx-uses-react': 0, - }, -}); +const legacyConfig = plugin.configs['jsx-runtime']; + +module.exports = { + plugins: { react: plugin }, + rules: legacyConfig.rules, + languageOptions: { parserOptions: legacyConfig.parserOptions }, +}; -// this is so the `languageOptions` property won't be warned in the new config system Object.defineProperty(module.exports, 'languageOptions', { enumerable: false }); diff --git a/configs/recommended.js b/configs/recommended.js index 0bba09f7bb..3af34bac4a 100644 --- a/configs/recommended.js +++ b/configs/recommended.js @@ -1,34 +1,13 @@ 'use strict'; -const all = require('./all'); +const plugin = require('../index'); -module.exports = Object.assign({}, all, { - languageOptions: all.languageOptions, - rules: { - 'react/display-name': 2, - 'react/jsx-key': 2, - 'react/jsx-no-comment-textnodes': 2, - 'react/jsx-no-duplicate-props': 2, - 'react/jsx-no-target-blank': 2, - 'react/jsx-no-undef': 2, - 'react/jsx-uses-react': 2, - 'react/jsx-uses-vars': 2, - 'react/no-children-prop': 2, - 'react/no-danger-with-children': 2, - 'react/no-deprecated': 2, - 'react/no-direct-mutation-state': 2, - 'react/no-find-dom-node': 2, - 'react/no-is-mounted': 2, - 'react/no-render-return-value': 2, - 'react/no-string-refs': 2, - 'react/no-unescaped-entities': 2, - 'react/no-unknown-property': 2, - 'react/no-unsafe': 0, - 'react/prop-types': 2, - 'react/react-in-jsx-scope': 2, - 'react/require-render-return': 2, - }, -}); +const legacyConfig = plugin.configs.recommended; + +module.exports = { + plugins: { react: plugin }, + rules: legacyConfig.rules, + languageOptions: { parserOptions: legacyConfig.parserOptions }, +}; -// this is so the `languageOptions` property won't be warned in the new config system Object.defineProperty(module.exports, 'languageOptions', { enumerable: false }); diff --git a/index.js b/index.js index 4140c6c881..b0a083d75c 100644 --- a/index.js +++ b/index.js @@ -1,31 +1,89 @@ 'use strict'; -const configAll = require('./configs/all'); -const configRecommended = require('./configs/recommended'); -const configRuntime = require('./configs/jsx-runtime'); +const fromEntries = require('object.fromentries'); +const entries = require('object.entries'); const allRules = require('./lib/rules'); +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} + */ +function configureAsError(rules) { + return fromEntries(Object.keys(rules).map((key) => [`react/${key}`, 2])); +} + +const activeRules = filterRules(allRules, (rule) => !rule.meta.deprecated); +const activeRulesConfig = configureAsError(activeRules); + +const deprecatedRules = filterRules(allRules, (rule) => rule.meta.deprecated); + // for legacy config system const plugins = [ 'react', ]; module.exports = { - deprecatedRules: configAll.plugins.react.deprecatedRules, + deprecatedRules, rules: allRules, configs: { - recommended: Object.assign({}, configRecommended, { - parserOptions: configRecommended.languageOptions.parserOptions, + recommended: { plugins, - }), - all: Object.assign({}, configAll, { - parserOptions: configAll.languageOptions.parserOptions, + parserOptions: { + ecmaFeatures: { + jsx: true, + }, + }, + rules: { + 'react/display-name': 2, + 'react/jsx-key': 2, + 'react/jsx-no-comment-textnodes': 2, + 'react/jsx-no-duplicate-props': 2, + 'react/jsx-no-target-blank': 2, + 'react/jsx-no-undef': 2, + 'react/jsx-uses-react': 2, + 'react/jsx-uses-vars': 2, + 'react/no-children-prop': 2, + 'react/no-danger-with-children': 2, + 'react/no-deprecated': 2, + 'react/no-direct-mutation-state': 2, + 'react/no-find-dom-node': 2, + 'react/no-is-mounted': 2, + 'react/no-render-return-value': 2, + 'react/no-string-refs': 2, + 'react/no-unescaped-entities': 2, + 'react/no-unknown-property': 2, + 'react/no-unsafe': 0, + 'react/prop-types': 2, + 'react/react-in-jsx-scope': 2, + 'react/require-render-return': 2, + }, + }, + all: { plugins, - }), - 'jsx-runtime': Object.assign({}, configRuntime, { - parserOptions: configRuntime.languageOptions.parserOptions, + 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': 0, + 'react/jsx-uses-react': 0, + }, + }, }, };