diff --git a/packages/eslint-plugin/docs/rules/require-await.mdx b/packages/eslint-plugin/docs/rules/require-await.mdx index 5a88e5012e03..c2bd9f7a072d 100644 --- a/packages/eslint-plugin/docs/rules/require-await.mdx +++ b/packages/eslint-plugin/docs/rules/require-await.mdx @@ -12,14 +12,43 @@ 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 +`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 -Examples of **correct** code for this rule: + + + +```ts +async function returnNumber() { + return 1; +} + +async function* asyncGenerator() { + yield 1; +} + +const num = returnNumber(); +const callAsyncGenerator = () => asyncGenerator(); +``` + + + ```ts -async function returnsPromise1() { - return Promise.resolve(1); +function returnNumber() { + return 1; } -const returnsPromise2 = () => returnsPromise1(); +function* syncGenerator() { + yield 1; +} + +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(); +" +`;