Skip to content

Commit

Permalink
refactor: add fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Oct 16, 2024
1 parent 3bf352c commit 7450023
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 133 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +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. | | | | |
| [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
20 changes: 19 additions & 1 deletion __tests__/prefer-catch.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,40 @@ ruleTester.run('prefer-catch', rule, {
{
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() { }, function() {}).then(fn1, fn2)
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)
}
`,
},
],
})
3 changes: 3 additions & 0 deletions docs/rules/prefer-catch.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# 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
Expand Down
Loading

0 comments on commit 7450023

Please sign in to comment.