-
Notifications
You must be signed in to change notification settings - Fork 752
refactor(general): avoid async method inside forEach (WIP)
#6264
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
91ba8b1
remove async within forEach
Hweinstock 890de9e
fully remove forEach async
Hweinstock f7b6d12
fix parameter mismatch
Hweinstock ec20f3a
implement lint rule for inline async
Hweinstock 25c42c7
add to eslint
Hweinstock 669a9cd
catch async defined functions
Hweinstock 0cc7906
clean up helper functions
Hweinstock 104a85f
add more test cases
Hweinstock a0e4713
add test cases for remaining cases
Hweinstock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
plugins/eslint-plugin-aws-toolkits/lib/rules/no-async-in-foreach.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /*! | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { ESLintUtils, TSESTree } from '@typescript-eslint/utils' | ||
| import { AST_NODE_TYPES } from '@typescript-eslint/types' | ||
| import { Rule } from 'eslint' | ||
| import { RuleContext } from '@typescript-eslint/utils/ts-eslint' | ||
|
|
||
| export const errMsg = 'Avoid using async methods with .forEach as it leads to race conditions' | ||
|
|
||
| function isFunctionExpression( | ||
| node: TSESTree.Node | ||
| ): node is TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression { | ||
| return node.type === AST_NODE_TYPES.ArrowFunctionExpression || node.type === AST_NODE_TYPES.FunctionExpression | ||
| } | ||
|
|
||
| function isAsyncFunction<T extends string, T2 extends readonly unknown[]>( | ||
| context: RuleContext<T, T2>, | ||
| funcNode: TSESTree.CallExpressionArgument | ||
| ) { | ||
| if (funcNode.type === AST_NODE_TYPES.Identifier) { | ||
| const scope = context.sourceCode.getScope(funcNode) | ||
| const maybeFNode = | ||
| scope.variables.find((v) => v.name === funcNode.name)?.defs.find((d) => !!d)?.node ?? undefined | ||
| // function declartions Ex. async function f() {} | ||
| if ( | ||
| maybeFNode && | ||
| (isFunctionExpression(maybeFNode) || maybeFNode.type === AST_NODE_TYPES.FunctionDeclaration) && | ||
| maybeFNode.async | ||
| ) { | ||
| return true | ||
| } | ||
| // variable-style function declarations Ex. const f = async function () {} | ||
| if ( | ||
| maybeFNode && | ||
| maybeFNode.type === AST_NODE_TYPES.VariableDeclarator && | ||
| maybeFNode.init && | ||
| isFunctionExpression(maybeFNode.init) && | ||
| maybeFNode.init.async | ||
| ) { | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
| return ( | ||
| (funcNode.type === AST_NODE_TYPES.ArrowFunctionExpression || | ||
| funcNode.type === AST_NODE_TYPES.FunctionExpression) && | ||
| funcNode.async | ||
| ) | ||
| } | ||
|
|
||
| export default ESLintUtils.RuleCreator.withoutDocs({ | ||
| meta: { | ||
| docs: { | ||
| description: 'disallow inlining async functions with .forEach', | ||
| recommended: 'recommended', | ||
| }, | ||
| messages: { | ||
| errMsg, | ||
| }, | ||
| type: 'problem', | ||
| fixable: 'code', | ||
| schema: [], | ||
| }, | ||
| defaultOptions: [], | ||
| create(context) { | ||
| return { | ||
| CallExpression(node: TSESTree.CallExpression) { | ||
| if ( | ||
| node.callee.type === AST_NODE_TYPES.MemberExpression && | ||
| node.callee.property.type === AST_NODE_TYPES.Identifier && | ||
| node.callee.property.name === 'forEach' && | ||
| node.arguments.length >= 1 | ||
| ) { | ||
| if (isAsyncFunction(context, node.arguments[0])) { | ||
| return context.report({ | ||
| node: node, | ||
| messageId: 'errMsg', | ||
| }) | ||
| } | ||
| } | ||
| }, | ||
| } | ||
| }, | ||
| }) as unknown as Rule.RuleModule |
43 changes: 43 additions & 0 deletions
43
plugins/eslint-plugin-aws-toolkits/test/rules/no-async-in-foreach.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /*! | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { rules } from '../../index' | ||
| import { errMsg } from '../../lib/rules/no-async-in-foreach' | ||
| import { getRuleTester } from '../testUtil' | ||
|
|
||
| getRuleTester().run('no-async-in-foreach', rules['no-async-in-foreach'], { | ||
| valid: [ | ||
| 'list.forEach((a) => a * a)', | ||
| 'list.forEach(asyncFunctionOrNot)', | ||
| 'list.forEach(() => console.log(x))', | ||
| 'list.forEach(function () {})', | ||
| 'function f(){} \n list.forEach(f)', | ||
| 'const f = function () {} \n list.forEach(f)', | ||
| ], | ||
|
|
||
| invalid: [ | ||
| { code: 'list.forEach(async (a) => await Promise.resolve(a * a))', errors: [errMsg] }, | ||
| { code: 'list.forEach(async (a: any) => console.log(x))', errors: [errMsg] }, | ||
| { code: 'list.forEach((a) => a.forEach(async (b) => a * b))', errors: [errMsg] }, | ||
| { code: 'list.forEach(async function () {})', errors: [errMsg] }, | ||
| { code: 'async function f(){} \n list.forEach(f)', errors: [errMsg] }, | ||
| { code: 'const f = async () => {} \n list.forEach(f)', errors: [errMsg] }, | ||
| { code: 'const f = async function () {} \n list.forEach(f)', errors: [errMsg] }, | ||
| { code: 'class c { \n public async f() {} \n } \n [].forEach((new c().f))', errors: [errMsg] }, | ||
| { | ||
| code: 'class c { \n public async f() {} \n } \n const c2 = new c() \n list.forEach(c2.f)', | ||
| errors: [errMsg], | ||
| }, | ||
| { code: 'function f() { \n return async function () {}} \n [].forEach(f())', errors: [errMsg] }, | ||
| { | ||
| code: 'function f() { \n return new (class c { \n public async f2() {} \n })().f2 \n } \n list.forEach(f())', | ||
| errors: [errMsg], | ||
| }, | ||
| { | ||
| code: 'function f() { \n return function f2() { \n return function f3() { \n return function f4() { \n return function f5() { \n return async function f6() { \n \n } \n } \n } \n } \n } \n } \n list.forEach(f()()()()())', | ||
| errors: [errMsg], | ||
| }, | ||
| ], | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we just ban
forEach? it is kind of pointless.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, I think banning it out right has a few advantages. Additionally, determining if a function is async is definitely non-trivial. This work is moved to #6281