Skip to content

Commit

Permalink
feat: support eslint.config.js (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed May 15, 2023
1 parent bea9981 commit ebc97bf
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 22 deletions.
7 changes: 0 additions & 7 deletions .eslintignore

This file was deleted.

2 changes: 2 additions & 0 deletions .eslintrc.js
@@ -1,5 +1,7 @@
/**
* @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"
Expand Down
48 changes: 38 additions & 10 deletions README.md
Expand Up @@ -15,7 +15,9 @@ npm install --save-dev eslint eslint-plugin-n
- Requires Node.js `>=16.0.0`
- Requires ESLint `>=7.0.0`

**.eslintrc.json** (An example)
**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.

### **[.eslintrc.json](https://eslint.org/docs/latest/use/configure/configuration-files)** (An example)

```jsonc
{
Expand All @@ -24,19 +26,26 @@ npm install --save-dev eslint eslint-plugin-n
"ecmaVersion": 2021
},
"rules": {
"n/exports-style": ["error", "module.exports"],
"n/file-extension-in-import": ["error", "always"],
"n/prefer-global/buffer": ["error", "always"],
"n/prefer-global/console": ["error", "always"],
"n/prefer-global/process": ["error", "always"],
"n/prefer-global/url-search-params": ["error", "always"],
"n/prefer-global/url": ["error", "always"],
"n/prefer-promises/dns": "error",
"n/prefer-promises/fs": "error"
"n/exports-style": ["error", "module.exports"]
}
}
```

### [`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")

module.exports = [
nodeRecommendedScript,
{
rules: {
"n/exports-style": ["error", "module.exports"]
}
}
]
```

**package.json** (An example)

```json
Expand Down Expand Up @@ -163,6 +172,25 @@ 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:

```js
const nodeRecommendedScript = require("eslint-plugin-n/configs/recommended-script");
const nodeRecommendedModule = require("eslint-plugin-n/configs/recommended-module");

module.exports = [
{
files: ["**/*.js", "**/*.cjs"],
...nodeRecommendedScript
},
{
files: ["**/*.mjs"],
...nodeRecommendedModule
}
]
```

## 🚥 Semantic Versioning Policy

`eslint-plugin-n` follows [semantic versioning](http://semver.org/) and [ESLint's Semantic Versioning Policy](https://github.com/eslint/eslint#semantic-versioning-policy).
Expand Down
17 changes: 17 additions & 0 deletions configs/recommended-module.js
@@ -0,0 +1,17 @@
/**
* @fileoverview the `recommended-module` config for `eslint.config.js`
* @author 唯然<weiran.zsd@outlook.com>
*/

"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,
}
17 changes: 17 additions & 0 deletions configs/recommended-script.js
@@ -0,0 +1,17 @@
/**
* @fileoverview the `recommended-script` config for `eslint.config.js`
* @author 唯然<weiran.zsd@outlook.com>
*/

"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,
}
41 changes: 41 additions & 0 deletions eslint.config.js
@@ -0,0 +1,41 @@
/**
* @author 唯然<weiran.zsd@outlook.com>
*/
"use strict"

const js = require("@eslint/js")
const { FlatCompat } = require("@eslint/eslintrc")
const globals = require("globals")
const nodeRecommended = require("eslint-plugin-n/configs/recommended-script")

const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
})

module.exports = [
{
languageOptions: { globals: globals.mocha },
linterOptions: { reportUnusedDisableDirectives: true },
},
{
ignores: [
".nyc_output/",
"coverage/",
"docs/",
"lib/converted-esm/",
"test/fixtures/",
],
},
js.configs.recommended,
nodeRecommended,
...compat.extends("plugin:eslint-plugin/recommended", "prettier"),
{ rules: { "eslint-plugin/require-meta-docs-description": "error" } },
{
// these messageIds were used outside
files: ["lib/rules/prefer-global/*.js"],
rules: {
"eslint-plugin/no-unused-message-ids": 0,
},
},
]
6 changes: 6 additions & 0 deletions lib/index.js
@@ -1,7 +1,13 @@
/* DON'T EDIT THIS FILE. This is generated by 'scripts/update-lib-index.js' */
"use strict"

const pkg = require("../package.json")

module.exports = {
meta: {
name: pkg.name,
version: pkg.version,
},
configs: {
"recommended-module": require("./configs/recommended-module"),
"recommended-script": require("./configs/recommended-script"),
Expand Down
2 changes: 1 addition & 1 deletion lib/util/check-unsupported-builtins.js
Expand Up @@ -4,7 +4,7 @@
*/
"use strict"

const { Range, lt, major } = require("semver") //eslint-disable-line no-unused-vars
const { Range, lt, major } = require("semver") // eslint-disable-line no-unused-vars
const { ReferenceTracker } = require("@eslint-community/eslint-utils")
const getConfiguredNodeVersion = require("./get-configured-node-version")
const getSemverRange = require("./get-semver-range")
Expand Down
2 changes: 1 addition & 1 deletion lib/util/get-configured-node-version.js
Expand Up @@ -4,7 +4,7 @@
*/
"use strict"

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

Expand Down
9 changes: 6 additions & 3 deletions package.json
Expand Up @@ -7,22 +7,25 @@
},
"main": "lib/index.js",
"files": [
"lib"
"lib/",
"configs/"
],
"peerDependencies": {
"eslint": ">=7.0.0"
},
"dependencies": {
"@eslint-community/eslint-utils": "^4.4.0",
"builtins": "^5.0.1",
"eslint-plugin-es-x": "^6.1.0",
"@eslint-community/eslint-utils": "^4.4.0",
"ignore": "^5.1.1",
"is-core-module": "^2.12.0",
"minimatch": "^3.1.2",
"resolve": "^1.22.2",
"semver": "^7.5.0"
},
"devDependencies": {
"@eslint/eslintrc": "^2.0.2",
"@eslint/js": "^8.38.0",
"@typescript-eslint/parser": "^5.59.0",
"codecov": "^3.3.0",
"esbuild": "^0.17.17",
Expand Down Expand Up @@ -56,7 +59,7 @@
"lint": "npm-run-all \"lint:*\"",
"lint:docs": "markdownlint \"**/*.md\"",
"lint:eslint-docs": "npm run update:eslint-docs -- --check",
"lint:js": "eslint lib scripts tests/lib .eslintrc.js",
"lint:js": "eslint .",
"new": "node scripts/new-rule",
"postversion": "git push && git push --tags",
"prepare": "npx husky install",
Expand Down
6 changes: 6 additions & 0 deletions scripts/update-lib-index.js
Expand Up @@ -13,7 +13,13 @@ const filePath = path.resolve(__dirname, "../lib/index.js")
const rawContent = `/* DON'T EDIT THIS FILE. This is generated by 'scripts/update-lib-index.js' */
"use strict"
const pkg = require("../package.json")
module.exports = {
meta: {
name: pkg.name,
version: pkg.version,
},
configs: {
"recommended-module": require("./configs/recommended-module"),
"recommended-script": require("./configs/recommended-script"),
Expand Down

0 comments on commit ebc97bf

Please sign in to comment.