Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
💥 update node/recommended
  • Loading branch information
mysticatea committed May 2, 2019
1 parent 9006518 commit 2061413
Show file tree
Hide file tree
Showing 14 changed files with 344 additions and 87 deletions.
25 changes: 16 additions & 9 deletions README.md
Expand Up @@ -23,7 +23,10 @@ $ npm install --save-dev eslint eslint-plugin-node

```json
{
"extends": ["eslint:recommended", "plugin:node/recommended"],
"extends": [
"eslint:recommended",
"plugin:node/recommended"
],
"rules": {
"node/exports-style": ["error", "module.exports"],
"node/prefer-global/buffer": ["error", "always"],
Expand Down Expand Up @@ -102,15 +105,19 @@ These rules have been deprecated in accordance with the [deprecation policy](htt

## 🔧 Configs

This plugin provides `plugin:node/recommended` preset config.
This preset config:
This plugin provides three configs:

- enables the environment of ES2015 (ES6) and Node.js.
- enables rules which are given :star: in the above table.
- 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()`.
- adds `{ecmaVersion: 2019}` into `parserOptions`.
- adds `Atomics` and `SharedArrayBuffer` into `globals`.
- adds this plugin into `plugins`.
- `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.
- `plugin:node/recommended-module` considers all files as ES Modules.
- `plugin:node/recommended-script` considers all files as CommonJS.

Those preset config:

- 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()`.
- enable plugin rules which are given :star: in the above table.
- add `{ecmaVersion: 2019}` and etc into `parserOptions`.
- add proper globals into `globals`.
- add this plugin into `plugins`.

## 👫 FAQ

Expand Down
72 changes: 72 additions & 0 deletions lib/configs/_commons.js
@@ -0,0 +1,72 @@
"use strict"

module.exports = {
commonGlobals: {
// ECMAScript
ArrayBuffer: "readonly",
Atomics: "readonly",
DataView: "readonly",
Float32Array: "readonly",
Float64Array: "readonly",
Int16Array: "readonly",
Int32Array: "readonly",
Int8Array: "readonly",
Map: "readonly",
Promise: "readonly",
Proxy: "readonly",
Reflect: "readonly",
Set: "readonly",
SharedArrayBuffer: "readonly",
Symbol: "readonly",
Uint16Array: "readonly",
Uint32Array: "readonly",
Uint8Array: "readonly",
Uint8ClampedArray: "readonly",
WeakMap: "readonly",
WeakSet: "readonly",

// ECMAScript (experimental)
BigInt: "readonly",
BigInt64Array: "readonly",
BigUint64Array: "readonly",
globalThis: "readonly",

// ECMA-404
Intl: "readonly",

// Web Standard
TextDecoder: "readonly",
TextEncoder: "readonly",
URL: "readonly",
URLSearchParams: "readonly",
WebAssembly: "readonly",
clearInterval: "readonly",
clearTimeout: "readonly",
console: "readonly",
queueMicrotask: "readonly",
setInterval: "readonly",
setTimeout: "readonly",

// Node.js
Buffer: "readonly",
GLOBAL: "readonly",
clearImmediate: "readonly",
global: "readonly",
process: "readonly",
root: "readonly",
setImmediate: "readonly",
},
commonRules: {
"no-process-exit": "error",
"node/no-deprecated-api": "error",
"node/no-extraneous-require": "error",
"node/no-missing-require": "error",
"node/no-unpublished-bin": "error",
"node/no-unpublished-require": "error",
"node/no-unsupported-features/es-builtins": "error",
"node/no-unsupported-features/es-syntax": "error",
"node/no-unsupported-features/node-builtins": "error",
"node/process-exit-as-throw": "error",
"node/shebang": "error",
},
}
30 changes: 30 additions & 0 deletions lib/configs/recommended-module.js
@@ -0,0 +1,30 @@
"use strict"

const { commonGlobals, commonRules } = require("./_commons")

module.exports = {
globals: {
...commonGlobals,
__dirname: "off",
__filename: "off",
exports: "off",
module: "off",
require: "off",
},
parserOptions: {
ecmaFeatures: { globalReturn: false },
ecmaVersion: 2019,
sourceType: "module",
},
plugins: ["node"],
rules: {
...commonRules,
"node/no-extraneous-import": "error",
"node/no-missing-import": "error",
"node/no-unpublished-import": "error",
"node/no-unsupported-features/es-syntax": [
"error",
{ ignores: ["modules"] },
],
},
}
27 changes: 27 additions & 0 deletions lib/configs/recommended-script.js
@@ -0,0 +1,27 @@
"use strict"

const { commonGlobals, commonRules } = require("./_commons")

module.exports = {
globals: {
...commonGlobals,
__dirname: "readonly",
__filename: "readonly",
exports: "readonly",
module: "readonly",
require: "readonly",
},
parserOptions: {
ecmaFeatures: { globalReturn: true },
ecmaVersion: 2019,
sourceType: "script",
},
plugins: ["node"],
rules: {
...commonRules,
"node/no-extraneous-import": "off",
"node/no-missing-import": "off",
"node/no-unpublished-import": "off",
"node/no-unsupported-features/es-syntax": ["error", { ignores: [] }],
},
}
18 changes: 18 additions & 0 deletions lib/configs/recommended.js
@@ -0,0 +1,18 @@
"use strict"

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"

return {
...(isModule ? moduleConfig : scriptConfig),
overrides: [
{ files: ["*.cjs", ".*.cjs"], ...scriptConfig },
{ files: ["*.mjs", ".*.mjs"], ...moduleConfig },
],
}
}
40 changes: 0 additions & 40 deletions lib/configs/recommended.json

This file was deleted.

6 changes: 5 additions & 1 deletion lib/index.js
Expand Up @@ -3,7 +3,11 @@

module.exports = {
configs: {
recommended: require("./configs/recommended.json"),
"recommended-module": require("./configs/recommended-module"),
"recommended-script": require("./configs/recommended-script"),
get recommended() {
return require("./configs/recommended")()
},
},
rules: {
"exports-style": require("./rules/exports-style"),
Expand Down
4 changes: 2 additions & 2 deletions lib/util/get-package-json.js
Expand Up @@ -39,11 +39,11 @@ function readPackageJson(dir) {
* Gets a `package.json` data.
* The data is cached if found, then it's used after.
*
* @param {string} startPath - A file path to lookup.
* @param {string} [startPath] - A file path to lookup.
* @returns {object|null} A found `package.json` data or `null`.
* This object have additional property `filePath`.
*/
module.exports = function getPackageJson(startPath) {
module.exports = function getPackageJson(startPath = "a.js") {
const startDir = path.dirname(path.resolve(startPath))
let dir = startDir
let prevDir = ""
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -24,6 +24,7 @@
"@mysticatea/eslint-plugin": "^10.0.3",
"codecov": "^3.3.0",
"eslint": "^5.16.0",
"eslint-plugin-node": "file:.",
"fast-glob": "^2.2.6",
"mocha": "^6.1.4",
"nyc": "^14.0.0",
Expand Down
34 changes: 0 additions & 34 deletions scripts/update-lib-configs-recommended.js

This file was deleted.

6 changes: 5 additions & 1 deletion scripts/update-lib-index.js
Expand Up @@ -15,7 +15,11 @@ const rawContent = `/* DON'T EDIT THIS FILE. This is generated by 'scripts/updat
module.exports = {
configs: {
recommended: require("./configs/recommended.json"),
"recommended-module": require("./configs/recommended-module"),
"recommended-script": require("./configs/recommended-script"),
get recommended() {
return require("./configs/recommended")()
},
},
rules: {
${rules
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/configs/cjs/package.json
@@ -0,0 +1,2 @@
{
}
3 changes: 3 additions & 0 deletions tests/fixtures/configs/esm/package.json
@@ -0,0 +1,3 @@
{
"type": "module"
}

0 comments on commit 2061413

Please sign in to comment.