Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: require-await ignore async generators (fixes #12459) #13048

Merged
merged 4 commits into from Mar 28, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 generator might yield all the values of another async generator without ever actually needing to use await.
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved

## 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
13 changes: 13 additions & 0 deletions tests/lib/rules/require-await.js
Expand Up @@ -55,6 +55,19 @@ 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 }
}
],
invalid: [
Expand Down