-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (67 loc) · 1.94 KB
/
index.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
const ERROR_MSG_NO_DEFAULT_VALUE =
"Default-value parameters on methods are not allowed in ImmerReducer classes";
const ERROR_MSG_NO_OPTIONAL =
"Optional parameters on methods are not allowed in ImmerReducer classes";
const getClassBody = node => {
const classBody = node.body && node.body.body ? node.body.body : null;
if (
node.superClass &&
node.superClass.name === "ImmerReducer" &&
classBody &&
classBody.length > 0
) {
return classBody;
}
return null;
};
const getMethodsFromClassBody = classBody => {
return classBody.filter(
bodyEntry => bodyEntry.type === "MethodDefinition" && bodyEntry.value
);
};
const getParamsFromMethod = method => {
if (!method || !method.value || !method.value.params) return null;
return method.value.params;
};
module.exports = {
configs: {
recommended: {
plugins: ["immer-reducer"],
rules: {
"immer-reducer/no-optional-or-default-value-params": 1
}
}
},
rules: {
"no-optional-or-default-value-params": {
create(context) {
return {
ClassDeclaration(node) {
const classBody = getClassBody(node);
if (!classBody) return;
const methods = getMethodsFromClassBody(classBody);
if (!methods) return;
methods.forEach(method => {
const methodParams = getParamsFromMethod(method);
if (!methodParams) return;
methodParams.forEach(param => {
if (param.type === "AssignmentPattern") {
context.report({
node: param,
message: ERROR_MSG_NO_DEFAULT_VALUE
});
}
if (param.optional) {
context.report({
node: param,
message: ERROR_MSG_NO_OPTIONAL
});
}
});
});
}
};
}
}
}
};