Skip to content

Commit

Permalink
feat: new rule prefer-catch (eslint-community#525)
Browse files Browse the repository at this point in the history
Adds new rule `prefer-catch` to prefer `catch` over `then(a, b)` or `then(null, b)`.

Co-authored-by: Sebastian Good <[email protected]>
  • Loading branch information
brettz9 and scagood authored Oct 26, 2024
1 parent 7ff2cb9 commit 05c8a93
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 131 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ or start with the recommended rule set:
| [param-names](docs/rules/param-names.md) | Enforce consistent param names and ordering when creating new promises. | ✅ | | | |
| [prefer-await-to-callbacks](docs/rules/prefer-await-to-callbacks.md) | Prefer `async`/`await` to the callback pattern. | | | | |
| [prefer-await-to-then](docs/rules/prefer-await-to-then.md) | Prefer `await` to `then()`/`catch()`/`finally()` for reading Promise values. | | | | |
| [prefer-catch](docs/rules/prefer-catch.md) | Prefer `catch` to `then(a, b)`/`then(null, b)` for handling errors. | | | | 🔧 |
| [spec-only](docs/rules/spec-only.md) | Disallow use of non-standard Promise static methods. | | | | |
| [valid-params](docs/rules/valid-params.md) | Enforces the proper number of arguments are passed to Promise functions. | | ✅ | | |

Expand Down
65 changes: 65 additions & 0 deletions __tests__/prefer-catch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict'

const rule = require('../rules/prefer-catch')
const { RuleTester } = require('./rule-tester')
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 8,
},
})

const message = 'Prefer `catch` to `then(a, b)`/`then(null, b)`.'

ruleTester.run('prefer-catch', rule, {
valid: [
'prom.then()',
'prom.then(fn)',
'prom.then(fn1).then(fn2)',
'prom.then(() => {})',
'prom.then(function () {})',
'prom.catch()',
'prom.catch(handleErr).then(handle)',
'prom.catch(handleErr)',
],

invalid: [
{
code: 'hey.then(fn1, fn2)',
errors: [{ message }],
output: 'hey.catch(fn2).then(fn1)',
},
{
code: 'hey.then(fn1, (fn2))',
errors: [{ message }],
output: 'hey.catch(fn2).then(fn1)',
},
{
code: 'hey.then(null, fn2)',
errors: [{ message }],
output: 'hey.catch(fn2)',
},
{
code: 'hey.then(undefined, fn2)',
errors: [{ message }],
output: 'hey.catch(fn2)',
},
{
code: 'function foo() { hey.then(x => {}, () => {}) }',
errors: [{ message }],
output: 'function foo() { hey.catch(() => {}).then(x => {}) }',
},
{
code: `
function foo() {
hey.then(function a() { }, function b() {}).then(fn1, fn2)
}
`,
errors: [{ message }, { message }],
output: `
function foo() {
hey.catch(function b() {}).then(function a() { }).catch(fn2).then(fn1)
}
`,
},
],
})
29 changes: 29 additions & 0 deletions docs/rules/prefer-catch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Prefer `catch` to `then(a, b)`/`then(null, b)` for handling errors (`promise/prefer-catch`)

🔧 This rule is automatically fixable by the
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

<!-- end auto-generated rule header -->

A `then` call with two arguments can make it more difficult to recognize that a
catch error handler is present and can be less clear as to the order in which
errors will be handled.

## Rule Details

The second argument of a `then` call may be thought to handle any errors in the
first argument, but it will only handle errors earlier in the Promise chain.

Examples of **incorrect** code for this rule:

```js
prom.then(fn1).then(fn2)
prom.catch(handleErr).then(handle)
```

Examples of **incorrect** code for this rule:

```js
hey.then(fn1, fn2)
hey.then(null, fn2)
```
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const pluginPromise = {
'catch-or-return': require('./rules/catch-or-return'),
'prefer-await-to-callbacks': require('./rules/prefer-await-to-callbacks'),
'prefer-await-to-then': require('./rules/prefer-await-to-then'),
'prefer-catch': require('./rules/prefer-catch'),
'no-native': require('./rules/no-native'),
'no-callback-in-promise': require('./rules/no-callback-in-promise'),
'no-promise-in-callback': require('./rules/no-promise-in-callback'),
Expand Down
Loading

0 comments on commit 05c8a93

Please sign in to comment.