-
Notifications
You must be signed in to change notification settings - Fork 16
/
no-ambiguity-target.js
74 lines (67 loc) · 2.07 KB
/
no-ambiguity-target.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 {
extractImportedFromEffector,
} = require("../../utils/extract-imported-from-effector");
const { traverseParentByType } = require("../../utils/traverse-parent-by-type");
const { createLinkToRule } = require("../../utils/create-link-to-rule");
module.exports = {
meta: {
type: "problem",
docs: {
description: "Forbids ambiguity targets in `sample` and `guard`",
category: "Quality",
recommended: true,
url: createLinkToRule("no-ambiguity-target"),
},
messages: {
ambiguityTarget:
"Method `{{ methodName }}` returns `target` and assigns the result to a variable. Consider removing one of them.",
},
schema: [],
},
create(context) {
const importedFromEffector = new Map();
return {
ImportDeclaration(node) {
extractImportedFromEffector(importedFromEffector, node);
},
CallExpression(node) {
const POSSIBLE_USELESS_METHODS = ["sample", "guard"];
for (const method of POSSIBLE_USELESS_METHODS) {
const localMethod = importedFromEffector.get(method);
if (!localMethod) {
continue;
}
const isEffectorMethod = node?.callee?.name === localMethod;
if (!isEffectorMethod) {
continue;
}
const configHasTarget = node?.arguments?.[0]?.properties?.some(
(prop) => prop?.key.name === "target"
);
if (!configHasTarget) {
continue;
}
const resultAssignedInVariable = traverseParentByType(
node,
"VariableDeclarator",
{ stopOnTypes: ["BlockStatement"] }
);
const resultPartOfChain = traverseParentByType(
node,
"ObjectExpression"
);
if (resultAssignedInVariable || resultPartOfChain) {
context.report({
node,
messageId: "ambiguityTarget",
data: {
methodName: node?.callee?.name,
},
});
continue;
}
}
},
};
},
};