Skip to content

Commit ebc97bf

Browse files
authored
feat: support eslint.config.js (#95)
1 parent bea9981 commit ebc97bf

11 files changed

+135
-22
lines changed

.eslintignore

-7
This file was deleted.

.eslintrc.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/**
22
* @author Toru Nagashima
3+
* @deprecated
4+
* @description the file is no longer used, and will be removed in the future.
35
* See LICENSE file in root directory for full license.
46
*/
57
"use strict"

README.md

+38-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ npm install --save-dev eslint eslint-plugin-n
1515
- Requires Node.js `>=16.0.0`
1616
- Requires ESLint `>=7.0.0`
1717

18-
**.eslintrc.json** (An example)
18+
**Note:** It recommends a use of [the "engines" field of package.json](https://docs.npmjs.com/files/package.json#engines). The "engines" field is used by `n/no-unsupported-features/*` rules.
19+
20+
### **[.eslintrc.json](https://eslint.org/docs/latest/use/configure/configuration-files)** (An example)
1921

2022
```jsonc
2123
{
@@ -24,19 +26,26 @@ npm install --save-dev eslint eslint-plugin-n
2426
"ecmaVersion": 2021
2527
},
2628
"rules": {
27-
"n/exports-style": ["error", "module.exports"],
28-
"n/file-extension-in-import": ["error", "always"],
29-
"n/prefer-global/buffer": ["error", "always"],
30-
"n/prefer-global/console": ["error", "always"],
31-
"n/prefer-global/process": ["error", "always"],
32-
"n/prefer-global/url-search-params": ["error", "always"],
33-
"n/prefer-global/url": ["error", "always"],
34-
"n/prefer-promises/dns": "error",
35-
"n/prefer-promises/fs": "error"
29+
"n/exports-style": ["error", "module.exports"]
3630
}
3731
}
3832
```
3933

34+
### [`eslint.config.js`](https://eslint.org/docs/latest/use/configure/configuration-files-new) (requires eslint>=v8.23.0)
35+
36+
```js
37+
const nodeRecommendedScript = require("eslint-plugin-n/configs/recommended-script")
38+
39+
module.exports = [
40+
nodeRecommendedScript,
41+
{
42+
rules: {
43+
"n/exports-style": ["error", "module.exports"]
44+
}
45+
}
46+
]
47+
```
48+
4049
**package.json** (An example)
4150

4251
```json
@@ -163,6 +172,25 @@ These preset configs:
163172
- Q: The `no-missing-import` / `no-missing-require` rules don't work with nested folders in SublimeLinter-eslint
164173
- 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.
165174

175+
- Q: How to use the new eslint config with mixed commonjs and es modules?
176+
- A: The `recommended` config is no longer exported. You can create a config based on `recommended-script` and `recommended-module`. An example:
177+
178+
```js
179+
const nodeRecommendedScript = require("eslint-plugin-n/configs/recommended-script");
180+
const nodeRecommendedModule = require("eslint-plugin-n/configs/recommended-module");
181+
182+
module.exports = [
183+
{
184+
files: ["**/*.js", "**/*.cjs"],
185+
...nodeRecommendedScript
186+
},
187+
{
188+
files: ["**/*.mjs"],
189+
...nodeRecommendedModule
190+
}
191+
]
192+
```
193+
166194
## 🚥 Semantic Versioning Policy
167195

168196
`eslint-plugin-n` follows [semantic versioning](http://semver.org/) and [ESLint's Semantic Versioning Policy](https://github.com/eslint/eslint#semantic-versioning-policy).

configs/recommended-module.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @fileoverview the `recommended-module` config for `eslint.config.js`
3+
* @author 唯然<[email protected]>
4+
*/
5+
6+
"use strict"
7+
8+
const mod = require("../lib/index.js")
9+
10+
module.exports = {
11+
plugins: { n: mod },
12+
languageOptions: {
13+
sourceType: "module",
14+
globals: mod.configs["recommended-module"].globals,
15+
},
16+
rules: mod.configs["recommended-module"].rules,
17+
}

configs/recommended-script.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @fileoverview the `recommended-script` config for `eslint.config.js`
3+
* @author 唯然<[email protected]>
4+
*/
5+
6+
"use strict"
7+
8+
const mod = require("../lib/index.js")
9+
10+
module.exports = {
11+
plugins: { n: mod },
12+
languageOptions: {
13+
sourceType: "commonjs",
14+
globals: mod.configs["recommended-script"].globals,
15+
},
16+
rules: mod.configs["recommended-script"].rules,
17+
}

eslint.config.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @author 唯然<[email protected]>
3+
*/
4+
"use strict"
5+
6+
const js = require("@eslint/js")
7+
const { FlatCompat } = require("@eslint/eslintrc")
8+
const globals = require("globals")
9+
const nodeRecommended = require("eslint-plugin-n/configs/recommended-script")
10+
11+
const compat = new FlatCompat({
12+
baseDirectory: __dirname,
13+
recommendedConfig: js.configs.recommended,
14+
})
15+
16+
module.exports = [
17+
{
18+
languageOptions: { globals: globals.mocha },
19+
linterOptions: { reportUnusedDisableDirectives: true },
20+
},
21+
{
22+
ignores: [
23+
".nyc_output/",
24+
"coverage/",
25+
"docs/",
26+
"lib/converted-esm/",
27+
"test/fixtures/",
28+
],
29+
},
30+
js.configs.recommended,
31+
nodeRecommended,
32+
...compat.extends("plugin:eslint-plugin/recommended", "prettier"),
33+
{ rules: { "eslint-plugin/require-meta-docs-description": "error" } },
34+
{
35+
// these messageIds were used outside
36+
files: ["lib/rules/prefer-global/*.js"],
37+
rules: {
38+
"eslint-plugin/no-unused-message-ids": 0,
39+
},
40+
},
41+
]

lib/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
/* DON'T EDIT THIS FILE. This is generated by 'scripts/update-lib-index.js' */
22
"use strict"
33

4+
const pkg = require("../package.json")
5+
46
module.exports = {
7+
meta: {
8+
name: pkg.name,
9+
version: pkg.version,
10+
},
511
configs: {
612
"recommended-module": require("./configs/recommended-module"),
713
"recommended-script": require("./configs/recommended-script"),

lib/util/check-unsupported-builtins.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
"use strict"
66

7-
const { Range, lt, major } = require("semver") //eslint-disable-line no-unused-vars
7+
const { Range, lt, major } = require("semver") // eslint-disable-line no-unused-vars
88
const { ReferenceTracker } = require("@eslint-community/eslint-utils")
99
const getConfiguredNodeVersion = require("./get-configured-node-version")
1010
const getSemverRange = require("./get-semver-range")

lib/util/get-configured-node-version.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
"use strict"
66

7-
const { Range } = require("semver") //eslint-disable-line no-unused-vars
7+
const { Range } = require("semver") // eslint-disable-line no-unused-vars
88
const getPackageJson = require("./get-package-json")
99
const getSemverRange = require("./get-semver-range")
1010

package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,25 @@
77
},
88
"main": "lib/index.js",
99
"files": [
10-
"lib"
10+
"lib/",
11+
"configs/"
1112
],
1213
"peerDependencies": {
1314
"eslint": ">=7.0.0"
1415
},
1516
"dependencies": {
17+
"@eslint-community/eslint-utils": "^4.4.0",
1618
"builtins": "^5.0.1",
1719
"eslint-plugin-es-x": "^6.1.0",
18-
"@eslint-community/eslint-utils": "^4.4.0",
1920
"ignore": "^5.1.1",
2021
"is-core-module": "^2.12.0",
2122
"minimatch": "^3.1.2",
2223
"resolve": "^1.22.2",
2324
"semver": "^7.5.0"
2425
},
2526
"devDependencies": {
27+
"@eslint/eslintrc": "^2.0.2",
28+
"@eslint/js": "^8.38.0",
2629
"@typescript-eslint/parser": "^5.59.0",
2730
"codecov": "^3.3.0",
2831
"esbuild": "^0.17.17",
@@ -56,7 +59,7 @@
5659
"lint": "npm-run-all \"lint:*\"",
5760
"lint:docs": "markdownlint \"**/*.md\"",
5861
"lint:eslint-docs": "npm run update:eslint-docs -- --check",
59-
"lint:js": "eslint lib scripts tests/lib .eslintrc.js",
62+
"lint:js": "eslint .",
6063
"new": "node scripts/new-rule",
6164
"postversion": "git push && git push --tags",
6265
"prepare": "npx husky install",

scripts/update-lib-index.js

+6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ const filePath = path.resolve(__dirname, "../lib/index.js")
1313
const rawContent = `/* DON'T EDIT THIS FILE. This is generated by 'scripts/update-lib-index.js' */
1414
"use strict"
1515
16+
const pkg = require("../package.json")
17+
1618
module.exports = {
19+
meta: {
20+
name: pkg.name,
21+
version: pkg.version,
22+
},
1723
configs: {
1824
"recommended-module": require("./configs/recommended-module"),
1925
"recommended-script": require("./configs/recommended-script"),

0 commit comments

Comments
 (0)