Skip to content

Commit

Permalink
feat: add allowThenStrict option; fixes #52
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 authored and MichaelDeBoey committed Oct 9, 2024
1 parent 3d22840 commit 1a0161f
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
62 changes: 62 additions & 0 deletions __tests__/catch-or-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,37 @@ ruleTester.run('catch-or-return', rule, {
options: [{ allowThen: true }],
},

// allowThenStrict - .then(null, fn)
{
code: 'frank().then(go).then(null, doIt)',
options: [{ allowThenStrict: true }],
},
{
code: 'frank().then(go).then().then().then().then(null, doIt)',
options: [{ allowThenStrict: true }],
},
{
code: 'frank().then(go).then().then(null, function() { /* why bother */ })',
options: [{ allowThenStrict: true }],
},
{
code: 'frank.then(go).then(to).then(null, jail)',
options: [{ allowThenStrict: true }],
},

{
code: 'frank().then(a).then(b).then(null, c)',
options: [{ allowThenStrict: true }],
},
{
code: 'frank().then(a).then(b).then().then().then(null, doIt)',
options: [{ allowThenStrict: true }],
},
{
code: 'frank().then(a).then(b).then(null, function() { /* why bother */ })',
options: [{ allowThenStrict: true }],
},

// allowFinally - .finally(fn)
{
code: 'frank().then(go).catch(doIt).finally(fn)',
Expand Down Expand Up @@ -273,5 +304,36 @@ ruleTester.run('catch-or-return', rule, {
code: 'frank.then(go).then(to).then(pewPew, jail)',
errors: [{ message: catchMessage }],
},

{
code: 'frank().then(a, b)',
errors: [{ message: catchMessage }],
options: [{ allowThenStrict: true }],
},
{
code: 'frank().then(go).then(zam, doIt)',
errors: [{ message: catchMessage }],
options: [{ allowThenStrict: true }],
},
{
code: 'frank().then(a).then(b).then(c, d)',
errors: [{ message: catchMessage }],
options: [{ allowThenStrict: true }],
},
{
code: 'frank().then(go).then().then().then().then(wham, doIt)',
errors: [{ message: catchMessage }],
options: [{ allowThenStrict: true }],
},
{
code: 'frank().then(go).then().then(function() {}, function() { /* why bother */ })',
errors: [{ message: catchMessage }],
options: [{ allowThenStrict: true }],
},
{
code: 'frank.then(go).then(to).then(pewPew, jail)',
errors: [{ message: catchMessage }],
options: [{ allowThenStrict: true }],
},
],
})
28 changes: 28 additions & 0 deletions docs/rules/catch-or-return.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,34 @@ myPromise.then(null, handleErrors)
myPromise.then(doSomething).catch(handleErrors)
```

##### `allowThenStrict`

`allowThenStrict` is similar to `allowThen` but it only permits `then` when the
fulfillment handler is `null`. This option ensures that the final rejected
handler works like a `catch` and can handle any uncaught errors from the final
`then`.

Examples of **incorrect** code for the default `{ allowThenStrict: false }`
option:

```js
myPromise.then(doSomething, handleErrors)
myPromise.then(null, handleErrors)
```

Examples of **correct** code for the `{ allowThenStrict: true }` option:

```js
myPromise.then(null, handleErrors)
myPromise.then(doSomething).catch(handleErrors)
```

Examples of **incorrect** code for the `{ allowThenStrict: true }` option:

```js
myPromise.then(doSomething, handleErrors)
```

##### `allowFinally`

You can pass an `{ allowFinally: true }` as an option to this rule to allow for
Expand Down
12 changes: 10 additions & 2 deletions rules/catch-or-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ module.exports = {
allowThen: {
type: 'boolean',
},
allowThenStrict: {
type: 'boolean',
},
terminationMethod: {
oneOf: [
{ type: 'string' },
Expand All @@ -49,6 +52,7 @@ module.exports = {
create(context) {
const options = context.options[0] || {}
const allowThen = options.allowThen
const allowThenStrict = options.allowThenStrict
const allowFinally = options.allowFinally
let terminationMethod = options.terminationMethod || 'catch'

Expand All @@ -59,11 +63,15 @@ module.exports = {
function isAllowedPromiseTermination(expression) {
// somePromise.then(a, b)
if (
allowThen &&
(allowThen || allowThenStrict) &&
expression.type === 'CallExpression' &&
expression.callee.type === 'MemberExpression' &&
expression.callee.property.name === 'then' &&
expression.arguments.length === 2
expression.arguments.length === 2 &&
// somePromise.then(null, b)
((allowThen && !allowThenStrict) ||
(expression.arguments[0].type === 'Literal' &&
expression.arguments[0].value === null))
) {
return true
}
Expand Down

0 comments on commit 1a0161f

Please sign in to comment.