Skip to content

Commit 0cc7906

Browse files
committed
clean up helper functions
1 parent 669a9cd commit 0cc7906

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

plugins/eslint-plugin-aws-toolkits/lib/rules/no-inline-async-foreach.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,38 @@ import { RuleContext } from '@typescript-eslint/utils/ts-eslint'
1010

1111
export const errMsg = 'Avoid using async methods with .forEach as it leads to race conditions'
1212

13+
function isFunctionExpression(
14+
node: TSESTree.Node
15+
): node is TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression {
16+
return node.type === AST_NODE_TYPES.ArrowFunctionExpression || node.type === AST_NODE_TYPES.FunctionExpression
17+
}
18+
1319
function isAsyncFunction<T extends string, T2 extends readonly unknown[]>(
1420
context: RuleContext<T, T2>,
1521
funcNode: TSESTree.CallExpressionArgument
1622
) {
1723
if (funcNode.type === AST_NODE_TYPES.Identifier) {
18-
console.log('is identifier')
1924
const scope = context.sourceCode.getScope(funcNode)
2025
const maybeFNode =
2126
scope.variables.find((v) => v.name === funcNode.name)?.defs.find((d) => !!d)?.node ?? undefined
22-
console.log(maybeFNode)
27+
// function declartions Ex. async function f() {}
2328
if (
2429
maybeFNode &&
25-
(maybeFNode.type === AST_NODE_TYPES.ArrowFunctionExpression ||
26-
maybeFNode.type === AST_NODE_TYPES.FunctionExpression ||
27-
maybeFNode.type === AST_NODE_TYPES.FunctionDeclaration) &&
30+
(isFunctionExpression(maybeFNode) || maybeFNode.type === AST_NODE_TYPES.FunctionDeclaration) &&
2831
maybeFNode.async
2932
) {
3033
return true
3134
}
35+
// variable-style function declarations Ex. const f = async function () {}
36+
if (
37+
maybeFNode &&
38+
maybeFNode.type === AST_NODE_TYPES.VariableDeclarator &&
39+
maybeFNode.init &&
40+
isFunctionExpression(maybeFNode.init) &&
41+
maybeFNode.init.async
42+
) {
43+
return true
44+
}
3245
return false
3346
}
3447
return (

plugins/eslint-plugin-aws-toolkits/test/rules/no-inline-async-foreach.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,5 @@ getRuleTester().run('no-inline-async-foreach', rules['no-inline-async-foreach'],
2525
{ code: 'async function f(){} \n list.forEach(f)', errors: [errMsg] },
2626
{ code: 'const f = async () => {} \n list.forEach(f)', errors: [errMsg] },
2727
{ code: 'const f = async function () {} \n list.forEach(f)', errors: [errMsg] },
28-
{ code: 'const f = function () {} \n list.forEach(f)', errors: [errMsg] },
2928
],
3029
})

0 commit comments

Comments
 (0)