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

Coverage #196

Merged
merged 7 commits into from Apr 9, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion CONTRIBUTING.md
Expand Up @@ -9,7 +9,6 @@ project, you agree to abide by its terms.
## Table of Contents

<!-- START doctoc generated TOC please keep comment here to allow auto update -->

<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

- [How can I contribute?](#how-can-i-contribute)
Expand Down
2 changes: 1 addition & 1 deletion README.md
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
3 changes: 3 additions & 0 deletions __tests__/catch-or-return.js
Expand Up @@ -123,6 +123,9 @@ ruleTester.run('catch-or-return', rule, {
code: 'frank().then(go).finally()',
options: [{ terminationMethod: ['catch', 'finally'] }],
},

// for coverage
'nonPromiseExpressionStatement();',
],

invalid: [
Expand Down
1 change: 1 addition & 0 deletions __tests__/param-names.js
Expand Up @@ -17,6 +17,7 @@ ruleTester.run('param-names', rule, {
'new Promise(resolve => {})',
'new Promise((resolve, reject) => {})',
'new Promise(() => {})',
'new NonPromise()',
],

invalid: [
Expand Down
1 change: 1 addition & 0 deletions __tests__/prefer-await-to-callbacks.js
Expand Up @@ -17,6 +17,7 @@ ruleTester.run('prefer-await-to-callbacks', rule, {
'async function hi() { await thing().catch() }',
'dbConn.on("error", err => { console.error(err) })',
'dbConn.once("error", err => { console.error(err) })',
'heart(something => {})',
],

invalid: [
Expand Down
16 changes: 14 additions & 2 deletions __tests__/prefer-await-to-then.js
Expand Up @@ -8,15 +8,19 @@ 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 (error) { somethingElse() }
}`,
'something().then(async () => await somethingElse())',
'function foo() { hey.somethingElse(x => {}) }',
],

invalid: [
Expand All @@ -30,12 +34,20 @@ ruleTester.run('prefer-await-to-then', rule, {
},
{
code: 'function foo() { hey.then(function() { }).then(x).catch() }',
errors: [{ message }, { message }],
errors: [{ message }, { message }, { message }],
},
{
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
@@ -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)
}
```