eslint-plugin-effector 0.12.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js
CHANGED
|
@@ -5,6 +5,7 @@ module.exports = {
|
|
|
5
5
|
"no-getState": require("./rules/no-getState/no-getState"),
|
|
6
6
|
"no-unnecessary-duplication": require("./rules/no-unnecessary-duplication/no-unnecessary-duplication"),
|
|
7
7
|
"prefer-sample-over-forward-with-mapping": require("./rules/prefer-sample-over-forward-with-mapping/prefer-sample-over-forward-with-mapping"),
|
|
8
|
+
"no-duplicate-clock-or-source-array-values": require("./rules/no-duplicate-clock-or-source-array-values/no-duplicate-clock-or-source-array-values"),
|
|
8
9
|
"no-useless-methods": require("./rules/no-useless-methods/no-useless-methods"),
|
|
9
10
|
"no-ambiguity-target": require("./rules/no-ambiguity-target/no-ambiguity-target"),
|
|
10
11
|
"no-watch": require("./rules/no-watch/no-watch"),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-effector",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "Enforcing best practices for Effector",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|
|
@@ -17,10 +17,10 @@
|
|
|
17
17
|
"access": "public"
|
|
18
18
|
},
|
|
19
19
|
"engines": {
|
|
20
|
-
"node": "^16 || ^17 || ^18 || ^19 || ^20"
|
|
20
|
+
"node": "^16 || ^17 || ^18 || ^19 || ^20 || ^21"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
|
-
"effector": "
|
|
23
|
+
"effector": "^23",
|
|
24
24
|
"eslint": "7 || 8"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
@@ -94,7 +94,7 @@ module.exports = {
|
|
|
94
94
|
}
|
|
95
95
|
|
|
96
96
|
const parentNode = traverseParentByType(node, "VariableDeclarator", {
|
|
97
|
-
stopOnTypes: ["Program", "BlockStatement"],
|
|
97
|
+
stopOnTypes: ["Program", "BlockStatement", "Property"],
|
|
98
98
|
});
|
|
99
99
|
|
|
100
100
|
const resultSavedInVariable =
|
|
@@ -153,7 +153,7 @@ module.exports = {
|
|
|
153
153
|
STORE_IN_DOMAIN_CREATION_METHODS.includes(node.callee?.property?.name)
|
|
154
154
|
) {
|
|
155
155
|
const parentNode = traverseParentByType(node, "VariableDeclarator", {
|
|
156
|
-
stopOnTypes: ["Program", "BlockStatement"],
|
|
156
|
+
stopOnTypes: ["Program", "BlockStatement", "Property"],
|
|
157
157
|
});
|
|
158
158
|
|
|
159
159
|
const resultSavedInVariable =
|
package/rules/no-duplicate-clock-or-source-array-values/no-duplicate-clock-or-source-array-values.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
const { extractImportedFrom } = require("../../utils/extract-imported-from");
|
|
2
|
+
const { createLinkToRule } = require("../../utils/create-link-to-rule");
|
|
3
|
+
const { method } = require("../../utils/method");
|
|
4
|
+
const {} = require("../../utils/traverse-nested-object-node");
|
|
5
|
+
|
|
6
|
+
module.exports = {
|
|
7
|
+
meta: {
|
|
8
|
+
type: "problem",
|
|
9
|
+
hasSuggestions: true,
|
|
10
|
+
docs: {
|
|
11
|
+
description: "Forbids unit duplicates on `source` and `clock``",
|
|
12
|
+
category: "Quality",
|
|
13
|
+
recommended: true,
|
|
14
|
+
url: createLinkToRule("no-duplicate-clock-or-source-array-values"),
|
|
15
|
+
},
|
|
16
|
+
messages: {
|
|
17
|
+
duplicatesInClock: "Clock contains duplicate units - {{ memberPath }}.",
|
|
18
|
+
duplicatesInSource: "Source contains duplicate units - {{ memberPath }}.",
|
|
19
|
+
removeDuplicate: "Remove duplicate {{ memberPath }}.",
|
|
20
|
+
},
|
|
21
|
+
schema: [],
|
|
22
|
+
},
|
|
23
|
+
create(context) {
|
|
24
|
+
const importedFromEffector = new Map();
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
ImportDeclaration(node) {
|
|
28
|
+
extractImportedFrom({
|
|
29
|
+
importMap: importedFromEffector,
|
|
30
|
+
node,
|
|
31
|
+
packageName: "effector",
|
|
32
|
+
});
|
|
33
|
+
},
|
|
34
|
+
CallExpression(node) {
|
|
35
|
+
if (
|
|
36
|
+
method.isNot(["sample", "guard"], {
|
|
37
|
+
node,
|
|
38
|
+
importMap: importedFromEffector,
|
|
39
|
+
})
|
|
40
|
+
) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const properties = getSourceOrClockProperties(node.arguments[0]);
|
|
45
|
+
|
|
46
|
+
properties.forEach(({ key, value }) => {
|
|
47
|
+
const propType = key.name;
|
|
48
|
+
const elements = value.elements;
|
|
49
|
+
|
|
50
|
+
const usedUnits = new Set();
|
|
51
|
+
|
|
52
|
+
for (const node of elements) {
|
|
53
|
+
const memberPath = createMemberExpressionPath(node);
|
|
54
|
+
|
|
55
|
+
if (usedUnits.has(memberPath)) {
|
|
56
|
+
const messageId = getMessageIdByPropType(propType);
|
|
57
|
+
|
|
58
|
+
context.report({
|
|
59
|
+
node,
|
|
60
|
+
messageId,
|
|
61
|
+
data: {
|
|
62
|
+
memberPath,
|
|
63
|
+
},
|
|
64
|
+
suggest: [
|
|
65
|
+
{
|
|
66
|
+
messageId: "removeDuplicate",
|
|
67
|
+
data: { memberPath },
|
|
68
|
+
fix(fixer) {
|
|
69
|
+
return fixer.remove(node);
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
usedUnits.add(memberPath);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
function createMemberExpressionPath(node, chain = "") {
|
|
87
|
+
const compactStrings = (...args) => args.filter(Boolean).join(".");
|
|
88
|
+
|
|
89
|
+
if (node.type === "MemberExpression") {
|
|
90
|
+
const propertyName = node.property.name;
|
|
91
|
+
|
|
92
|
+
const updatedChain = compactStrings(propertyName, chain);
|
|
93
|
+
|
|
94
|
+
return createMemberExpressionPath(node.object, updatedChain);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
chain = compactStrings(node.name, chain);
|
|
98
|
+
|
|
99
|
+
// remove last dot
|
|
100
|
+
return chain.slice(0, -1);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function getSourceOrClockProperties(node) {
|
|
104
|
+
if (node.type !== "ObjectExpression") return [];
|
|
105
|
+
|
|
106
|
+
const allowedProps = ["clock", "source"];
|
|
107
|
+
|
|
108
|
+
const isClockOrSourceArray = (prop) => {
|
|
109
|
+
return (
|
|
110
|
+
allowedProps.includes(prop.key.name) &&
|
|
111
|
+
prop.value.type === "ArrayExpression"
|
|
112
|
+
);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
return node.properties.filter(isClockOrSourceArray);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function getMessageIdByPropType(propType) {
|
|
119
|
+
return propType === "clock" ? "duplicatesInClock" : "duplicatesInSource";
|
|
120
|
+
}
|
package/utils/node-type-is.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { ESLintUtils } = require(
|
|
1
|
+
const { ESLintUtils } = require("@typescript-eslint/utils");
|
|
2
2
|
|
|
3
3
|
function hasType({ node, possibleTypes, context, from }) {
|
|
4
4
|
try {
|
|
@@ -24,13 +24,27 @@ const nodeTypeIs = {
|
|
|
24
24
|
effect: (opts) =>
|
|
25
25
|
hasType({ ...opts, possibleTypes: ["Effect"], from: "effector" }),
|
|
26
26
|
store: (opts) =>
|
|
27
|
-
hasType({
|
|
27
|
+
hasType({
|
|
28
|
+
...opts,
|
|
29
|
+
possibleTypes: ["Store", "StoreWritable"],
|
|
30
|
+
from: "effector",
|
|
31
|
+
}),
|
|
28
32
|
event: (opts) =>
|
|
29
|
-
hasType({
|
|
33
|
+
hasType({
|
|
34
|
+
...opts,
|
|
35
|
+
possibleTypes: ["Event", "EventCallable"],
|
|
36
|
+
from: "effector",
|
|
37
|
+
}),
|
|
30
38
|
unit: (opts) =>
|
|
31
39
|
hasType({
|
|
32
40
|
...opts,
|
|
33
|
-
possibleTypes: [
|
|
41
|
+
possibleTypes: [
|
|
42
|
+
"Effect",
|
|
43
|
+
"Store",
|
|
44
|
+
"Event",
|
|
45
|
+
"EventCallable",
|
|
46
|
+
"StoreWritable",
|
|
47
|
+
],
|
|
34
48
|
from: "effector",
|
|
35
49
|
}),
|
|
36
50
|
gate: (opts) =>
|
|
@@ -58,13 +72,27 @@ const nodeTypeIs = {
|
|
|
58
72
|
from: "effector",
|
|
59
73
|
}),
|
|
60
74
|
store: (opts) =>
|
|
61
|
-
!hasType({
|
|
75
|
+
!hasType({
|
|
76
|
+
...opts,
|
|
77
|
+
possibleTypes: ["Store", "StoreWritable"],
|
|
78
|
+
from: "effector",
|
|
79
|
+
}),
|
|
62
80
|
event: (opts) =>
|
|
63
|
-
!hasType({
|
|
81
|
+
!hasType({
|
|
82
|
+
...opts,
|
|
83
|
+
possibleTypes: ["Event", "EventCallable"],
|
|
84
|
+
from: "effector",
|
|
85
|
+
}),
|
|
64
86
|
unit: (opts) =>
|
|
65
87
|
!hasType({
|
|
66
88
|
...opts,
|
|
67
|
-
possibleTypes: [
|
|
89
|
+
possibleTypes: [
|
|
90
|
+
"Effect",
|
|
91
|
+
"Store",
|
|
92
|
+
"Event",
|
|
93
|
+
"EventCallable",
|
|
94
|
+
"StoreWritable",
|
|
95
|
+
],
|
|
68
96
|
from: "effector",
|
|
69
97
|
}),
|
|
70
98
|
gate: (opts) =>
|