-
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 5 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
50 changes: 50 additions & 0 deletions
50
plugins/eslint-plugin-aws-toolkits/lib/rules/no-inline-async-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,50 @@ | ||
| /*! | ||
| * 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' | ||
|
|
||
| export const errMsg = 'Avoid using async methods with .forEach as it leads to race conditions' | ||
|
|
||
| function isAsyncFunction(node: TSESTree.CallExpressionArgument): boolean { | ||
| return ( | ||
| (node.type === AST_NODE_TYPES.ArrowFunctionExpression || node.type === AST_NODE_TYPES.FunctionExpression) && | ||
| node.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 && | ||
| isAsyncFunction(node.arguments[0]) | ||
| ) { | ||
| return context.report({ | ||
| node: node, | ||
| messageId: 'errMsg', | ||
| }) | ||
| } | ||
| }, | ||
| } | ||
| }, | ||
| }) as unknown as Rule.RuleModule |
24 changes: 24 additions & 0 deletions
24
plugins/eslint-plugin-aws-toolkits/test/rules/no-inline-async-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,24 @@ | ||
| /*! | ||
| * 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-inline-async-foreach' | ||
| import { getRuleTester } from '../testUtil' | ||
|
|
||
| getRuleTester().run('no-inline-async-foreach', rules['no-inline-async-foreach'], { | ||
| valid: [ | ||
| 'list.forEach((a) => a * a)', | ||
| 'list.forEach(asyncFunctionOrNot)', | ||
| 'list.forEach(() => console.log(x))', | ||
| 'list.forEach(function () {})', | ||
| ], | ||
|
|
||
| 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] }, | ||
| ], | ||
| }) | ||
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.
what about
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.
See recent commits, also added a few more edge cases.