diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..c6812c16 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +; EditorConfig file: https://EditorConfig.org +; Install the "EditorConfig" plugin into your editor to use + +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 2 +trim_trailing_whitespace = true diff --git a/README.md b/README.md index 0ef515aa..1d9ae361 100644 --- a/README.md +++ b/README.md @@ -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** diff --git a/__tests__/prefer-await-to-then.js b/__tests__/prefer-await-to-then.js index 4d88a0f8..fefcd165 100644 --- a/__tests__/prefer-await-to-then.js +++ b/__tests__/prefer-await-to-then.js @@ -8,7 +8,7 @@ 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: [ @@ -16,6 +16,9 @@ ruleTester.run('prefer-await-to-then', rule, { '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())' ], @@ -30,12 +33,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 }] } ] }) diff --git a/docs/rules/prefer-await-to-then.md b/docs/rules/prefer-await-to-then.md index 4e40f2ea..fe1ffab0 100644 --- a/docs/rules/prefer-await-to-then.md +++ b/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 @@ -33,4 +33,14 @@ function exampleTwo() { .then(doSomethingElseAsync) .catch(errors) } + +function exampleThree() { + return myPromise + .catch(errors) +} + +function exampleFour() { + return myPromise + .finally(cleanup) +} ``` diff --git a/rules/prefer-await-to-then.js b/rules/prefer-await-to-then.js index d9bc6525..c82a818c 100644 --- a/rules/prefer-await-to-then.js +++ b/rules/prefer-await-to-then.js @@ -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' @@ -39,11 +39,16 @@ 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().' }) } }