Skip to content

Commit 2061413

Browse files
committed
💥 update node/recommended
1 parent 9006518 commit 2061413

File tree

14 files changed

+344
-87
lines changed

14 files changed

+344
-87
lines changed

‎README.md‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ $ npm install --save-dev eslint eslint-plugin-node
2323

2424
```json
2525
{
26-
"extends": ["eslint:recommended", "plugin:node/recommended"],
26+
"extends": [
27+
"eslint:recommended",
28+
"plugin:node/recommended"
29+
],
2730
"rules": {
2831
"node/exports-style": ["error", "module.exports"],
2932
"node/prefer-global/buffer": ["error", "always"],
@@ -102,15 +105,19 @@ These rules have been deprecated in accordance with the [deprecation policy](htt
102105

103106
## 🔧 Configs
104107

105-
This plugin provides `plugin:node/recommended` preset config.
106-
This preset config:
108+
This plugin provides three configs:
107109

108-
- enables the environment of ES2015 (ES6) and Node.js.
109-
- enables rules which are given :star: in the above table.
110-
- enables [no-process-exit](http://eslint.org/docs/rules/no-process-exit) rule because [the official document](https://nodejs.org/api/process.html#process_process_exit_code) does not recommend a use of `process.exit()`.
111-
- adds `{ecmaVersion: 2019}` into `parserOptions`.
112-
- adds `Atomics` and `SharedArrayBuffer` into `globals`.
113-
- adds this plugin into `plugins`.
110+
- `plugin:node/recommended` condiders both CommonJS and ES Modules. If [`"type":"module"` field](https://medium.com/@nodejs/announcing-a-new-experimental-modules-1be8d2d6c2ff#b023) existed in package.json then it considers files as ES Modules. Otherwise it considers files as CommonJS. In addition, it considers `*.mjs` files as ES Modules and `*.cjs` files as CommonJS.
111+
- `plugin:node/recommended-module` considers all files as ES Modules.
112+
- `plugin:node/recommended-script` considers all files as CommonJS.
113+
114+
Those preset config:
115+
116+
- enable [no-process-exit](http://eslint.org/docs/rules/no-process-exit) rule because [the official document](https://nodejs.org/api/process.html#process_process_exit_code) does not recommend a use of `process.exit()`.
117+
- enable plugin rules which are given :star: in the above table.
118+
- add `{ecmaVersion: 2019}` and etc into `parserOptions`.
119+
- add proper globals into `globals`.
120+
- add this plugin into `plugins`.
114121

115122
## 👫 FAQ
116123

‎lib/configs/_commons.js‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"use strict"
2+
3+
module.exports = {
4+
commonGlobals: {
5+
// ECMAScript
6+
ArrayBuffer: "readonly",
7+
Atomics: "readonly",
8+
DataView: "readonly",
9+
Float32Array: "readonly",
10+
Float64Array: "readonly",
11+
Int16Array: "readonly",
12+
Int32Array: "readonly",
13+
Int8Array: "readonly",
14+
Map: "readonly",
15+
Promise: "readonly",
16+
Proxy: "readonly",
17+
Reflect: "readonly",
18+
Set: "readonly",
19+
SharedArrayBuffer: "readonly",
20+
Symbol: "readonly",
21+
Uint16Array: "readonly",
22+
Uint32Array: "readonly",
23+
Uint8Array: "readonly",
24+
Uint8ClampedArray: "readonly",
25+
WeakMap: "readonly",
26+
WeakSet: "readonly",
27+
28+
// ECMAScript (experimental)
29+
BigInt: "readonly",
30+
BigInt64Array: "readonly",
31+
BigUint64Array: "readonly",
32+
globalThis: "readonly",
33+
34+
// ECMA-404
35+
Intl: "readonly",
36+
37+
// Web Standard
38+
TextDecoder: "readonly",
39+
TextEncoder: "readonly",
40+
URL: "readonly",
41+
URLSearchParams: "readonly",
42+
WebAssembly: "readonly",
43+
clearInterval: "readonly",
44+
clearTimeout: "readonly",
45+
console: "readonly",
46+
queueMicrotask: "readonly",
47+
setInterval: "readonly",
48+
setTimeout: "readonly",
49+
50+
// Node.js
51+
Buffer: "readonly",
52+
GLOBAL: "readonly",
53+
clearImmediate: "readonly",
54+
global: "readonly",
55+
process: "readonly",
56+
root: "readonly",
57+
setImmediate: "readonly",
58+
},
59+
commonRules: {
60+
"no-process-exit": "error",
61+
"node/no-deprecated-api": "error",
62+
"node/no-extraneous-require": "error",
63+
"node/no-missing-require": "error",
64+
"node/no-unpublished-bin": "error",
65+
"node/no-unpublished-require": "error",
66+
"node/no-unsupported-features/es-builtins": "error",
67+
"node/no-unsupported-features/es-syntax": "error",
68+
"node/no-unsupported-features/node-builtins": "error",
69+
"node/process-exit-as-throw": "error",
70+
"node/shebang": "error",
71+
},
72+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"use strict"
2+
3+
const { commonGlobals, commonRules } = require("./_commons")
4+
5+
module.exports = {
6+
globals: {
7+
...commonGlobals,
8+
__dirname: "off",
9+
__filename: "off",
10+
exports: "off",
11+
module: "off",
12+
require: "off",
13+
},
14+
parserOptions: {
15+
ecmaFeatures: { globalReturn: false },
16+
ecmaVersion: 2019,
17+
sourceType: "module",
18+
},
19+
plugins: ["node"],
20+
rules: {
21+
...commonRules,
22+
"node/no-extraneous-import": "error",
23+
"node/no-missing-import": "error",
24+
"node/no-unpublished-import": "error",
25+
"node/no-unsupported-features/es-syntax": [
26+
"error",
27+
{ ignores: ["modules"] },
28+
],
29+
},
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"use strict"
2+
3+
const { commonGlobals, commonRules } = require("./_commons")
4+
5+
module.exports = {
6+
globals: {
7+
...commonGlobals,
8+
__dirname: "readonly",
9+
__filename: "readonly",
10+
exports: "readonly",
11+
module: "readonly",
12+
require: "readonly",
13+
},
14+
parserOptions: {
15+
ecmaFeatures: { globalReturn: true },
16+
ecmaVersion: 2019,
17+
sourceType: "script",
18+
},
19+
plugins: ["node"],
20+
rules: {
21+
...commonRules,
22+
"node/no-extraneous-import": "off",
23+
"node/no-missing-import": "off",
24+
"node/no-unpublished-import": "off",
25+
"node/no-unsupported-features/es-syntax": ["error", { ignores: [] }],
26+
},
27+
}

‎lib/configs/recommended.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict"
2+
3+
const getPackageJson = require("../util/get-package-json")
4+
const moduleConfig = require("./recommended-module")
5+
const scriptConfig = require("./recommended-script")
6+
7+
module.exports = () => {
8+
const packageJson = getPackageJson()
9+
const isModule = (packageJson && packageJson.type) === "module"
10+
11+
return {
12+
...(isModule ? moduleConfig : scriptConfig),
13+
overrides: [
14+
{ files: ["*.cjs", ".*.cjs"], ...scriptConfig },
15+
{ files: ["*.mjs", ".*.mjs"], ...moduleConfig },
16+
],
17+
}
18+
}

‎lib/configs/recommended.json‎

Lines changed: 0 additions & 40 deletions
This file was deleted.

‎lib/index.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33

44
module.exports = {
55
configs: {
6-
recommended: require("./configs/recommended.json"),
6+
"recommended-module": require("./configs/recommended-module"),
7+
"recommended-script": require("./configs/recommended-script"),
8+
get recommended() {
9+
return require("./configs/recommended")()
10+
},
711
},
812
rules: {
913
"exports-style": require("./rules/exports-style"),

‎lib/util/get-package-json.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ function readPackageJson(dir) {
3939
* Gets a `package.json` data.
4040
* The data is cached if found, then it's used after.
4141
*
42-
* @param {string} startPath - A file path to lookup.
42+
* @param {string} [startPath] - A file path to lookup.
4343
* @returns {object|null} A found `package.json` data or `null`.
4444
* This object have additional property `filePath`.
4545
*/
46-
module.exports = function getPackageJson(startPath) {
46+
module.exports = function getPackageJson(startPath = "a.js") {
4747
const startDir = path.dirname(path.resolve(startPath))
4848
let dir = startDir
4949
let prevDir = ""

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"@mysticatea/eslint-plugin": "^10.0.3",
2525
"codecov": "^3.3.0",
2626
"eslint": "^5.16.0",
27+
"eslint-plugin-node": "file:.",
2728
"fast-glob": "^2.2.6",
2829
"mocha": "^6.1.4",
2930
"nyc": "^14.0.0",

‎scripts/update-lib-configs-recommended.js‎

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)