Skip to content

Commit

Permalink
prefer-await-to-then should flag promise.catch/finally as well
Browse files Browse the repository at this point in the history
  • Loading branch information
jp7837 committed May 9, 2020
1 parent f9a9ee0 commit 6378bb9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ or start with the recommended rule set:
| [`no-new-statics`][no-new-statics] | Avoid calling `new` on a Promise static method | :bangbang: | :wrench: |
| [`no-return-in-finally`][no-return-in-finally] | Disallow return statements in `finally()` | :warning: | |
| [`valid-params`][valid-params] | Ensures the proper number of arguments are passed to Promise functions | :warning: | |
| [`prefer-await-to-then`][prefer-await-to-then] | Prefer `await` to `then()` for reading Promise values | :seven: | |
| [`prefer-await-to-then`][prefer-await-to-then] | Prefer `await` to `then()`/`catch()`/`finally()` for reading Promise values | :seven: | |
| [`prefer-await-to-callbacks`][prefer-await-to-callbacks] | Prefer async/await to the callback pattern | :seven: | |

**Key**
Expand Down
11 changes: 10 additions & 1 deletion __tests__/prefer-await-to-then.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ const ruleTester = new RuleTester({
}
})

const message = 'Prefer await to then().'
const message = 'Prefer await to then()/catch()/finally().'

ruleTester.run('prefer-await-to-then', rule, {
valid: [
'async function hi() { await thing() }',
'async function hi() { await thing().then() }',
'async function hi() { await thing().catch() }',
'a = async () => (await something())',
'a = async () => (try { await something() } catch { somethingElse() })',
'something().then(async () => await somethingElse())'
],

Expand All @@ -36,6 +37,14 @@ ruleTester.run('prefer-await-to-then', rule, {
code:
'async function a() { hey.then(function() { }).then(function() { }) }',
errors: [{ message }, { message }]
},
{
code: 'function foo() { hey.catch(x => {}) }',
errors: [{ message }]
},
{
code: 'function foo() { hey.finally(x => {}) }',
errors: [{ message }]
}
]
})
12 changes: 11 additions & 1 deletion docs/rules/prefer-await-to-then.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Prefer `await` to `then()` for reading Promise values (prefer-await-to-then)
# Prefer `await` to `then()`/`catch()`/`finally()` for reading Promise values (prefer-await-to-then)

#### Valid

Expand Down Expand Up @@ -33,4 +33,14 @@ function exampleTwo() {
.then(doSomethingElseAsync)
.catch(errors)
}

function exampleThree() {
return myPromise
.catch(errors)
}

function exampleFour() {
return myPromise
.finally(cleanup)
}
```
8 changes: 4 additions & 4 deletions rules/prefer-await-to-then.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Rule: prefer-await-to-then
* Discourage using then() and instead use async/await.
* Discourage using then()/catch()/finally() and instead use async/await.
*/

'use strict'
Expand Down Expand Up @@ -38,11 +38,11 @@ module.exports = {
return
}

// if you're a then expression then you're probably a promise
if (node.property && node.property.name === 'then') {
// if you're a then/catch/finally expression then you're probably a promise
if (node.property && (node.property.name === 'then' || node.property.name === 'catch' || node.property.name === 'finally')) {
context.report({
node: node.property,
message: 'Prefer await to then().'
message: 'Prefer await to then()/catch()/finally().'
})
}
}
Expand Down

0 comments on commit 6378bb9

Please sign in to comment.