-
Notifications
You must be signed in to change notification settings - Fork 16
/
no-watch.js
53 lines (47 loc) · 1.38 KB
/
no-watch.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
const {
traverseNestedObjectNode,
} = require("../../utils/traverse-nested-object-node");
const { createLinkToRule } = require("../../utils/create-link-to-rule");
const { nodeTypeIs } = require("../../utils/node-type-is");
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "Avoid `.watch` calls on any Effector unit or operator",
category: "Quality",
recommended: true,
url: createLinkToRule("no-watch"),
},
messages: {
abusiveCall:
"Method `.watch` leads to imperative code. Try to replace it with operators (`sample`, `guard`, etc) or use the `target` parameter of the operators.",
},
schema: [],
},
create(context) {
const { parserServices } = context;
if (!parserServices.hasFullTypeInformation) {
// JavaScript-way https://github.com/effector/eslint-plugin/issues/48#issuecomment-931107829
return {};
}
return {
'CallExpression[callee.property.name="watch"]'(node) {
const object = traverseNestedObjectNode(node.callee?.object);
const isEffectorUnit = nodeTypeIs.unit({
node: object,
context,
});
if (!isEffectorUnit) {
return;
}
reportWatchCall({ context, node });
},
};
},
};
function reportWatchCall({ context, node }) {
context.report({
node,
messageId: "abusiveCall",
});
}