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.

132 lines
6.0 KiB

преди 3 години
  1. # eslint-plugin-promise
  2. Enforce best practices for JavaScript promises.
  3. [![travis-ci](https://travis-ci.org/xjamundx/eslint-plugin-promise.svg)](https://travis-ci.org/xjamundx/eslint-plugin-promise)
  4. [![npm version](https://badge.fury.io/js/eslint-plugin-promise.svg)](https://www.npmjs.com/package/eslint-plugin-promise)
  5. [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
  6. <!-- START doctoc generated TOC please keep comment here to allow auto update -->
  7. <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
  8. - [Installation](#installation)
  9. - [Usage](#usage)
  10. - [Rules](#rules)
  11. - [Maintainers](#maintainers)
  12. - [License](#license)
  13. <!-- END doctoc generated TOC please keep comment here to allow auto update -->
  14. ## Installation
  15. You'll first need to install [ESLint](http://eslint.org):
  16. ```
  17. $ npm install eslint --save-dev
  18. ```
  19. Next, install `eslint-plugin-promise`:
  20. ```
  21. $ npm install eslint-plugin-promise --save-dev
  22. ```
  23. **Note:** If you installed ESLint globally (using the `-g` flag) then you must
  24. also install `eslint-plugin-promise` globally.
  25. ## Usage
  26. Add `promise` to the plugins section of your `.eslintrc.json` configuration
  27. file. You can omit the `eslint-plugin-` prefix:
  28. ```json
  29. {
  30. "plugins": ["promise"]
  31. }
  32. ```
  33. Then configure the rules you want to use under the rules section.
  34. ```json
  35. {
  36. "rules": {
  37. "promise/always-return": "error",
  38. "promise/no-return-wrap": "error",
  39. "promise/param-names": "error",
  40. "promise/catch-or-return": "error",
  41. "promise/no-native": "off",
  42. "promise/no-nesting": "warn",
  43. "promise/no-promise-in-callback": "warn",
  44. "promise/no-callback-in-promise": "warn",
  45. "promise/avoid-new": "warn",
  46. "promise/no-new-statics": "error",
  47. "promise/no-return-in-finally": "warn",
  48. "promise/valid-params": "warn"
  49. }
  50. }
  51. ```
  52. or start with the recommended rule set:
  53. ```json
  54. {
  55. "extends": ["plugin:promise/recommended"]
  56. }
  57. ```
  58. ## Rules
  59. | rule | description | recommended | fixable |
  60. | -------------------------------------------------------- | -------------------------------------------------------------------------------- | ----------- | -------- |
  61. | [`catch-or-return`][catch-or-return] | Enforces the use of `catch()` on un-returned promises. | :bangbang: | |
  62. | [`no-return-wrap`][no-return-wrap] | Avoid wrapping values in `Promise.resolve` or `Promise.reject` when not needed. | :bangbang: | |
  63. | [`param-names`][param-names] | Enforce consistent param names and ordering when creating new promises. | :bangbang: | |
  64. | [`always-return`][always-return] | Return inside each `then()` to create readable and reusable Promise chains. | :bangbang: | |
  65. | [`no-native`][no-native] | In an ES5 environment, make sure to create a `Promise` constructor before using. | | |
  66. | [`no-nesting`][no-nesting] | Avoid nested `then()` or `catch()` statements | :warning: | |
  67. | [`no-promise-in-callback`][no-promise-in-callback] | Avoid using promises inside of callbacks | :warning: | |
  68. | [`no-callback-in-promise`][no-callback-in-promise] | Avoid calling `cb()` inside of a `then()` (use [nodeify][] instead) | :warning: | |
  69. | [`avoid-new`][avoid-new] | Avoid creating `new` promises outside of utility libs (use [pify][] instead) | | |
  70. | [`no-new-statics`][no-new-statics] | Avoid calling `new` on a Promise static method | :bangbang: | :wrench: |
  71. | [`no-return-in-finally`][no-return-in-finally] | Disallow return statements in `finally()` | :warning: | |
  72. | [`valid-params`][valid-params] | Ensures the proper number of arguments are passed to Promise functions | :warning: | |
  73. | [`prefer-await-to-then`][prefer-await-to-then] | Prefer `await` to `then()` for reading Promise values | :seven: | |
  74. | [`prefer-await-to-callbacks`][prefer-await-to-callbacks] | Prefer async/await to the callback pattern | :seven: | |
  75. **Key**
  76. | icon | description |
  77. | ---------- | ----------------------------------------------- |
  78. | :bangbang: | Reports as error in recommended configuration |
  79. | :warning: | Reports as warning in recommended configuration |
  80. | :seven: | ES2017 Async Await rules |
  81. | :wrench: | Rule is fixable with `eslint --fix` |
  82. ## Maintainers
  83. - Jamund Ferguson - [@xjamundx][]
  84. - Macklin Underdown - [@macklinu][]
  85. ## License
  86. - (c) MMXV jden <mailto:jason@denizac.org> - ISC license.
  87. - (c) 2016 Jamund Ferguson <mailto:jamund@gmail.com> - ISC license.
  88. [catch-or-return]: docs/rules/catch-or-return.md
  89. [no-return-wrap]: docs/rules/no-return-wrap.md
  90. [param-names]: docs/rules/param-names.md
  91. [always-return]: docs/rules/always-return.md
  92. [no-native]: docs/rules/no-native.md
  93. [no-nesting]: docs/rules/no-nesting.md
  94. [no-promise-in-callback]: docs/rules/no-promise-in-callback.md
  95. [no-callback-in-promise]: docs/rules/no-callback-in-promise.md
  96. [avoid-new]: docs/rules/avoid-new.md
  97. [no-new-statics]: docs/rules/no-new-statics.md
  98. [no-return-in-finally]: docs/rules/no-return-in-finally.md
  99. [valid-params]: docs/rules/valid-params.md
  100. [prefer-await-to-then]: docs/rules/prefer-await-to-then.md
  101. [prefer-await-to-callbacks]: docs/rules/prefer-await-to-callbacks.md
  102. [nodeify]: https://www.npmjs.com/package/nodeify
  103. [pify]: https://www.npmjs.com/package/pify
  104. [@macklinu]: https://github.com/macklinu
  105. [@xjamundx]: https://github.com/xjamundx