Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer-await-to-then should flag promise.catch/finally as well #188

Merged
merged 1 commit into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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