-
Notifications
You must be signed in to change notification settings - Fork 16
/
strict-effect-handlers.js
74 lines (64 loc) · 1.93 KB
/
strict-effect-handlers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const { createLinkToRule } = require("../../utils/create-link-to-rule");
const { is } = require("../../utils/is");
module.exports = {
meta: {
type: "problem",
docs: {
description:
"Forbids mix of async functions and effects calls in effect handlers.",
category: "Quality",
recommended: true,
url: createLinkToRule("strict-effect-handlers"),
},
messages: {
mixedCallsInHandler:
"Handler of effect `{{ effectName }}` can lead to scope loosing in Fork API.",
mixedCallsInFunction:
"Function `{{ functionName }}` can lead to scope loosing in Fork API.",
},
schema: [],
},
create(context) {
function onEffectHandler(node) {
if (!node.body?.body) {
return;
}
const calledNodes = node.body.body
.filter((bodyNode) => bodyNode.expression?.type === "AwaitExpression")
.map((awaitNode) => ({
node: awaitNode.expression.argument.callee,
context,
}));
const hasEffects = calledNodes.some(is.effect);
const hasRegularAsyncFunctions = calledNodes.some(is.not.effect);
const hasError = hasEffects && hasRegularAsyncFunctions;
if (!hasError) {
return;
}
const isEffectHandler = is.effect({
node: node.parent?.parent,
context,
});
if (isEffectHandler) {
const effectName = node.parent?.parent?.id?.name ?? "Unknown";
context.report({
node: node.parent,
messageId: "mixedCallsInHandler",
data: { effectName },
});
} else {
const functionName = node.id?.name ?? "Unknown";
context.report({
node,
messageId: "mixedCallsInFunction",
data: { functionName },
});
}
}
return {
ArrowFunctionExpression: onEffectHandler,
FunctionExpression: onEffectHandler,
FunctionDeclaration: onEffectHandler,
};
},
};