From 2e13d59baef7380a702a91f8c2dd2313ff83977c Mon Sep 17 00:00:00 2001 From: weiren Date: Mon, 23 Dec 2024 17:16:25 +0800 Subject: [PATCH] feat: add config "flat/all" open questions: 1. need to add an equivalent eslintrc config? fixes https://github.com/eslint-community/eslint-plugin-n/issues/400 --- .eslint-doc-generatorrc.js | 1 + lib/all-rules.js | 54 +++++++++++++++++++++++++++++++ lib/configs/_commons.js | 40 ++++++++++++----------- lib/configs/all.js | 14 ++++++++ lib/configs/recommended-module.js | 4 +-- lib/configs/recommended-script.js | 4 +-- lib/index.js | 4 +++ lib/rules/no-process-exit.js | 2 +- 8 files changed, 99 insertions(+), 24 deletions(-) create mode 100644 lib/all-rules.js create mode 100644 lib/configs/all.js diff --git a/.eslint-doc-generatorrc.js b/.eslint-doc-generatorrc.js index 42ba0a78..08357af1 100644 --- a/.eslint-doc-generatorrc.js +++ b/.eslint-doc-generatorrc.js @@ -8,6 +8,7 @@ const config = { "flat/recommended-module", "flat/recommended", "flat/mixed-esm-and-cjs", + "flat/all", ], urlConfigs: "https://github.com/eslint-community/eslint-plugin-n#-configs", configEmoji: [ diff --git a/lib/all-rules.js b/lib/all-rules.js new file mode 100644 index 00000000..de0016da --- /dev/null +++ b/lib/all-rules.js @@ -0,0 +1,54 @@ +/** + * @fileoverview All rules in the plugin. + * @author aladdin-add + * TODO: maybe auto-generated in the future? + */ +"use strict" + +/** @import { Rule } from 'eslint' */ + +/** @type {Record } */ +module.exports = { + "callback-return": require("./rules/callback-return"), + "exports-style": require("./rules/exports-style"), + "file-extension-in-import": require("./rules/file-extension-in-import"), + "global-require": require("./rules/global-require"), + "handle-callback-err": require("./rules/handle-callback-err"), + "no-callback-literal": require("./rules/no-callback-literal"), + "no-deprecated-api": require("./rules/no-deprecated-api"), + "no-exports-assign": require("./rules/no-exports-assign"), + "no-extraneous-import": require("./rules/no-extraneous-import"), + "no-extraneous-require": require("./rules/no-extraneous-require"), + "no-missing-import": require("./rules/no-missing-import"), + "no-missing-require": require("./rules/no-missing-require"), + "no-mixed-requires": require("./rules/no-mixed-requires"), + "no-new-require": require("./rules/no-new-require"), + "no-path-concat": require("./rules/no-path-concat"), + "no-process-env": require("./rules/no-process-env"), + "no-process-exit": require("./rules/no-process-exit"), + "no-restricted-import": require("./rules/no-restricted-import"), + "no-restricted-require": require("./rules/no-restricted-require"), + "no-sync": require("./rules/no-sync"), + "no-unpublished-bin": require("./rules/no-unpublished-bin"), + "no-unpublished-import": require("./rules/no-unpublished-import"), + "no-unpublished-require": require("./rules/no-unpublished-require"), + "no-unsupported-features/es-builtins": require("./rules/no-unsupported-features/es-builtins"), + "no-unsupported-features/es-syntax": require("./rules/no-unsupported-features/es-syntax"), + "no-unsupported-features/node-builtins": require("./rules/no-unsupported-features/node-builtins"), + "prefer-global/buffer": require("./rules/prefer-global/buffer"), + "prefer-global/console": require("./rules/prefer-global/console"), + "prefer-global/process": require("./rules/prefer-global/process"), + "prefer-global/text-decoder": require("./rules/prefer-global/text-decoder"), + "prefer-global/text-encoder": require("./rules/prefer-global/text-encoder"), + "prefer-global/url-search-params": require("./rules/prefer-global/url-search-params"), + "prefer-global/url": require("./rules/prefer-global/url"), + "prefer-node-protocol": require("./rules/prefer-node-protocol"), + "prefer-promises/dns": require("./rules/prefer-promises/dns"), + "prefer-promises/fs": require("./rules/prefer-promises/fs"), + "process-exit-as-throw": require("./rules/process-exit-as-throw"), + hashbang: require("./rules/hashbang"), + + // Deprecated rules. + "no-hide-core-modules": require("./rules/no-hide-core-modules"), + shebang: require("./rules/shebang"), +} diff --git a/lib/configs/_commons.js b/lib/configs/_commons.js index d9be6ef0..44349ca5 100644 --- a/lib/configs/_commons.js +++ b/lib/configs/_commons.js @@ -1,22 +1,24 @@ "use strict" -/** - * @type {import('eslint').Linter.RulesRecord} - */ -module.exports.commonRules = { - "n/no-deprecated-api": "error", - "n/no-extraneous-import": "error", - "n/no-extraneous-require": "error", - "n/no-exports-assign": "error", - "n/no-missing-import": "error", - "n/no-missing-require": "error", - "n/no-process-exit": "error", - "n/no-unpublished-bin": "error", - "n/no-unpublished-import": "error", - "n/no-unpublished-require": "error", - "n/no-unsupported-features/es-builtins": "error", - "n/no-unsupported-features/es-syntax": "error", - "n/no-unsupported-features/node-builtins": "error", - "n/process-exit-as-throw": "error", - "n/hashbang": "error", +const rules = require("../all-rules") + +/** @type {import('eslint').Linter.RulesRecord} */ +const recommendeRulesConfig = {} + +/** @type {import('eslint').Linter.RulesRecord} */ +const allRulesConfig = {} + +for (const [ruleName, rule] of Object.entries(rules)) { + const scopedRuleName = `n/${ruleName}` + // only non-deprecated rules + if (rule.meta?.deprecated !== true) { + allRulesConfig[scopedRuleName] = "error" + + if (rule.meta?.docs?.recommended === true) { + recommendeRulesConfig[scopedRuleName] = "error" + } + } } + +exports.recommendeRulesConfig = recommendeRulesConfig +exports.allRulesConfig = allRulesConfig diff --git a/lib/configs/all.js b/lib/configs/all.js new file mode 100644 index 00000000..086ab253 --- /dev/null +++ b/lib/configs/all.js @@ -0,0 +1,14 @@ +/** + * @fileoverview config for enabling all rules in the plugin. + * @author aladdin-addSS + */ +"use strict" + +const { allRulesConfig } = require("./_commons") +const recommendeConfig = require("./recommended") + +exports.flat = { + name: "node/flat/all", + languageOptions: recommendeConfig.flat.languageOptions, + rules: allRulesConfig, +} diff --git a/lib/configs/recommended-module.js b/lib/configs/recommended-module.js index 97fe573a..c8e15c02 100644 --- a/lib/configs/recommended-module.js +++ b/lib/configs/recommended-module.js @@ -1,7 +1,7 @@ "use strict" const globals = require("globals") -const { commonRules } = require("./_commons") +const { recommendeRulesConfig } = require("./_commons") /** * https://eslint.org/docs/latest/use/configure/configuration-files @@ -25,7 +25,7 @@ module.exports.eslintrc = { sourceType: "module", }, rules: { - ...commonRules, + ...recommendeRulesConfig, "n/no-unsupported-features/es-syntax": [ "error", { ignores: ["modules"] }, diff --git a/lib/configs/recommended-script.js b/lib/configs/recommended-script.js index 21120c2c..8c66cf01 100644 --- a/lib/configs/recommended-script.js +++ b/lib/configs/recommended-script.js @@ -1,7 +1,7 @@ "use strict" const globals = require("globals") -const { commonRules } = require("./_commons") +const { recommendeRulesConfig } = require("./_commons") /** * https://eslint.org/docs/latest/use/configure/configuration-files @@ -25,7 +25,7 @@ module.exports.eslintrc = { sourceType: "script", }, rules: { - ...commonRules, + ...recommendeRulesConfig, "n/no-unsupported-features/es-syntax": ["error", { ignores: [] }], }, } diff --git a/lib/index.js b/lib/index.js index de952189..8447011d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -4,6 +4,7 @@ const pkg = require("../package.json") const esmConfig = require("./configs/recommended-module") const cjsConfig = require("./configs/recommended-script") const recommendedConfig = require("./configs/recommended") +const allRulesConfig = require("./configs/all") /** @import { ESLint, Linter } from 'eslint' */ @@ -67,6 +68,7 @@ const base = { * 'flat/recommended-script': Linter.Config; * 'flat/recommended': Linter.Config; * 'flat/mixed-esm-and-cjs': Linter.Config[]; + * 'flat/all': Linter.Config; * }} Configs */ @@ -83,6 +85,8 @@ const configs = { { files: ["**/*.mjs"], plugins: { n: base }, ...esmConfig.flat }, { files: ["**/*.cjs"], plugins: { n: base }, ...cjsConfig.flat }, ], + // @ts-ignore -- tsc warns incompatible languageOptions, but it's fine + "flat/all": { plugins: { n: base }, ...allRulesConfig.flat }, } /** @type {ESLint.Plugin & { configs: Configs }} */ diff --git a/lib/rules/no-process-exit.js b/lib/rules/no-process-exit.js index c295aec2..c219fb34 100644 --- a/lib/rules/no-process-exit.js +++ b/lib/rules/no-process-exit.js @@ -17,7 +17,7 @@ module.exports = { type: "suggestion", docs: { description: "disallow the use of `process.exit()`", - recommended: false, + recommended: true, url: "https://github.com/eslint-community/eslint-plugin-n/blob/HEAD/docs/rules/no-process-exit.md", }, fixable: null,