-
Notifications
You must be signed in to change notification settings - Fork 16
/
no-unnecessary-combination.js
94 lines (80 loc) · 2.67 KB
/
no-unnecessary-combination.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 { createLinkToRule } = require("../../utils/create-link-to-rule");
module.exports = {
meta: {
type: "problem",
docs: {
description:
"Forbids unnecessary combinations in `clock`, `source` and `forward`",
category: "Quality",
recommended: true,
url: createLinkToRule("no-unnecessary-combination"),
},
messages: {
unnecessaryCombination:
"Method {{ methodName }} is used under the hood, you can omit it.",
},
schema: [],
},
create(context) {
const importedFromEffector = new Map();
return {
ImportDeclaration(node) {
extractImportedFromEffector(importedFromEffector, node);
},
CallExpression(node) {
const METHODS_WITH_POSSIBLE_UNNECESSARY_COMBINATION = [
"sample",
"guard",
"forward",
];
const CONFIG_ARG_PROPERTIES = ["source", "clock", "from"];
function toLocalMethod(method) {
return importedFromEffector.get(method);
}
const UNNECESSARY_METHODS = {
source: ["combine", "merge"].map(toLocalMethod).filter(Boolean),
clock: ["merge"].map(toLocalMethod).filter(Boolean),
from: ["merge"].map(toLocalMethod).filter(Boolean),
};
for (const method of METHODS_WITH_POSSIBLE_UNNECESSARY_COMBINATION) {
const localMethod = importedFromEffector.get(method);
if (!localMethod) {
continue;
}
const isEffectorMethod = node?.callee?.name === localMethod;
if (!isEffectorMethod) {
continue;
}
const candidates =
node?.arguments?.[0]?.properties?.filter((n) =>
CONFIG_ARG_PROPERTIES.includes(n.key.name)
) ?? [];
if (candidates.length === 0) {
continue;
}
for (const candidate of candidates) {
const candidateName = candidate?.value?.callee?.name;
const argProp = candidate?.key?.name;
if (!candidateName || !argProp) {
continue;
}
const localUnnecessaryMethods = UNNECESSARY_METHODS[argProp];
const UnnecessaryMethodIsEffectorMethod =
localUnnecessaryMethods.some((m) => m === candidateName);
if (!UnnecessaryMethodIsEffectorMethod) {
continue;
}
context.report({
node: candidate?.value,
messageId: "unnecessaryCombination",
data: { methodName: candidateName },
});
}
}
},
};
},
};