From 6378bb9abb61a586976b915e7adac1431677a7f0 Mon Sep 17 00:00:00 2001 From: jp7837 Date: Sat, 9 May 2020 14:57:45 -0400 Subject: [PATCH 1/3] prefer-await-to-then should flag promise.catch/finally as well --- README.md | 2 +- __tests__/prefer-await-to-then.js | 11 ++++++++++- docs/rules/prefer-await-to-then.md | 12 +++++++++++- rules/prefer-await-to-then.js | 8 ++++---- 4 files changed, 26 insertions(+), 7 deletions(-) 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..bc4d8fbe 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,7 @@ 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 { somethingElse() })', 'something().then(async () => await somethingElse())' ], @@ -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 }] } ] }) 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 05c25af8..1dec4426 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' @@ -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().' }) } } From 71710382d8a3b4de34189b0fb36a53a3bb60ce2e Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Sat, 11 Apr 2020 17:56:38 +0800 Subject: [PATCH 2/3] - add `.editorconfig` to allow IDEs to set indent before linting --- .editorconfig | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 .editorconfig 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 From cd268e5ef5da037fe2486e2b35dccf9b7ae11c5f Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Thu, 9 Jul 2020 07:47:37 +0800 Subject: [PATCH 3/3] - Linting: Fix as per current prettier - Testing: Fix test (was not parsing) - Testing: Fix test (missing an error message) --- __tests__/prefer-await-to-then.js | 6 ++++-- rules/prefer-await-to-then.js | 7 ++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/__tests__/prefer-await-to-then.js b/__tests__/prefer-await-to-then.js index bc4d8fbe..fefcd165 100644 --- a/__tests__/prefer-await-to-then.js +++ b/__tests__/prefer-await-to-then.js @@ -16,7 +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 { somethingElse() })', + `a = async () => { + try { await something() } catch (error) { somethingElse() } + }`, 'something().then(async () => await somethingElse())' ], @@ -31,7 +33,7 @@ ruleTester.run('prefer-await-to-then', rule, { }, { code: 'function foo() { hey.then(function() { }).then(x).catch() }', - errors: [{ message }, { message }] + errors: [{ message }, { message }, { message }] }, { code: diff --git a/rules/prefer-await-to-then.js b/rules/prefer-await-to-then.js index 852b3665..c82a818c 100644 --- a/rules/prefer-await-to-then.js +++ b/rules/prefer-await-to-then.js @@ -40,7 +40,12 @@ module.exports = { } // 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')) { + 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()/catch()/finally().'