Skip to content

Commit 05c8a93

Browse files
brettz9scagood
andauthored
feat: new rule prefer-catch (#525)
Adds new rule `prefer-catch` to prefer `catch` over `then(a, b)` or `then(null, b)`. Co-authored-by: Sebastian Good <[email protected]>
1 parent 7ff2cb9 commit 05c8a93

File tree

9 files changed

+286
-131
lines changed

9 files changed

+286
-131
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ or start with the recommended rule set:
115115
| [param-names](docs/rules/param-names.md) | Enforce consistent param names and ordering when creating new promises. || | | |
116116
| [prefer-await-to-callbacks](docs/rules/prefer-await-to-callbacks.md) | Prefer `async`/`await` to the callback pattern. | | | | |
117117
| [prefer-await-to-then](docs/rules/prefer-await-to-then.md) | Prefer `await` to `then()`/`catch()`/`finally()` for reading Promise values. | | | | |
118+
| [prefer-catch](docs/rules/prefer-catch.md) | Prefer `catch` to `then(a, b)`/`then(null, b)` for handling errors. | | | | 🔧 |
118119
| [spec-only](docs/rules/spec-only.md) | Disallow use of non-standard Promise static methods. | | | | |
119120
| [valid-params](docs/rules/valid-params.md) | Enforces the proper number of arguments are passed to Promise functions. | || | |
120121

__tests__/prefer-catch.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict'
2+
3+
const rule = require('../rules/prefer-catch')
4+
const { RuleTester } = require('./rule-tester')
5+
const ruleTester = new RuleTester({
6+
parserOptions: {
7+
ecmaVersion: 8,
8+
},
9+
})
10+
11+
const message = 'Prefer `catch` to `then(a, b)`/`then(null, b)`.'
12+
13+
ruleTester.run('prefer-catch', rule, {
14+
valid: [
15+
'prom.then()',
16+
'prom.then(fn)',
17+
'prom.then(fn1).then(fn2)',
18+
'prom.then(() => {})',
19+
'prom.then(function () {})',
20+
'prom.catch()',
21+
'prom.catch(handleErr).then(handle)',
22+
'prom.catch(handleErr)',
23+
],
24+
25+
invalid: [
26+
{
27+
code: 'hey.then(fn1, fn2)',
28+
errors: [{ message }],
29+
output: 'hey.catch(fn2).then(fn1)',
30+
},
31+
{
32+
code: 'hey.then(fn1, (fn2))',
33+
errors: [{ message }],
34+
output: 'hey.catch(fn2).then(fn1)',
35+
},
36+
{
37+
code: 'hey.then(null, fn2)',
38+
errors: [{ message }],
39+
output: 'hey.catch(fn2)',
40+
},
41+
{
42+
code: 'hey.then(undefined, fn2)',
43+
errors: [{ message }],
44+
output: 'hey.catch(fn2)',
45+
},
46+
{
47+
code: 'function foo() { hey.then(x => {}, () => {}) }',
48+
errors: [{ message }],
49+
output: 'function foo() { hey.catch(() => {}).then(x => {}) }',
50+
},
51+
{
52+
code: `
53+
function foo() {
54+
hey.then(function a() { }, function b() {}).then(fn1, fn2)
55+
}
56+
`,
57+
errors: [{ message }, { message }],
58+
output: `
59+
function foo() {
60+
hey.catch(function b() {}).then(function a() { }).catch(fn2).then(fn1)
61+
}
62+
`,
63+
},
64+
],
65+
})

docs/rules/prefer-catch.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Prefer `catch` to `then(a, b)`/`then(null, b)` for handling errors (`promise/prefer-catch`)
2+
3+
🔧 This rule is automatically fixable by the
4+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
5+
6+
<!-- end auto-generated rule header -->
7+
8+
A `then` call with two arguments can make it more difficult to recognize that a
9+
catch error handler is present and can be less clear as to the order in which
10+
errors will be handled.
11+
12+
## Rule Details
13+
14+
The second argument of a `then` call may be thought to handle any errors in the
15+
first argument, but it will only handle errors earlier in the Promise chain.
16+
17+
Examples of **incorrect** code for this rule:
18+
19+
```js
20+
prom.then(fn1).then(fn2)
21+
prom.catch(handleErr).then(handle)
22+
```
23+
24+
Examples of **incorrect** code for this rule:
25+
26+
```js
27+
hey.then(fn1, fn2)
28+
hey.then(null, fn2)
29+
```

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const pluginPromise = {
2323
'catch-or-return': require('./rules/catch-or-return'),
2424
'prefer-await-to-callbacks': require('./rules/prefer-await-to-callbacks'),
2525
'prefer-await-to-then': require('./rules/prefer-await-to-then'),
26+
'prefer-catch': require('./rules/prefer-catch'),
2627
'no-native': require('./rules/no-native'),
2728
'no-callback-in-promise': require('./rules/no-callback-in-promise'),
2829
'no-promise-in-callback': require('./rules/no-promise-in-callback'),

0 commit comments

Comments
 (0)