Skip to content

Commit 0304cac

Browse files
committed
feat: new rule prefer-catch
1 parent e545254 commit 0304cac

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
},
30+
{
31+
code: 'hey.then(null, fn2)',
32+
errors: [{ message }],
33+
},
34+
{
35+
code: 'function foo() { hey.then(x => {}, () => {}) }',
36+
errors: [{ message }],
37+
},
38+
{
39+
code: `
40+
function foo() {
41+
hey.then(function() { }, function() {}).then(fn1, fn2)
42+
}
43+
`,
44+
errors: [{ message }, { message }],
45+
},
46+
],
47+
})

docs/rules/prefer-catch.md

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

index.js

Lines changed: 1 addition & 0 deletions
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'),

rules/prefer-catch.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Rule: prefer-catch
3+
* Discourage using then(a, b) or then(null, b) and instead use catch().
4+
*/
5+
6+
'use strict'
7+
8+
const getDocsUrl = require('./lib/get-docs-url')
9+
10+
module.exports = {
11+
meta: {
12+
type: 'suggestion',
13+
docs: {
14+
description:
15+
'Prefer `catch` to `then(a, b)`/`then(null, b)` for handling errors.',
16+
url: getDocsUrl('prefer-catch'),
17+
},
18+
schema: [],
19+
messages: {
20+
preferCatchToThen: 'Prefer `catch` to `then(a, b)`/`then(null, b)`.',
21+
},
22+
},
23+
create(context) {
24+
return {
25+
'CallExpression > MemberExpression.callee'(node) {
26+
if (
27+
node.property &&
28+
node.property.name === 'then' &&
29+
node.parent.arguments.length >= 2
30+
) {
31+
context.report({
32+
node: node.property,
33+
messageId: 'preferCatchToThen',
34+
})
35+
}
36+
},
37+
}
38+
},
39+
}

0 commit comments

Comments
 (0)