-
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
9 changed files
with
286 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
`, | ||
}, | ||
], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.