-
Notifications
You must be signed in to change notification settings - Fork 16
/
no-useless-methods.js
94 lines (84 loc) · 2.54 KB
/
no-useless-methods.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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 useless calls of `sample` and `guard`",
category: "Quality",
recommended: true,
url: createLinkToRule("no-useless-methods"),
},
messages: {
uselessMethod:
"Method `{{ methodName }}` does nothing in this case. You should assign the result to variable or pass `target` to it.",
},
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 resultAssignedInVariable = traverseParentByType(
node,
"VariableDeclarator"
);
if (resultAssignedInVariable) {
continue;
}
const resultReturnedFromFactory = traverseParentByType(
node,
"ReturnStatement"
);
if (resultReturnedFromFactory) {
continue;
}
const resultPartOfChain = traverseParentByType(
node,
"ObjectExpression"
);
if (resultPartOfChain) {
continue;
}
const configHasTarget = node?.arguments?.[0]?.properties?.some(
(prop) => prop?.key.name === "target"
);
if (configHasTarget) {
continue;
}
const resultIsWatched = node?.parent?.property?.name === "watch";
if (resultIsWatched) {
continue;
}
const resultIsArgument = node?.parent?.type === "CallExpression";
if (resultIsArgument) {
continue;
}
context.report({
node,
messageId: "uselessMethod",
data: {
methodName: node?.callee?.name,
},
});
}
},
};
},
};