From 251a0adaa29f8d17a63fee791d6b0cc5136a87a4 Mon Sep 17 00:00:00 2001 From: Sergey Kalmykov Date: Thu, 15 Aug 2024 00:43:51 +0300 Subject: [PATCH 1/4] docs: added note on async generator support in require-await --- packages/eslint-plugin/docs/rules/require-await.mdx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/eslint-plugin/docs/rules/require-await.mdx b/packages/eslint-plugin/docs/rules/require-await.mdx index 5a88e5012e03..4b07222243e9 100644 --- a/packages/eslint-plugin/docs/rules/require-await.mdx +++ b/packages/eslint-plugin/docs/rules/require-await.mdx @@ -12,6 +12,10 @@ import TabItem from '@theme/TabItem'; This rule extends the base [`eslint/require-await`](https://eslint.org/docs/rules/require-await) rule. It uses type information to allow promise-returning functions to be marked as `async` without containing an `await` expression. +:::note +Unlike the base rule, our implementation also supports [async generator functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function*). +::: + ## Examples Examples of **correct** code for this rule: From 387fbd695103b5d6b8d16adf329274b45ddc1c56 Mon Sep 17 00:00:00 2001 From: Sergey Kalmykov Date: Sat, 17 Aug 2024 00:13:19 +0300 Subject: [PATCH 2/4] docs: add a new example for the require-await rule --- packages/eslint-plugin/docs/rules/require-await.mdx | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/eslint-plugin/docs/rules/require-await.mdx b/packages/eslint-plugin/docs/rules/require-await.mdx index 4b07222243e9..d5e53893b153 100644 --- a/packages/eslint-plugin/docs/rules/require-await.mdx +++ b/packages/eslint-plugin/docs/rules/require-await.mdx @@ -25,5 +25,11 @@ async function returnsPromise1() { return Promise.resolve(1); } +async function* asyncGenerator() { + const num = await Promise.resolve(1); + yield 1 + num; +} + const returnsPromise2 = () => returnsPromise1(); +const callAsyncGenerator = () => asyncGenerator(); ``` From 3ac01683c085365e17d863dc6df34cc5cf9ff213 Mon Sep 17 00:00:00 2001 From: Sergey Kalmykov Date: Tue, 27 Aug 2024 01:18:20 +0300 Subject: [PATCH 3/4] docs: add examples of incorrect code --- .../docs/rules/require-await.mdx | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/require-await.mdx b/packages/eslint-plugin/docs/rules/require-await.mdx index d5e53893b153..2dbce14e1e76 100644 --- a/packages/eslint-plugin/docs/rules/require-await.mdx +++ b/packages/eslint-plugin/docs/rules/require-await.mdx @@ -18,11 +18,29 @@ Unlike the base rule, our implementation also supports [async generator function ## Examples -Examples of **correct** code for this rule: + + ```ts -async function returnsPromise1() { - return Promise.resolve(1); +async function returnNumber() { + return 1; +} + +async function* asyncGenerator() { + const num = Promise.resolve(1); + yield 1 + num; +} + +const num = returnNumber(); +const callAsyncGenerator = () => asyncGenerator(); +``` + + + + +```ts +function returnNumber() { + return 1; } async function* asyncGenerator() { @@ -30,6 +48,9 @@ async function* asyncGenerator() { yield 1 + num; } -const returnsPromise2 = () => returnsPromise1(); +const num = () => returnNumber(); const callAsyncGenerator = () => asyncGenerator(); ``` + + + From 7a9bb14926c410d94100c19eeddd5549cbcf4d1f Mon Sep 17 00:00:00 2001 From: Sergey Kalmykov Date: Tue, 27 Aug 2024 09:26:37 +0300 Subject: [PATCH 4/4] docs: update the note and the examples --- .../docs/rules/require-await.mdx | 14 ++++---- .../require-await.shot | 35 +++++++++++++++++++ 2 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 packages/eslint-plugin/tests/docs-eslint-output-snapshots/require-await.shot diff --git a/packages/eslint-plugin/docs/rules/require-await.mdx b/packages/eslint-plugin/docs/rules/require-await.mdx index 2dbce14e1e76..c2bd9f7a072d 100644 --- a/packages/eslint-plugin/docs/rules/require-await.mdx +++ b/packages/eslint-plugin/docs/rules/require-await.mdx @@ -13,7 +13,7 @@ This rule extends the base [`eslint/require-await`](https://eslint.org/docs/rule It uses type information to allow promise-returning functions to be marked as `async` without containing an `await` expression. :::note -Unlike the base rule, our implementation also supports [async generator functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function*). +`yield` expressions in [async generator functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function*) behave differently from sync generator functions (they unwrap promises), so the base rule never checks async generator functions. On the other hand, our rule uses type information and can detect async generator functions that both never use `await` and always yield non-promise values. ::: ## Examples @@ -27,8 +27,7 @@ async function returnNumber() { } async function* asyncGenerator() { - const num = Promise.resolve(1); - yield 1 + num; + yield 1; } const num = returnNumber(); @@ -43,13 +42,12 @@ function returnNumber() { return 1; } -async function* asyncGenerator() { - const num = await Promise.resolve(1); - yield 1 + num; +function* syncGenerator() { + yield 1; } -const num = () => returnNumber(); -const callAsyncGenerator = () => asyncGenerator(); +const num = returnNumber(); +const callSyncGenerator = () => syncGenerator(); ``` diff --git a/packages/eslint-plugin/tests/docs-eslint-output-snapshots/require-await.shot b/packages/eslint-plugin/tests/docs-eslint-output-snapshots/require-await.shot new file mode 100644 index 000000000000..a4584d5b8ad7 --- /dev/null +++ b/packages/eslint-plugin/tests/docs-eslint-output-snapshots/require-await.shot @@ -0,0 +1,35 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Validating rule docs require-await.mdx code examples ESLint output 1`] = ` +"Incorrect + +async function returnNumber() { +~~~~~~~~~~~~~~~~~~~~~~~~~~~ Async function 'returnNumber' has no 'await' expression. + return 1; +} + +async function* asyncGenerator() { +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Async generator function 'asyncGenerator' has no 'await' expression. + yield 1; +} + +const num = returnNumber(); +const callAsyncGenerator = () => asyncGenerator(); +" +`; + +exports[`Validating rule docs require-await.mdx code examples ESLint output 2`] = ` +"Correct + +function returnNumber() { + return 1; +} + +function* syncGenerator() { + yield 1; +} + +const num = returnNumber(); +const callSyncGenerator = () => syncGenerator(); +" +`;