Skip to content

Commit 53dfbcf

Browse files
committed
chore: add no-async-foreach-callback eslint rule
1 parent 33b1249 commit 53dfbcf

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

packages/eslint-plugin/src/configs/recommended.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const recommended = {
55
"@calcom/eslint/deprecated-imports": "error",
66
"@calcom/eslint/avoid-web-storage": "error",
77
"@calcom/eslint/avoid-prisma-client-import-for-enums": "error",
8+
"@calcom/eslint/no-async-foreach-callback": "error",
89
},
910
};
1011

packages/eslint-plugin/src/rules/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export default {
55
"deprecated-imports": require("./deprecated-imports").default,
66
"avoid-web-storage": require("./avoid-web-storage").default,
77
"avoid-prisma-client-import-for-enums": require("./avoid-prisma-client-import-for-enums").default,
8+
"no-async-foreach-callback": require("./no-async-foreach-callback").default,
89
} as ESLint.Plugin["rules"];
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { ESLintUtils } from "@typescript-eslint/utils";
2+
3+
const createRule = ESLintUtils.RuleCreator((name) => `https://developer.cal.com/eslint/rule/${name}`);
4+
5+
const rule = createRule({
6+
create(context) {
7+
return {
8+
CallExpression(node) {
9+
const { callee } = node;
10+
if (
11+
callee.type === "MemberExpression" &&
12+
callee.property.type === "Identifier" &&
13+
callee.property.name === "forEach"
14+
) {
15+
const firstArg = node.arguments[0];
16+
if (
17+
(firstArg.type === "ArrowFunctionExpression" || firstArg.type === "FunctionExpression") &&
18+
firstArg.async
19+
) {
20+
context.report({
21+
node,
22+
messageId: "async-foreach",
23+
});
24+
}
25+
}
26+
},
27+
};
28+
},
29+
name: "no-async-foreach-callback",
30+
meta: {
31+
docs: {
32+
description: "Disallow using an async function as a forEach callback",
33+
recommended: "warn",
34+
},
35+
messages: {
36+
"async-foreach": "forEach callbacks should not be async, did you mean to use Promise.all?",
37+
},
38+
type: "suggestion",
39+
schema: [],
40+
},
41+
defaultOptions: [],
42+
});
43+
44+
export default rule;

0 commit comments

Comments
 (0)