Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support eslint.config.js #95

Merged
merged 13 commits into from
May 15, 2023
7 changes: 0 additions & 7 deletions .eslintignore

This file was deleted.

2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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"]
}
}
]
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there should also be an example with configs/recommended-module, for ESM ("type": "module") projects?


**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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @fileoverview the `recommended-module` config for `eslint.config.js`
* @author 唯然<[email protected]>
*/

"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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @fileoverview the `recommended-script` config for `eslint.config.js`
* @author 唯然<[email protected]>
*/

"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
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @author 唯然<[email protected]>
*/
"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 = [
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
{
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,
},
},
]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compared to the previous (.eslintrc.js) config, this one might be missing this rule:

rules: {
"eslint-plugin/require-meta-docs-description": "error",
},

6 changes: 6 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 .",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compared to the previous version, this will now lint test/fixtures too, so just to check if that's intentional?

"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
Original file line number Diff line number Diff line change
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