Skip to content

Commit 7cb29c8

Browse files
committed
refactor: add fixer
1 parent cd07235 commit 7cb29c8

8 files changed

+174
-133
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +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. | | | | |
118+
| [prefer-catch](docs/rules/prefer-catch.md) | Prefer `catch` to `then(a, b)`/`then(null, b)` for handling errors. | | | | 🔧 |
119119
| [spec-only](docs/rules/spec-only.md) | Disallow use of non-standard Promise static methods. | | | | |
120120
| [valid-params](docs/rules/valid-params.md) | Enforces the proper number of arguments are passed to Promise functions. | || | |
121121

__tests__/prefer-catch.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,40 @@ ruleTester.run('prefer-catch', rule, {
2626
{
2727
code: 'hey.then(fn1, fn2)',
2828
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)',
2935
},
3036
{
3137
code: 'hey.then(null, fn2)',
3238
errors: [{ message }],
39+
output: 'hey.catch(fn2)',
40+
},
41+
{
42+
code: 'hey.then(undefined, fn2)',
43+
errors: [{ message }],
44+
output: 'hey.catch(fn2)',
3345
},
3446
{
3547
code: 'function foo() { hey.then(x => {}, () => {}) }',
3648
errors: [{ message }],
49+
output: 'function foo() { hey.catch(() => {}).then(x => {}) }',
3750
},
3851
{
3952
code: `
4053
function foo() {
41-
hey.then(function() { }, function() {}).then(fn1, fn2)
54+
hey.then(function a() { }, function b() {}).then(fn1, fn2)
4255
}
4356
`,
4457
errors: [{ message }, { message }],
58+
output: `
59+
function foo() {
60+
hey.catch(function b() {}).then(function a() { }).catch(fn2).then(fn1)
61+
}
62+
`,
4563
},
4664
],
4765
})

docs/rules/prefer-catch.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Prefer `catch` to `then(a, b)`/`then(null, b)` for handling errors (`promise/prefer-catch`)
22

3+
🔧 This rule is automatically fixable by the
4+
[`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
5+
36
<!-- end auto-generated rule header -->
47

58
A `then` call with two arguments can make it more difficult to recognize that a

0 commit comments

Comments
 (0)