Skip to content

Commit

Permalink
feat: add config "flat/all"
Browse files Browse the repository at this point in the history
open questions:

1. need to add an equivalent eslintrc config?

fixes #400
  • Loading branch information
aladdin-add committed Dec 24, 2024
1 parent 86a5242 commit 2e13d59
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 24 deletions.
1 change: 1 addition & 0 deletions .eslint-doc-generatorrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
54 changes: 54 additions & 0 deletions lib/all-rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @fileoverview All rules in the plugin.
* @author aladdin-add<[email protected]>
* TODO: maybe auto-generated in the future?
*/
"use strict"

/** @import { Rule } from 'eslint' */

/** @type {Record<string, Rule.RuleModule> } */
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"),
}
40 changes: 21 additions & 19 deletions lib/configs/_commons.js
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions lib/configs/all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @fileoverview config for enabling all rules in the plugin.
* @author aladdin-add<[email protected]>SS
*/
"use strict"

const { allRulesConfig } = require("./_commons")
const recommendeConfig = require("./recommended")

exports.flat = {
name: "node/flat/all",
languageOptions: recommendeConfig.flat.languageOptions,
rules: allRulesConfig,
}
4 changes: 2 additions & 2 deletions lib/configs/recommended-module.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,7 +25,7 @@ module.exports.eslintrc = {
sourceType: "module",
},
rules: {
...commonRules,
...recommendeRulesConfig,
"n/no-unsupported-features/es-syntax": [
"error",
{ ignores: ["modules"] },
Expand Down
4 changes: 2 additions & 2 deletions lib/configs/recommended-script.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,7 +25,7 @@ module.exports.eslintrc = {
sourceType: "script",
},
rules: {
...commonRules,
...recommendeRulesConfig,
"n/no-unsupported-features/es-syntax": ["error", { ignores: [] }],
},
}
Expand Down
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' */

Expand Down Expand Up @@ -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
*/

Expand All @@ -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 }} */
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-process-exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 2e13d59

Please sign in to comment.