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