From 1f8fdc88a7d36603bf8571d11a407290e4106327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Fri, 8 Sep 2023 01:32:17 -0500 Subject: [PATCH] feat: export flat/recommended-script and flat/recommended-module (#113) `eslint-plugin-n/configs/recommended-script` & `eslint-plugin-n/configs/recommended-module` are deprecated and may be removed in future majors. --- .eslint-doc-generatorrc.js | 13 ++ .eslintrc.js | 102 --------------- README.md | 123 ++++++++---------- configs/recommended-module.js | 12 +- configs/recommended-script.js | 12 +- docs/rules/no-deprecated-api.md | 2 +- docs/rules/no-exports-assign.md | 2 +- docs/rules/no-extraneous-import.md | 2 +- docs/rules/no-extraneous-require.md | 2 +- docs/rules/no-missing-import.md | 2 +- docs/rules/no-missing-require.md | 2 +- docs/rules/no-process-exit.md | 2 +- docs/rules/no-unpublished-bin.md | 2 +- docs/rules/no-unpublished-import.md | 2 +- docs/rules/no-unpublished-require.md | 2 +- .../no-unsupported-features/es-builtins.md | 2 +- .../no-unsupported-features/es-syntax.md | 2 +- .../no-unsupported-features/node-builtins.md | 2 +- docs/rules/process-exit-as-throw.md | 2 +- docs/rules/shebang.md | 2 +- lib/configs/recommended-module.js | 13 +- lib/configs/recommended-script.js | 13 +- lib/configs/recommended.js | 21 +-- lib/index.js | 111 ++++++++-------- package.json | 2 +- scripts/update.js | 2 +- tests/lib/configs/recommended.js | 9 ++ 27 files changed, 192 insertions(+), 271 deletions(-) create mode 100644 .eslint-doc-generatorrc.js delete mode 100644 .eslintrc.js diff --git a/.eslint-doc-generatorrc.js b/.eslint-doc-generatorrc.js new file mode 100644 index 00000000..157c8efa --- /dev/null +++ b/.eslint-doc-generatorrc.js @@ -0,0 +1,13 @@ +"use strict" +const config = { + urlConfigs: "https://github.com/eslint-community/eslint-plugin-n#-configs", + configEmoji: [ + ["recommended-script", "βœ…"], + ["recommended-module", "🟒"], + ["recommended", "β˜‘οΈ"], + ["flat/recommended-script", "βœ…"], + ["flat/recommended-module", "🟒"], + ], +} + +module.exports = config diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 564aca77..00000000 --- a/.eslintrc.js +++ /dev/null @@ -1,102 +0,0 @@ -/** - * @author Toru Nagashima - * @deprecated - * @description the file is no longer used, and will be removed in the future. - * See LICENSE file in root directory for full license. - */ -"use strict" - -// the config does not support eslint v8 yet, just replaced it with a simple config, -// to make the ci passing. -module.exports = { - reportUnusedDisableDirectives: true, - extends: [ - "eslint:recommended", - "plugin:n/recommended", - "plugin:eslint-plugin/recommended", - "prettier", - ], - env: { - mocha: true, - }, - rules: { - "eslint-plugin/require-meta-docs-description": "error", - }, - overrides: [ - { - // these messageIds were used outside - files: ["lib/rules/prefer-global/*.js"], - rules: { - "eslint-plugin/no-unused-message-ids": 0, - }, - }, - ], -} - -// const version = require("./package.json").version - -// module.exports = { -// extends: ["plugin:@mysticatea/es2015", "plugin:@mysticatea/+eslint-plugin"], -// rules: { -// "@mysticatea/eslint-plugin/test-case-property-ordering": [ -// "error", -// [ -// "filename", -// "code", -// "output", -// "options", -// "parser", -// "parserOptions", -// "globals", -// "env", -// "errors", -// ], -// ], -// }, -// overrides: [ -// { -// files: ["**/rules/*.js"], -// rules: { -// "@mysticatea/eslint-plugin/require-meta-docs-url": [ -// "error", -// { -// pattern: `https://github.com/mysticatea/eslint-plugin-node/blob/v${version}/docs/rules/{{name}}.md`, -// }, -// ], -// }, -// }, -// { -// files: ["**/rules/no-unsupported-features/*.js"], -// rules: { -// "@mysticatea/eslint-plugin/require-meta-docs-url": [ -// "error", -// { -// pattern: `https://github.com/mysticatea/eslint-plugin-node/blob/v${version}/docs/rules/no-unsupported-features/{{name}}.md`, -// }, -// ], -// }, -// }, -// { -// files: ["**/rules/prefer-global/*.js"], -// rules: { -// "@mysticatea/eslint-plugin/require-meta-docs-url": [ -// "error", -// { -// pattern: `https://github.com/mysticatea/eslint-plugin-node/blob/v${version}/docs/rules/prefer-global/{{name}}.md`, -// }, -// ], -// }, -// }, -// { -// files: ["**/rules/prefer-promises/*.js"], -// rules: { -// "@mysticatea/eslint-plugin/require-meta-docs-url": [ -// "error", -// { -// pattern: `https://github.com/mysticatea/eslint-plugin-node/blob/v${version}/docs/rules/prefer-promises/{{name}}.md`, -// }, -// ], -// }, -// }, -// ], -// } diff --git a/README.md b/README.md index 76740fb9..d5125fd3 100644 --- a/README.md +++ b/README.md @@ -34,10 +34,10 @@ npm install --save-dev eslint eslint-plugin-n ### [`eslint.config.js`](https://eslint.org/docs/latest/use/configure/configuration-files-new) (requires eslint>=v8.23.0) ```js -const nodeRecommendedScript = require("eslint-plugin-n/configs/recommended-script") +const nodePlugin = require("eslint-plugin-n") module.exports = [ - nodeRecommendedScript, + nodePlugin.configs["flat/recommended-script"], { rules: { "n/exports-style": ["error", "module.exports"] @@ -89,63 +89,55 @@ For [Shareable Configs](https://eslint.org/docs/latest/developer-guide/shareable πŸ’Ό [Configurations](https://github.com/eslint-community/eslint-plugin-n#-configs) enabled in.\ -βœ… Set in the `recommended` [configuration](https://github.com/eslint-community/eslint-plugin-n#-configs).\ +🟒 Set in the `flat/recommended-module` [configuration](https://github.com/eslint-community/eslint-plugin-n#-configs).\ +βœ… Set in the `flat/recommended-script` [configuration](https://github.com/eslint-community/eslint-plugin-n#-configs).\ +β˜‘οΈ Set in the `recommended` [configuration](https://github.com/eslint-community/eslint-plugin-n#-configs).\ +🟒 Set in the `recommended-module` [configuration](https://github.com/eslint-community/eslint-plugin-n#-configs).\ +βœ… Set in the `recommended-script` [configuration](https://github.com/eslint-community/eslint-plugin-n#-configs).\ πŸ”§ Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\ ❌ Deprecated. -### Best Practices - -| Name | Description | πŸ’Ό | πŸ”§ | ❌ | -| :--------------------------------------------------- | :----------------------- | :- | :- | :- | -| [no-deprecated-api](docs/rules/no-deprecated-api.md) | disallow deprecated APIs | βœ… | | | - -### Possible Errors - -| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  | Description | πŸ’Ό | πŸ”§ | ❌ | -| :------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------- | :- | :- | :- | -| [handle-callback-err](docs/rules/handle-callback-err.md) | require error handling in callbacks | | | | -| [no-callback-literal](docs/rules/no-callback-literal.md) | enforce Node.js-style error-first callback pattern is followed | | | | -| [no-exports-assign](docs/rules/no-exports-assign.md) | disallow the assignment to `exports` | βœ… | | | -| [no-extraneous-import](docs/rules/no-extraneous-import.md) | disallow `import` declarations which import extraneous modules | βœ… | | | -| [no-extraneous-require](docs/rules/no-extraneous-require.md) | disallow `require()` expressions which import extraneous modules | βœ… | | | -| [no-hide-core-modules](docs/rules/no-hide-core-modules.md) | disallow third-party modules which are hiding core modules | | | ❌ | -| [no-missing-import](docs/rules/no-missing-import.md) | disallow `import` declarations which import non-existence modules | βœ… | | | -| [no-missing-require](docs/rules/no-missing-require.md) | disallow `require()` expressions which import non-existence modules | βœ… | | | -| [no-new-require](docs/rules/no-new-require.md) | disallow `new` operators with calls to `require` | | | | -| [no-path-concat](docs/rules/no-path-concat.md) | disallow string concatenation with `__dirname` and `__filename` | | | | -| [no-process-exit](docs/rules/no-process-exit.md) | disallow the use of `process.exit()` | βœ… | | | -| [no-unpublished-bin](docs/rules/no-unpublished-bin.md) | disallow `bin` files that npm ignores | βœ… | | | -| [no-unpublished-import](docs/rules/no-unpublished-import.md) | disallow `import` declarations which import private modules | βœ… | | | -| [no-unpublished-require](docs/rules/no-unpublished-require.md) | disallow `require()` expressions which import private modules | βœ… | | | -| [no-unsupported-features](docs/rules/no-unsupported-features.md) | disallow unsupported ECMAScript features on the specified version | | | ❌ | -| [no-unsupported-features/es-builtins](docs/rules/no-unsupported-features/es-builtins.md) | disallow unsupported ECMAScript built-ins on the specified version | βœ… | | | -| [no-unsupported-features/es-syntax](docs/rules/no-unsupported-features/es-syntax.md) | disallow unsupported ECMAScript syntax on the specified version | βœ… | | | -| [no-unsupported-features/node-builtins](docs/rules/no-unsupported-features/node-builtins.md) | disallow unsupported Node.js built-in APIs on the specified version | βœ… | | | -| [process-exit-as-throw](docs/rules/process-exit-as-throw.md) | require that `process.exit()` expressions use the same code path as `throw` | βœ… | | | -| [shebang](docs/rules/shebang.md) | require correct usage of shebang | βœ… | πŸ”§ | | - -### Stylistic Issues - -| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  | Description | πŸ’Ό | πŸ”§ | ❌ | -| :------------------------------------------------------------------------------- | :---------------------------------------------------------------------- | :- | :- | :- | -| [callback-return](docs/rules/callback-return.md) | require `return` statements after callbacks | | | | -| [exports-style](docs/rules/exports-style.md) | enforce either `module.exports` or `exports` | | πŸ”§ | | -| [file-extension-in-import](docs/rules/file-extension-in-import.md) | enforce the style of file extensions in `import` declarations | | πŸ”§ | | -| [global-require](docs/rules/global-require.md) | require `require()` calls to be placed at top-level module scope | | | | -| [no-mixed-requires](docs/rules/no-mixed-requires.md) | disallow `require` calls to be mixed with regular variable declarations | | | | -| [no-process-env](docs/rules/no-process-env.md) | disallow the use of `process.env` | | | | -| [no-restricted-import](docs/rules/no-restricted-import.md) | disallow specified modules when loaded by `import` declarations | | | | -| [no-restricted-require](docs/rules/no-restricted-require.md) | disallow specified modules when loaded by `require` | | | | -| [no-sync](docs/rules/no-sync.md) | disallow synchronous methods | | | | -| [prefer-global/buffer](docs/rules/prefer-global/buffer.md) | enforce either `Buffer` or `require("buffer").Buffer` | | | | -| [prefer-global/console](docs/rules/prefer-global/console.md) | enforce either `console` or `require("console")` | | | | -| [prefer-global/process](docs/rules/prefer-global/process.md) | enforce either `process` or `require("process")` | | | | -| [prefer-global/text-decoder](docs/rules/prefer-global/text-decoder.md) | enforce either `TextDecoder` or `require("util").TextDecoder` | | | | -| [prefer-global/text-encoder](docs/rules/prefer-global/text-encoder.md) | enforce either `TextEncoder` or `require("util").TextEncoder` | | | | -| [prefer-global/url](docs/rules/prefer-global/url.md) | enforce either `URL` or `require("url").URL` | | | | -| [prefer-global/url-search-params](docs/rules/prefer-global/url-search-params.md) | enforce either `URLSearchParams` or `require("url").URLSearchParams` | | | | -| [prefer-promises/dns](docs/rules/prefer-promises/dns.md) | enforce `require("dns").promises` | | | | -| [prefer-promises/fs](docs/rules/prefer-promises/fs.md) | enforce `require("fs").promises` | | | | +| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  | Description | πŸ’Ό | πŸ”§ | ❌ | +| :------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------- | :----------- | :- | :- | +| [callback-return](docs/rules/callback-return.md) | require `return` statements after callbacks | | | | +| [exports-style](docs/rules/exports-style.md) | enforce either `module.exports` or `exports` | | πŸ”§ | | +| [file-extension-in-import](docs/rules/file-extension-in-import.md) | enforce the style of file extensions in `import` declarations | | πŸ”§ | | +| [global-require](docs/rules/global-require.md) | require `require()` calls to be placed at top-level module scope | | | | +| [handle-callback-err](docs/rules/handle-callback-err.md) | require error handling in callbacks | | | | +| [no-callback-literal](docs/rules/no-callback-literal.md) | enforce Node.js-style error-first callback pattern is followed | | | | +| [no-deprecated-api](docs/rules/no-deprecated-api.md) | disallow deprecated APIs | 🟒 βœ… β˜‘οΈ 🟒 βœ… | | | +| [no-exports-assign](docs/rules/no-exports-assign.md) | disallow the assignment to `exports` | 🟒 βœ… β˜‘οΈ 🟒 βœ… | | | +| [no-extraneous-import](docs/rules/no-extraneous-import.md) | disallow `import` declarations which import extraneous modules | 🟒 βœ… β˜‘οΈ 🟒 βœ… | | | +| [no-extraneous-require](docs/rules/no-extraneous-require.md) | disallow `require()` expressions which import extraneous modules | 🟒 βœ… β˜‘οΈ 🟒 βœ… | | | +| [no-hide-core-modules](docs/rules/no-hide-core-modules.md) | disallow third-party modules which are hiding core modules | | | ❌ | +| [no-missing-import](docs/rules/no-missing-import.md) | disallow `import` declarations which import non-existence modules | 🟒 βœ… β˜‘οΈ 🟒 βœ… | | | +| [no-missing-require](docs/rules/no-missing-require.md) | disallow `require()` expressions which import non-existence modules | 🟒 βœ… β˜‘οΈ 🟒 βœ… | | | +| [no-mixed-requires](docs/rules/no-mixed-requires.md) | disallow `require` calls to be mixed with regular variable declarations | | | | +| [no-new-require](docs/rules/no-new-require.md) | disallow `new` operators with calls to `require` | | | | +| [no-path-concat](docs/rules/no-path-concat.md) | disallow string concatenation with `__dirname` and `__filename` | | | | +| [no-process-env](docs/rules/no-process-env.md) | disallow the use of `process.env` | | | | +| [no-process-exit](docs/rules/no-process-exit.md) | disallow the use of `process.exit()` | 🟒 βœ… β˜‘οΈ 🟒 βœ… | | | +| [no-restricted-import](docs/rules/no-restricted-import.md) | disallow specified modules when loaded by `import` declarations | | | | +| [no-restricted-require](docs/rules/no-restricted-require.md) | disallow specified modules when loaded by `require` | | | | +| [no-sync](docs/rules/no-sync.md) | disallow synchronous methods | | | | +| [no-unpublished-bin](docs/rules/no-unpublished-bin.md) | disallow `bin` files that npm ignores | 🟒 βœ… β˜‘οΈ 🟒 βœ… | | | +| [no-unpublished-import](docs/rules/no-unpublished-import.md) | disallow `import` declarations which import private modules | 🟒 βœ… β˜‘οΈ 🟒 βœ… | | | +| [no-unpublished-require](docs/rules/no-unpublished-require.md) | disallow `require()` expressions which import private modules | 🟒 βœ… β˜‘οΈ 🟒 βœ… | | | +| [no-unsupported-features](docs/rules/no-unsupported-features.md) | disallow unsupported ECMAScript features on the specified version | | | ❌ | +| [no-unsupported-features/es-builtins](docs/rules/no-unsupported-features/es-builtins.md) | disallow unsupported ECMAScript built-ins on the specified version | 🟒 βœ… β˜‘οΈ 🟒 βœ… | | | +| [no-unsupported-features/es-syntax](docs/rules/no-unsupported-features/es-syntax.md) | disallow unsupported ECMAScript syntax on the specified version | 🟒 βœ… β˜‘οΈ 🟒 βœ… | | | +| [no-unsupported-features/node-builtins](docs/rules/no-unsupported-features/node-builtins.md) | disallow unsupported Node.js built-in APIs on the specified version | 🟒 βœ… β˜‘οΈ 🟒 βœ… | | | +| [prefer-global/buffer](docs/rules/prefer-global/buffer.md) | enforce either `Buffer` or `require("buffer").Buffer` | | | | +| [prefer-global/console](docs/rules/prefer-global/console.md) | enforce either `console` or `require("console")` | | | | +| [prefer-global/process](docs/rules/prefer-global/process.md) | enforce either `process` or `require("process")` | | | | +| [prefer-global/text-decoder](docs/rules/prefer-global/text-decoder.md) | enforce either `TextDecoder` or `require("util").TextDecoder` | | | | +| [prefer-global/text-encoder](docs/rules/prefer-global/text-encoder.md) | enforce either `TextEncoder` or `require("util").TextEncoder` | | | | +| [prefer-global/url](docs/rules/prefer-global/url.md) | enforce either `URL` or `require("url").URL` | | | | +| [prefer-global/url-search-params](docs/rules/prefer-global/url-search-params.md) | enforce either `URLSearchParams` or `require("url").URLSearchParams` | | | | +| [prefer-promises/dns](docs/rules/prefer-promises/dns.md) | enforce `require("dns").promises` | | | | +| [prefer-promises/fs](docs/rules/prefer-promises/fs.md) | enforce `require("fs").promises` | | | | +| [process-exit-as-throw](docs/rules/process-exit-as-throw.md) | require that `process.exit()` expressions use the same code path as `throw` | 🟒 βœ… β˜‘οΈ 🟒 βœ… | | | +| [shebang](docs/rules/shebang.md) | require correct usage of shebang | 🟒 βœ… β˜‘οΈ 🟒 βœ… | πŸ”§ | | @@ -172,22 +164,19 @@ These preset configs: - Q: The `no-missing-import` / `no-missing-require` rules don't work with nested folders in SublimeLinter-eslint - A: See [context.getFilename() in rule returns relative path](https://github.com/roadhump/SublimeLinter-eslint#contextgetfilename-in-rule-returns-relative-path) in the SublimeLinter-eslint FAQ. -- Q: How to use the new eslint config with mixed commonjs and es modules? -- A: The `recommended` config is no longer exported. You can create a config based on `recommended-script` and `recommended-module`. An example: +- Q: How to use the flat eslint config with mixed commonjs and es modules? +- A: You can use the new exported flat config `flat/mixed-esm-and-cjs`, an example: ```js -const nodeRecommendedScript = require("eslint-plugin-n/configs/recommended-script"); -const nodeRecommendedModule = require("eslint-plugin-n/configs/recommended-module"); +const nodePlugin = require("eslint-plugin-n"); module.exports = [ - { - files: ["**/*.js", "**/*.cjs"], - ...nodeRecommendedScript + ...nodePlugin.configs["flat/mixed-esm-and-cjs"], + { + rules: { + "n/exports-style": ["error", "module.exports"], }, - { - files: ["**/*.mjs"], - ...nodeRecommendedModule - } + }, ] ``` diff --git a/configs/recommended-module.js b/configs/recommended-module.js index 8c676422..bc49c77c 100644 --- a/configs/recommended-module.js +++ b/configs/recommended-module.js @@ -1,17 +1,9 @@ /** * @fileoverview the `recommended-module` config for `eslint.config.js` * @author ε”―η„Ά + * @deprecated use `flat/recommended-module` instead */ "use strict" -const mod = require("../lib/index.js") - -module.exports = { - plugins: { n: mod }, - languageOptions: { - sourceType: "module", - globals: mod.configs["recommended-module"].globals, - }, - rules: mod.configs["recommended-module"].rules, -} +module.exports = require("../lib/index.js").configs["flat/recommended-module"] diff --git a/configs/recommended-script.js b/configs/recommended-script.js index a6df853e..5464d7b9 100644 --- a/configs/recommended-script.js +++ b/configs/recommended-script.js @@ -1,17 +1,9 @@ /** * @fileoverview the `recommended-script` config for `eslint.config.js` * @author ε”―η„Ά + * @deprecated use `flat/recommended-script` instead */ "use strict" -const mod = require("../lib/index.js") - -module.exports = { - plugins: { n: mod }, - languageOptions: { - sourceType: "commonjs", - globals: mod.configs["recommended-script"].globals, - }, - rules: mod.configs["recommended-script"].rules, -} +module.exports = require("../lib/index.js").configs["flat/recommended-script"] diff --git a/docs/rules/no-deprecated-api.md b/docs/rules/no-deprecated-api.md index c67dd782..416f2d6e 100644 --- a/docs/rules/no-deprecated-api.md +++ b/docs/rules/no-deprecated-api.md @@ -1,6 +1,6 @@ # Disallow deprecated APIs (`n/no-deprecated-api`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-n#-configs). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-n#-configs): 🟒 `flat/recommended-module`, βœ… `flat/recommended-script`, β˜‘οΈ `recommended`, 🟒 `recommended-module`, βœ… `recommended-script`. diff --git a/docs/rules/no-exports-assign.md b/docs/rules/no-exports-assign.md index 891e1ebb..5524a915 100644 --- a/docs/rules/no-exports-assign.md +++ b/docs/rules/no-exports-assign.md @@ -1,6 +1,6 @@ # Disallow the assignment to `exports` (`n/no-exports-assign`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-n#-configs). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-n#-configs): 🟒 `flat/recommended-module`, βœ… `flat/recommended-script`, β˜‘οΈ `recommended`, 🟒 `recommended-module`, βœ… `recommended-script`. diff --git a/docs/rules/no-extraneous-import.md b/docs/rules/no-extraneous-import.md index 305b7e86..3f26daab 100644 --- a/docs/rules/no-extraneous-import.md +++ b/docs/rules/no-extraneous-import.md @@ -1,6 +1,6 @@ # Disallow `import` declarations which import extraneous modules (`n/no-extraneous-import`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-n#-configs). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-n#-configs): 🟒 `flat/recommended-module`, βœ… `flat/recommended-script`, β˜‘οΈ `recommended`, 🟒 `recommended-module`, βœ… `recommended-script`. diff --git a/docs/rules/no-extraneous-require.md b/docs/rules/no-extraneous-require.md index 0ca76395..1599c94d 100644 --- a/docs/rules/no-extraneous-require.md +++ b/docs/rules/no-extraneous-require.md @@ -1,6 +1,6 @@ # Disallow `require()` expressions which import extraneous modules (`n/no-extraneous-require`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-n#-configs). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-n#-configs): 🟒 `flat/recommended-module`, βœ… `flat/recommended-script`, β˜‘οΈ `recommended`, 🟒 `recommended-module`, βœ… `recommended-script`. diff --git a/docs/rules/no-missing-import.md b/docs/rules/no-missing-import.md index 3b4d9e7b..9ffdeec4 100644 --- a/docs/rules/no-missing-import.md +++ b/docs/rules/no-missing-import.md @@ -1,6 +1,6 @@ # Disallow `import` declarations which import non-existence modules (`n/no-missing-import`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-n#-configs). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-n#-configs): 🟒 `flat/recommended-module`, βœ… `flat/recommended-script`, β˜‘οΈ `recommended`, 🟒 `recommended-module`, βœ… `recommended-script`. diff --git a/docs/rules/no-missing-require.md b/docs/rules/no-missing-require.md index e45a2fbf..9b377ff0 100644 --- a/docs/rules/no-missing-require.md +++ b/docs/rules/no-missing-require.md @@ -1,6 +1,6 @@ # Disallow `require()` expressions which import non-existence modules (`n/no-missing-require`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-n#-configs). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-n#-configs): 🟒 `flat/recommended-module`, βœ… `flat/recommended-script`, β˜‘οΈ `recommended`, 🟒 `recommended-module`, βœ… `recommended-script`. diff --git a/docs/rules/no-process-exit.md b/docs/rules/no-process-exit.md index c00cfa74..816d9388 100644 --- a/docs/rules/no-process-exit.md +++ b/docs/rules/no-process-exit.md @@ -1,6 +1,6 @@ # Disallow the use of `process.exit()` (`n/no-process-exit`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-n#-configs). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-n#-configs): 🟒 `flat/recommended-module`, βœ… `flat/recommended-script`, β˜‘οΈ `recommended`, 🟒 `recommended-module`, βœ… `recommended-script`. diff --git a/docs/rules/no-unpublished-bin.md b/docs/rules/no-unpublished-bin.md index 4288b480..1b348d29 100644 --- a/docs/rules/no-unpublished-bin.md +++ b/docs/rules/no-unpublished-bin.md @@ -1,6 +1,6 @@ # Disallow `bin` files that npm ignores (`n/no-unpublished-bin`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-n#-configs). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-n#-configs): 🟒 `flat/recommended-module`, βœ… `flat/recommended-script`, β˜‘οΈ `recommended`, 🟒 `recommended-module`, βœ… `recommended-script`. diff --git a/docs/rules/no-unpublished-import.md b/docs/rules/no-unpublished-import.md index 114a305b..f0f93f68 100644 --- a/docs/rules/no-unpublished-import.md +++ b/docs/rules/no-unpublished-import.md @@ -1,6 +1,6 @@ # Disallow `import` declarations which import private modules (`n/no-unpublished-import`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-n#-configs). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-n#-configs): 🟒 `flat/recommended-module`, βœ… `flat/recommended-script`, β˜‘οΈ `recommended`, 🟒 `recommended-module`, βœ… `recommended-script`. diff --git a/docs/rules/no-unpublished-require.md b/docs/rules/no-unpublished-require.md index 84615ee8..7022cd10 100644 --- a/docs/rules/no-unpublished-require.md +++ b/docs/rules/no-unpublished-require.md @@ -1,6 +1,6 @@ # Disallow `require()` expressions which import private modules (`n/no-unpublished-require`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-n#-configs). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-n#-configs): 🟒 `flat/recommended-module`, βœ… `flat/recommended-script`, β˜‘οΈ `recommended`, 🟒 `recommended-module`, βœ… `recommended-script`. diff --git a/docs/rules/no-unsupported-features/es-builtins.md b/docs/rules/no-unsupported-features/es-builtins.md index 4c6e438f..786c9fad 100644 --- a/docs/rules/no-unsupported-features/es-builtins.md +++ b/docs/rules/no-unsupported-features/es-builtins.md @@ -1,6 +1,6 @@ # Disallow unsupported ECMAScript built-ins on the specified version (`n/no-unsupported-features/es-builtins`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-n#-configs). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-n#-configs): 🟒 `flat/recommended-module`, βœ… `flat/recommended-script`, β˜‘οΈ `recommended`, 🟒 `recommended-module`, βœ… `recommended-script`. diff --git a/docs/rules/no-unsupported-features/es-syntax.md b/docs/rules/no-unsupported-features/es-syntax.md index e4f51daa..41281851 100644 --- a/docs/rules/no-unsupported-features/es-syntax.md +++ b/docs/rules/no-unsupported-features/es-syntax.md @@ -1,6 +1,6 @@ # Disallow unsupported ECMAScript syntax on the specified version (`n/no-unsupported-features/es-syntax`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-n#-configs). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-n#-configs): 🟒 `flat/recommended-module`, βœ… `flat/recommended-script`, β˜‘οΈ `recommended`, 🟒 `recommended-module`, βœ… `recommended-script`. diff --git a/docs/rules/no-unsupported-features/node-builtins.md b/docs/rules/no-unsupported-features/node-builtins.md index a31dfc3d..87ff8bfb 100644 --- a/docs/rules/no-unsupported-features/node-builtins.md +++ b/docs/rules/no-unsupported-features/node-builtins.md @@ -1,6 +1,6 @@ # Disallow unsupported Node.js built-in APIs on the specified version (`n/no-unsupported-features/node-builtins`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-n#-configs). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-n#-configs): 🟒 `flat/recommended-module`, βœ… `flat/recommended-script`, β˜‘οΈ `recommended`, 🟒 `recommended-module`, βœ… `recommended-script`. diff --git a/docs/rules/process-exit-as-throw.md b/docs/rules/process-exit-as-throw.md index aaf0ef13..bac3dd8a 100644 --- a/docs/rules/process-exit-as-throw.md +++ b/docs/rules/process-exit-as-throw.md @@ -1,6 +1,6 @@ # Require that `process.exit()` expressions use the same code path as `throw` (`n/process-exit-as-throw`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-n#-configs). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-n#-configs): 🟒 `flat/recommended-module`, βœ… `flat/recommended-script`, β˜‘οΈ `recommended`, 🟒 `recommended-module`, βœ… `recommended-script`. diff --git a/docs/rules/shebang.md b/docs/rules/shebang.md index 63ab3639..f3fe13fd 100644 --- a/docs/rules/shebang.md +++ b/docs/rules/shebang.md @@ -1,6 +1,6 @@ # Require correct usage of shebang (`n/shebang`) -πŸ’Ό This rule is enabled in the βœ… `recommended` [config](https://github.com/eslint-community/eslint-plugin-n#-configs). +πŸ’Ό This rule is enabled in the following [configs](https://github.com/eslint-community/eslint-plugin-n#-configs): 🟒 `flat/recommended-module`, βœ… `flat/recommended-script`, β˜‘οΈ `recommended`, 🟒 `recommended-module`, βœ… `recommended-script`. πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). diff --git a/lib/configs/recommended-module.js b/lib/configs/recommended-module.js index d4988308..bb6fb0c0 100644 --- a/lib/configs/recommended-module.js +++ b/lib/configs/recommended-module.js @@ -2,7 +2,8 @@ const { commonGlobals, commonRules } = require("./_commons") -module.exports = { +// eslintrc config: https://eslint.org/docs/latest/use/configure/configuration-files +module.exports.eslintrc = { globals: { ...commonGlobals, __dirname: "off", @@ -16,7 +17,6 @@ module.exports = { ecmaVersion: 2021, sourceType: "module", }, - plugins: ["n"], rules: { ...commonRules, "n/no-unsupported-features/es-syntax": [ @@ -25,3 +25,12 @@ module.exports = { ], }, } + +// flat config: https://eslint.org/docs/latest/use/configure/configuration-files-new +module.exports.flat = { + languageOptions: { + sourceType: "module", + globals: module.exports.eslintrc.globals, + }, + rules: module.exports.eslintrc.rules, +} diff --git a/lib/configs/recommended-script.js b/lib/configs/recommended-script.js index cac6a29f..2d7f7c5c 100644 --- a/lib/configs/recommended-script.js +++ b/lib/configs/recommended-script.js @@ -2,7 +2,8 @@ const { commonGlobals, commonRules } = require("./_commons") -module.exports = { +// eslintrc config: https://eslint.org/docs/latest/use/configure/configuration-files +module.exports.eslintrc = { globals: { ...commonGlobals, __dirname: "readonly", @@ -16,9 +17,17 @@ module.exports = { ecmaVersion: 2021, sourceType: "script", }, - plugins: ["n"], rules: { ...commonRules, "n/no-unsupported-features/es-syntax": ["error", { ignores: [] }], }, } + +// https://eslint.org/docs/latest/use/configure/configuration-files-new +module.exports.flat = { + languageOptions: { + sourceType: "commonjs", + globals: module.exports.eslintrc.globals, + }, + rules: module.exports.eslintrc.rules, +} diff --git a/lib/configs/recommended.js b/lib/configs/recommended.js index 830c7662..f0cdf423 100644 --- a/lib/configs/recommended.js +++ b/lib/configs/recommended.js @@ -4,15 +4,16 @@ const getPackageJson = require("../util/get-package-json") const moduleConfig = require("./recommended-module") const scriptConfig = require("./recommended-script") -module.exports = () => { - const packageJson = getPackageJson() - const isModule = (packageJson && packageJson.type) === "module" +const packageJson = getPackageJson() +const isModule = (packageJson && packageJson.type) === "module" +const recommendedConfig = isModule ? moduleConfig : scriptConfig - return { - ...(isModule ? moduleConfig : scriptConfig), - overrides: [ - { files: ["*.cjs", ".*.cjs"], ...scriptConfig }, - { files: ["*.mjs", ".*.mjs"], ...moduleConfig }, - ], - } +module.exports.eslintrc = { + ...recommendedConfig.eslintrc, + overrides: [ + { files: ["*.cjs", ".*.cjs"], ...scriptConfig.eslintrc }, + { files: ["*.mjs", ".*.mjs"], ...moduleConfig.eslintrc }, + ], } + +module.exports.flat = recommendedConfig.flat diff --git a/lib/index.js b/lib/index.js index 341c86df..58c34925 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,61 +1,70 @@ -/* DON'T EDIT THIS FILE. This is generated by 'scripts/update-lib-index.js' */ "use strict" const pkg = require("../package.json") +const esmConfig = require("./configs/recommended-module") +const cjsConfig = require("./configs/recommended-script") +const recommendedConfig = require("./configs/recommended") -module.exports = { +const rules = { + "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-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"), + shebang: require("./rules/shebang"), + + // Deprecated rules. + "no-hide-core-modules": require("./rules/no-hide-core-modules"), + "no-unsupported-features": require("./rules/no-unsupported-features"), +} + +const mod = { meta: { name: pkg.name, version: pkg.version, }, - configs: { - "recommended-module": require("./configs/recommended-module"), - "recommended-script": require("./configs/recommended-script"), - get recommended() { - return require("./configs/recommended")() - }, - }, - rules: { - "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-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"), - shebang: require("./rules/shebang"), + rules, +} - // Deprecated rules. - "no-hide-core-modules": require("./rules/no-hide-core-modules"), - "no-unsupported-features": require("./rules/no-unsupported-features"), - }, +// set configs, e.g. mod.configs["recommended-module"] +// do not defined in the mod obj - to avoid circular dependency +mod.configs = { + "recommended-module": { plugins: ["n"], ...esmConfig.eslintrc }, + "recommended-script": { plugins: ["n"], ...cjsConfig.eslintrc }, + recommended: { plugins: ["n"], ...recommendedConfig.eslintrc }, + "flat/recommended-module": { plugins: { n: mod }, ...esmConfig.flat }, + "flat/recommended-script": { plugins: { n: mod }, ...cjsConfig.flat }, } + +module.exports = mod diff --git a/package.json b/package.json index a31616f2..cb2521a0 100644 --- a/package.json +++ b/package.json @@ -69,7 +69,7 @@ "test": "nyc npm run -s test:_mocha", "test:_mocha": "_mocha \"tests/lib/**/*.js\" --reporter progress --timeout 4000", "test:ci": "nyc npm run -s test:_mocha", - "update:eslint-docs": "eslint-doc-generator --config-emoji recommended-module,β˜‘οΈ --config-emoji recommended-script,βœ”οΈ --rule-list-split meta.docs.category --ignore-config recommended-module --ignore-config recommended-script --url-configs \"https://github.com/eslint-community/eslint-plugin-n#-configs\"", + "update:eslint-docs": "eslint-doc-generator", "version": "npm run -s build && eslint lib/rules --fix && git add .", "watch": "npm run test:_mocha -- --watch --growl" }, diff --git a/scripts/update.js b/scripts/update.js index 5d7366ee..f33909d8 100644 --- a/scripts/update.js +++ b/scripts/update.js @@ -5,4 +5,4 @@ "use strict" require("./update-rule-docs") -require("./update-lib-index") +// require("./update-lib-index") // TODO: the script needs to be updated. diff --git a/tests/lib/configs/recommended.js b/tests/lib/configs/recommended.js index 0111f475..44107d45 100644 --- a/tests/lib/configs/recommended.js +++ b/tests/lib/configs/recommended.js @@ -7,6 +7,13 @@ const { gtEslintV8 } = require("../../helpers") const originalCwd = process.cwd() +// this is needed as `recommended` config was cached +function clearRequireCache() { + for (const k in require.cache) { + delete require.cache[k] + } +} + describe("node/recommended config", () => { ;(gtEslintV8 ? describe : describe.skip)("in CJS directory", () => { const root = path.resolve(__dirname, "../../fixtures/configs/cjs/") @@ -16,6 +23,7 @@ describe("node/recommended config", () => { beforeEach(() => { process.chdir(root) + clearRequireCache() linter = new ESLint({ baseConfig: { extends: "plugin:n/recommended" }, useEslintrc: false, @@ -93,6 +101,7 @@ describe("node/recommended config", () => { beforeEach(() => { process.chdir(root) + clearRequireCache() linter = new ESLint({ baseConfig: { extends: "plugin:n/recommended" }, useEslintrc: false,