Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new rule prefer-catch #525

Merged
merged 3 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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