Skip to content

Commit

Permalink
Fix: require-await ignore async generators (fixes #12459) (#13048)
Browse files Browse the repository at this point in the history
* Fix: require-await should ignore async generators (fixes #12459)

* Chore: docs update and test update

* Docs: fixed typo for generator to generators

Co-Authored-By: Kai Cataldo <kai@kaicataldo.com>

* Chore: added more tests for async generators

Co-authored-by: Kai Cataldo <kai@kaicataldo.com>
  • Loading branch information
anikethsaha and kaicataldo committed Mar 28, 2020
1 parent 920465b commit eb1a43c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/rules/require-await.md
Expand Up @@ -18,6 +18,7 @@ async function fetchData(processDataItem) {

Asynchronous functions that don't use `await` might not need to be asynchronous functions and could be the unintentional result of refactoring.

Note: this rule ignores async generator functions. This is because generators yield rather than return a value and async generators might yield all the values of another async generator without ever actually needing to use await.

## Rule Details

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/require-await.js
Expand Up @@ -68,7 +68,7 @@ module.exports = {
* @returns {void}
*/
function exitFunction(node) {
if (node.async && !scopeInfo.hasAwait && !astUtils.isEmptyFunction(node)) {
if (!node.generator && node.async && !scopeInfo.hasAwait && !astUtils.isEmptyFunction(node)) {
context.report({
node,
loc: astUtils.getFunctionHeadLoc(node, sourceCode),
Expand Down
30 changes: 30 additions & 0 deletions tests/lib/rules/require-await.js
Expand Up @@ -55,7 +55,37 @@ ruleTester.run("require-await", rule, {
}
`,
parser: require.resolve("../../fixtures/parsers/typescript-parsers/global-for-await-of")
},
{
code: "async function* run() { yield * anotherAsyncGenerator() }",
parserOptions: { ecmaVersion: 9 }
},
{
code: `async function* run() {
await new Promise(resolve => setTimeout(resolve, 100));
yield 'Hello';
console.log('World');
}
`,
parserOptions: { ecmaVersion: 9 }
},
{
code: "async function* run() { }",
parserOptions: { ecmaVersion: 9 }
},
{
code: "const foo = async function *(){}",
parserOptions: { ecmaVersion: 9 }
},
{
code: 'const foo = async function *(){ console.log("bar") }',
parserOptions: { ecmaVersion: 9 }
},
{
code: 'async function* run() { console.log("bar") }',
parserOptions: { ecmaVersion: 9 }
}

],
invalid: [
{
Expand Down

0 comments on commit eb1a43c

Please sign in to comment.