You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
9.2 KiB

4 years ago
  1. # eslint-plugin-node
  2. [![npm version](https://img.shields.io/npm/v/eslint-plugin-node.svg)](https://www.npmjs.com/package/eslint-plugin-node)
  3. [![Downloads/month](https://img.shields.io/npm/dm/eslint-plugin-node.svg)](http://www.npmtrends.com/eslint-plugin-node)
  4. [![Build Status](https://github.com/mysticatea/eslint-plugin-node/workflows/CI/badge.svg)](https://github.com/mysticatea/eslint-plugin-node/actions)
  5. [![Coverage Status](https://codecov.io/gh/mysticatea/eslint-plugin-node/branch/master/graph/badge.svg)](https://codecov.io/gh/mysticatea/eslint-plugin-node)
  6. [![Dependency Status](https://david-dm.org/mysticatea/eslint-plugin-node.svg)](https://david-dm.org/mysticatea/eslint-plugin-node)
  7. Additional ESLint's rules for Node.js
  8. ## 💿 Install & Usage
  9. ```
  10. $ npm install --save-dev eslint eslint-plugin-node
  11. ```
  12. - Requires Node.js `>=8.10.0`
  13. - Requires ESLint `>=5.16.0`
  14. **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 `node/no-unsupported-features/*` rules.
  15. **.eslintrc.json** (An example)
  16. ```jsonc
  17. {
  18. "extends": [
  19. "eslint:recommended",
  20. "plugin:node/recommended"
  21. ],
  22. "parserOptions": {
  23. // Only ESLint 6.2.0 and later support ES2020.
  24. "ecmaVersion": 2020
  25. },
  26. "rules": {
  27. "node/exports-style": ["error", "module.exports"],
  28. "node/file-extension-in-import": ["error", "always"],
  29. "node/prefer-global/buffer": ["error", "always"],
  30. "node/prefer-global/console": ["error", "always"],
  31. "node/prefer-global/process": ["error", "always"],
  32. "node/prefer-global/url-search-params": ["error", "always"],
  33. "node/prefer-global/url": ["error", "always"],
  34. "node/prefer-promises/dns": "error",
  35. "node/prefer-promises/fs": "error"
  36. }
  37. }
  38. ```
  39. **package.json** (An example)
  40. ```json
  41. {
  42. "name": "your-module",
  43. "version": "1.0.0",
  44. "type": "commonjs",
  45. "engines": {
  46. "node": ">=8.10.0"
  47. }
  48. }
  49. ```
  50. ## 📖 Rules
  51. - ⭐️ - the mark of recommended rules.
  52. - ✒️ - the mark of fixable rules.
  53. <!--RULES_TABLE_START-->
  54. ### Possible Errors
  55. | Rule ID | Description | |
  56. |:--------|:------------|:--:|
  57. | [node/no-callback-literal](./docs/rules/no-callback-literal.md) | ensure Node.js-style error-first callback pattern is followed | |
  58. | [node/no-exports-assign](./docs/rules/no-exports-assign.md) | disallow the assignment to `exports` | ⭐️ |
  59. | [node/no-extraneous-import](./docs/rules/no-extraneous-import.md) | disallow `import` declarations which import extraneous modules | ⭐️ |
  60. | [node/no-extraneous-require](./docs/rules/no-extraneous-require.md) | disallow `require()` expressions which import extraneous modules | ⭐️ |
  61. | [node/no-missing-import](./docs/rules/no-missing-import.md) | disallow `import` declarations which import non-existence modules | ⭐️ |
  62. | [node/no-missing-require](./docs/rules/no-missing-require.md) | disallow `require()` expressions which import non-existence modules | ⭐️ |
  63. | [node/no-unpublished-bin](./docs/rules/no-unpublished-bin.md) | disallow `bin` files that npm ignores | ⭐️ |
  64. | [node/no-unpublished-import](./docs/rules/no-unpublished-import.md) | disallow `import` declarations which import private modules | ⭐️ |
  65. | [node/no-unpublished-require](./docs/rules/no-unpublished-require.md) | disallow `require()` expressions which import private modules | ⭐️ |
  66. | [node/no-unsupported-features/es-builtins](./docs/rules/no-unsupported-features/es-builtins.md) | disallow unsupported ECMAScript built-ins on the specified version | ⭐️ |
  67. | [node/no-unsupported-features/es-syntax](./docs/rules/no-unsupported-features/es-syntax.md) | disallow unsupported ECMAScript syntax on the specified version | ⭐️ |
  68. | [node/no-unsupported-features/node-builtins](./docs/rules/no-unsupported-features/node-builtins.md) | disallow unsupported Node.js built-in APIs on the specified version | ⭐️ |
  69. | [node/process-exit-as-throw](./docs/rules/process-exit-as-throw.md) | make `process.exit()` expressions the same code path as `throw` | ⭐️ |
  70. | [node/shebang](./docs/rules/shebang.md) | suggest correct usage of shebang | ⭐️✒️ |
  71. ### Best Practices
  72. | Rule ID | Description | |
  73. |:--------|:------------|:--:|
  74. | [node/no-deprecated-api](./docs/rules/no-deprecated-api.md) | disallow deprecated APIs | ⭐️ |
  75. ### Stylistic Issues
  76. | Rule ID | Description | |
  77. |:--------|:------------|:--:|
  78. | [node/exports-style](./docs/rules/exports-style.md) | enforce either `module.exports` or `exports` | |
  79. | [node/file-extension-in-import](./docs/rules/file-extension-in-import.md) | enforce the style of file extensions in `import` declarations | ✒️ |
  80. | [node/prefer-global/buffer](./docs/rules/prefer-global/buffer.md) | enforce either `Buffer` or `require("buffer").Buffer` | |
  81. | [node/prefer-global/console](./docs/rules/prefer-global/console.md) | enforce either `console` or `require("console")` | |
  82. | [node/prefer-global/process](./docs/rules/prefer-global/process.md) | enforce either `process` or `require("process")` | |
  83. | [node/prefer-global/text-decoder](./docs/rules/prefer-global/text-decoder.md) | enforce either `TextDecoder` or `require("util").TextDecoder` | |
  84. | [node/prefer-global/text-encoder](./docs/rules/prefer-global/text-encoder.md) | enforce either `TextEncoder` or `require("util").TextEncoder` | |
  85. | [node/prefer-global/url-search-params](./docs/rules/prefer-global/url-search-params.md) | enforce either `URLSearchParams` or `require("url").URLSearchParams` | |
  86. | [node/prefer-global/url](./docs/rules/prefer-global/url.md) | enforce either `URL` or `require("url").URL` | |
  87. | [node/prefer-promises/dns](./docs/rules/prefer-promises/dns.md) | enforce `require("dns").promises` | |
  88. | [node/prefer-promises/fs](./docs/rules/prefer-promises/fs.md) | enforce `require("fs").promises` | |
  89. ### Deprecated rules
  90. These rules have been deprecated in accordance with the [deprecation policy](https://eslint.org/docs/user-guide/rule-deprecation), and replaced by newer rules:
  91. | Rule ID | Replaced by |
  92. |:--------|:------------|
  93. | [node/no-hide-core-modules](./docs/rules/no-hide-core-modules.md) | (nothing) |
  94. | [node/no-unsupported-features](./docs/rules/no-unsupported-features.md) | [node/no-unsupported-features/es-syntax](./docs/rules/no-unsupported-features/es-syntax.md) and [node/no-unsupported-features/es-builtins](./docs/rules/no-unsupported-features/es-builtins.md) |
  95. <!--RULES_TABLE_END-->
  96. ## 🔧 Configs
  97. This plugin provides three configs:
  98. - `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.
  99. - `plugin:node/recommended-module` considers all files as ES Modules.
  100. - `plugin:node/recommended-script` considers all files as CommonJS.
  101. Those preset config:
  102. - 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()`.
  103. - enable plugin rules which are given :star: in the above table.
  104. - add `{ecmaVersion: 2019}` and etc into `parserOptions`.
  105. - add proper globals into `globals`.
  106. - add this plugin into `plugins`.
  107. ## 👫 FAQ
  108. - Q: The `no-missing-import` / `no-missing-require` rules don't work with nested folders in SublimeLinter-eslint
  109. - 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.
  110. ## 🚥 Semantic Versioning Policy
  111. `eslint-plugin-node` follows [semantic versioning](http://semver.org/) and [ESLint's Semantic Versioning Policy](https://github.com/eslint/eslint#semantic-versioning-policy).
  112. - Patch release (intended to not break your lint build)
  113. - A bug fix in a rule that results in it reporting fewer errors.
  114. - Improvements to documentation.
  115. - Non-user-facing changes such as refactoring code, adding, deleting, or modifying tests, and increasing test coverage.
  116. - Re-releasing after a failed release (i.e., publishing a release that doesn't work for anyone).
  117. - Minor release (might break your lint build)
  118. - A bug fix in a rule that results in it reporting more errors.
  119. - A new rule is created.
  120. - A new option to an existing rule is created.
  121. - An existing rule is deprecated.
  122. - Major release (likely to break your lint build)
  123. - A support for old Node version is dropped.
  124. - A support for old ESLint version is dropped.
  125. - An existing rule is changed in it reporting more errors.
  126. - An existing rule is removed.
  127. - An existing option of a rule is removed.
  128. - An existing config is updated.
  129. ## 📰 Changelog
  130. - [GitHub Releases](https://github.com/mysticatea/eslint-plugin-node/releases)
  131. ## ❤️ Contributing
  132. Welcome contributing!
  133. Please use GitHub's Issues/PRs.
  134. ### Development Tools
  135. - `npm test` runs tests and measures coverage.
  136. - `npm run coverage` shows the coverage result of `npm test` command.
  137. - `npm run clean` removes the coverage result of `npm test` command.