eslint-plugin-effector 0.16.0 → 0.17.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.
Files changed (53) hide show
  1. package/README.md +24 -37
  2. package/dist/index.cjs +1259 -0
  3. package/dist/index.d.cts +177 -0
  4. package/dist/index.d.mts +178 -0
  5. package/dist/index.mjs +1233 -0
  6. package/package.json +71 -17
  7. package/.nvmrc +0 -1
  8. package/config/future.js +0 -7
  9. package/config/patronum.js +0 -5
  10. package/config/react.js +0 -7
  11. package/config/recommended.js +0 -15
  12. package/config/scope.js +0 -6
  13. package/index.js +0 -31
  14. package/rules/enforce-effect-naming-convention/enforce-effect-naming-convention.js +0 -143
  15. package/rules/enforce-gate-naming-convention/enforce-gate-naming-convention.js +0 -122
  16. package/rules/enforce-store-naming-convention/enforce-store-naming-convention.js +0 -205
  17. package/rules/keep-options-order/config.js +0 -3
  18. package/rules/keep-options-order/keep-options-order.js +0 -107
  19. package/rules/mandatory-scope-binding/mandatory-scope-binding.js +0 -81
  20. package/rules/no-ambiguity-target/no-ambiguity-target.js +0 -74
  21. package/rules/no-duplicate-clock-or-source-array-values/no-duplicate-clock-or-source-array-values.js +0 -124
  22. package/rules/no-duplicate-on/no-duplicate-on.js +0 -137
  23. package/rules/no-forward/no-forward.js +0 -73
  24. package/rules/no-getState/no-getState.js +0 -50
  25. package/rules/no-guard/no-guard.js +0 -78
  26. package/rules/no-patronum-debug/no-patronum-debug.js +0 -133
  27. package/rules/no-unnecessary-combination/no-unnecessary-combination.js +0 -88
  28. package/rules/no-unnecessary-duplication/no-unnecessary-duplication.js +0 -115
  29. package/rules/no-useless-methods/no-useless-methods.js +0 -93
  30. package/rules/no-watch/no-watch.js +0 -61
  31. package/rules/prefer-sample-over-forward-with-mapping/prefer-sample-over-forward-with-mapping.js +0 -111
  32. package/rules/prefer-useUnit/prefer-useUnit.js +0 -56
  33. package/rules/require-pickup-in-persist/require-pickup-in-persist.js +0 -47
  34. package/rules/strict-effect-handlers/strict-effect-handlers.js +0 -76
  35. package/utils/are-nodes-same-in-text.js +0 -22
  36. package/utils/builders.js +0 -19
  37. package/utils/create-link-to-rule.js +0 -5
  38. package/utils/extract-config.js +0 -26
  39. package/utils/extract-imported-from.js +0 -18
  40. package/utils/get-corrected-store-name.js +0 -45
  41. package/utils/get-nested-object-name.js +0 -18
  42. package/utils/get-store-name-convention.js +0 -6
  43. package/utils/is.js +0 -39
  44. package/utils/method.js +0 -23
  45. package/utils/naming.js +0 -47
  46. package/utils/node-is-type.js +0 -5
  47. package/utils/node-type-is.js +0 -106
  48. package/utils/react.js +0 -214
  49. package/utils/read-example.js +0 -63
  50. package/utils/replace-by-sample.js +0 -98
  51. package/utils/traverse-nested-object-node.js +0 -9
  52. package/utils/traverse-parent-by-type.js +0 -15
  53. package/utils/validate-store-name-convention.js +0 -13
package/utils/react.js DELETED
@@ -1,214 +0,0 @@
1
- // borrowed from
2
- // https://github.com/facebook/react/blob/main/packages/eslint-plugin-react-hooks/src/RulesOfHooks.js
3
-
4
- /**
5
- * Catch all identifiers that begin with "use" followed by an uppercase Latin
6
- * character to exclude identifiers like "user".
7
- */
8
-
9
- function isHookName(s) {
10
- return /^use[A-Z0-9].*$/.test(s);
11
- }
12
-
13
- /**
14
- * We consider hooks to be a hook name identifier or a member expression
15
- * containing a hook name.
16
- */
17
-
18
- function isHook(node) {
19
- if (node.type === "Identifier") {
20
- return isHookName(node.name);
21
- } else if (
22
- node.type === "MemberExpression" &&
23
- !node.computed &&
24
- isHook(node.property)
25
- ) {
26
- const obj = node.object;
27
- const isPascalCaseNameSpace = /^[A-Z].*/;
28
- return obj.type === "Identifier" && isPascalCaseNameSpace.test(obj.name);
29
- } else {
30
- return false;
31
- }
32
- }
33
-
34
- /**
35
- * Checks if the node is a React component name. React component names must
36
- * always start with a non-lowercase letter. So `MyComponent` or `_MyComponent`
37
- * are valid component names for instance.
38
- */
39
- function isComponentName(node) {
40
- if (node.type === "Identifier") {
41
- return !/^[a-z]/.test(node.name);
42
- } else {
43
- return false;
44
- }
45
- }
46
-
47
- function isReactFunction(node, functionName) {
48
- return (
49
- node.name === functionName ||
50
- (node.type === "MemberExpression" &&
51
- node.object.name === "React" &&
52
- node.property.name === functionName)
53
- );
54
- }
55
-
56
- /**
57
- * Checks if the node is a callback argument of forwardRef. This render function
58
- * should follow the rules of hooks.
59
- */
60
-
61
- function isForwardRefCallback(node) {
62
- return !!(
63
- node.parent &&
64
- node.parent.callee &&
65
- isReactFunction(node.parent.callee, "forwardRef")
66
- );
67
- }
68
-
69
- /**
70
- * Checks if the node is a callback argument of React.memo. This anonymous
71
- * functional component should follow the rules of hooks.
72
- */
73
-
74
- function isMemoCallback(node) {
75
- return !!(
76
- node.parent &&
77
- node.parent.callee &&
78
- isReactFunction(node.parent.callee, "memo")
79
- );
80
- }
81
-
82
- function isInsideReactComponent(node) {
83
- while (node) {
84
- const functionName = getFunctionName(node);
85
- if (functionName) {
86
- if (isComponentName(functionName) || isHook(functionName)) {
87
- return true;
88
- }
89
- }
90
- if (isForwardRefCallback(node) || isMemoCallback(node)) {
91
- return true;
92
- }
93
-
94
- if (isClass(node) && !isClassComponent(node)) {
95
- return false;
96
- }
97
-
98
- if (isClassComponent(node)) {
99
- return true;
100
- }
101
-
102
- node = node.parent;
103
- }
104
- return false;
105
- }
106
-
107
- function isClass(node) {
108
- return node?.type === "ClassDeclaration";
109
- }
110
-
111
- function isClassComponent(node) {
112
- if (!node?.superClass) {
113
- return false;
114
- }
115
- if (node?.superClass?.type === "MemberExpression") {
116
- return (
117
- node?.superClass?.object?.name === "React" &&
118
- /^(Pure)?Component$/.test(node?.superClass?.property?.name)
119
- );
120
- }
121
- if (node?.superClass?.type === "Identifier") {
122
- return /^(Pure)?Component$/.test(node?.superClass?.name);
123
- }
124
-
125
- return false;
126
- }
127
-
128
- function isInsideReactHook(node) {
129
- while (node) {
130
- const functionName = getFunctionName(node);
131
- if (functionName) {
132
- if (isHook(functionName)) {
133
- return true;
134
- }
135
- }
136
- node = node.parent;
137
- }
138
- return false;
139
- }
140
-
141
- module.exports = {
142
- isInsideReactComponent,
143
- isInsideReactHook,
144
- };
145
-
146
- /**
147
- * Gets the static name of a function AST node. For function declarations it is
148
- * easy. For anonymous function expressions it is much harder. If you search for
149
- * `IsAnonymousFunctionDefinition()` in the ECMAScript spec you'll find places
150
- * where JS gives anonymous function expressions names. We roughly detect the
151
- * same AST nodes with some exceptions to better fit our use case.
152
- */
153
-
154
- function getFunctionName(node) {
155
- if (
156
- node.type === "FunctionDeclaration" ||
157
- (node.type === "FunctionExpression" && node.id)
158
- ) {
159
- // function useHook() {}
160
- // const whatever = function useHook() {};
161
- //
162
- // Function declaration or function expression names win over any
163
- // assignment statements or other renames.
164
- return node.id;
165
- } else if (
166
- node.type === "FunctionExpression" ||
167
- node.type === "ArrowFunctionExpression"
168
- ) {
169
- if (
170
- node.parent.type === "VariableDeclarator" &&
171
- node.parent.init === node
172
- ) {
173
- // const useHook = () => {};
174
- return node.parent.id;
175
- } else if (
176
- node.parent.type === "AssignmentExpression" &&
177
- node.parent.right === node &&
178
- node.parent.operator === "="
179
- ) {
180
- // useHook = () => {};
181
- return node.parent.left;
182
- } else if (
183
- node.parent.type === "Property" &&
184
- node.parent.value === node &&
185
- !node.parent.computed
186
- ) {
187
- // {useHook: () => {}}
188
- // {useHook() {}}
189
- return node.parent.key;
190
-
191
- // NOTE: We could also support `ClassProperty` and `MethodDefinition`
192
- // here to be pedantic. However, hooks in a class are an anti-pattern. So
193
- // we don't allow it to error early.
194
- //
195
- // class {useHook = () => {}}
196
- // class {useHook() {}}
197
- } else if (
198
- node.parent.type === "AssignmentPattern" &&
199
- node.parent.right === node &&
200
- !node.parent.computed
201
- ) {
202
- // const {useHook = () => {}} = {};
203
- // ({useHook = () => {}} = {});
204
- //
205
- // Kinda clowny, but we'd said we'd follow spec convention for
206
- // `IsAnonymousFunctionDefinition()` usage.
207
- return node.parent.left;
208
- } else {
209
- return undefined;
210
- }
211
- } else {
212
- return undefined;
213
- }
214
- }
@@ -1,63 +0,0 @@
1
- const { readFileSync } = require("fs");
2
- const { join } = require("path");
3
- const glob = require("glob");
4
-
5
- function readExample(dirname, exampleName) {
6
- return readFileSync(join(dirname, "examples", exampleName)).toString();
7
- }
8
-
9
- function getCorrectExamples(dirname, config = {}) {
10
- const { ext, namesOnly = true } = config;
11
- const pattern = `correct-*.${resolveExtension(ext)}`;
12
- const correct = glob.sync(join(dirname, "examples", pattern));
13
-
14
- let result = correct;
15
-
16
- if (namesOnly) {
17
- result = result.map((path) => {
18
- const rightSlashIdx = path.lastIndexOf("/");
19
-
20
- return path.slice(rightSlashIdx + 1);
21
- });
22
- }
23
-
24
- return result;
25
- }
26
-
27
- function resolveExtension(ext) {
28
- const DEFAULT_EXT = "js";
29
-
30
- if (Array.isArray(ext)) {
31
- if (ext.length === 0) {
32
- return DEFAULT_EXT;
33
- }
34
-
35
- if (ext.length === 1) {
36
- return ext[0];
37
- }
38
-
39
- return `{${ext.join(",")}}`;
40
- }
41
-
42
- return ext ?? DEFAULT_EXT;
43
- }
44
-
45
- function getIncorrectExamples(dirname, config = {}) {
46
- const { ext, namesOnly = true } = config;
47
- const pattern = `incorrect-*.${resolveExtension(ext)}`;
48
- const incorrect = glob.sync(join(dirname, "examples", pattern));
49
-
50
- let result = incorrect;
51
-
52
- if (namesOnly) {
53
- result = result.map((path) => {
54
- const rightSlashIdx = path.lastIndexOf("/");
55
-
56
- return path.slice(rightSlashIdx + 1);
57
- });
58
- }
59
-
60
- return result;
61
- }
62
-
63
- module.exports = { readExample, getCorrectExamples, getIncorrectExamples };
@@ -1,98 +0,0 @@
1
- const { buildObjectInText } = require("./builders");
2
-
3
- function* replaceGuardBySample(
4
- guardConfig,
5
- { fixer, node, context, importNodes }
6
- ) {
7
- let mapperFunctionNode = null;
8
-
9
- let clockNode = guardConfig.clock?.value;
10
- let targetNode = guardConfig.target?.value;
11
- let sourceNode = guardConfig.source?.value;
12
- let filterNode = guardConfig.filter?.value;
13
-
14
- if (
15
- targetNode?.type === "CallExpression" &&
16
- targetNode?.callee?.property?.name === "prepend"
17
- ) {
18
- mapperFunctionNode = targetNode?.arguments?.[0];
19
- targetNode = targetNode.callee.object;
20
- targetMapperUsed = true;
21
- }
22
-
23
- yield* replaceBySample(
24
- { clockNode, sourceNode, filterNode, mapperFunctionNode, targetNode },
25
- { node, fixer, context, importNodes, methodName: "guard" }
26
- );
27
- }
28
-
29
- function* replaceForwardBySample(
30
- forwardConfig,
31
- { fixer, node, context, importNodes }
32
- ) {
33
- let mapperFunctionNode = null;
34
-
35
- let clockMapperUsed = false;
36
- let targetMapperUsed = false;
37
-
38
- let clockNode = forwardConfig.from.value;
39
- let targetNode = forwardConfig.to.value;
40
-
41
- if (
42
- clockNode?.type === "CallExpression" &&
43
- clockNode?.callee?.property?.name === "map"
44
- ) {
45
- mapperFunctionNode = clockNode?.arguments?.[0];
46
- clockNode = clockNode.callee.object;
47
- clockMapperUsed = true;
48
- }
49
-
50
- if (
51
- targetNode?.type === "CallExpression" &&
52
- targetNode?.callee?.property?.name === "prepend"
53
- ) {
54
- mapperFunctionNode = targetNode?.arguments?.[0];
55
- targetNode = targetNode.callee.object;
56
- targetMapperUsed = true;
57
- }
58
-
59
- // We cannot apply two mappers in one sample
60
- // Let's revert mappers and use .map + .prepend
61
- if (clockMapperUsed && targetMapperUsed) {
62
- mapperFunctionNode = null;
63
- clockNode = forwardConfig.from.value;
64
- targetNode = forwardConfig.to.value;
65
- }
66
-
67
- yield* replaceBySample(
68
- { clockNode, mapperFunctionNode, targetNode },
69
- { node, fixer, context, importNodes, methodName: "forward" }
70
- );
71
- }
72
-
73
- function* replaceBySample(
74
- { clockNode, sourceNode, filterNode, mapperFunctionNode, targetNode },
75
- { node, fixer, context, importNodes, methodName }
76
- ) {
77
- yield fixer.replaceText(
78
- node,
79
- `sample(${buildObjectInText.fromMapOfNodes({
80
- properties: {
81
- clock: clockNode,
82
- source: sourceNode,
83
- filter: filterNode,
84
- fn: mapperFunctionNode,
85
- target: targetNode,
86
- },
87
- context,
88
- })})`
89
- );
90
-
91
- const importNode = importNodes.get(methodName);
92
-
93
- if (!importNodes.has("sample")) {
94
- yield fixer.insertTextAfter(importNode, ", sample");
95
- }
96
- }
97
-
98
- module.exports = { replaceForwardBySample, replaceGuardBySample };
@@ -1,9 +0,0 @@
1
- function traverseNestedObjectNode(node) {
2
- if (node.type === "MemberExpression") {
3
- return traverseNestedObjectNode(node.property);
4
- }
5
-
6
- return node;
7
- }
8
-
9
- module.exports = { traverseNestedObjectNode };
@@ -1,15 +0,0 @@
1
- function traverseParentByType(node, type, config) {
2
- const stopOnTypes = config?.stopOnTypes ?? [];
3
-
4
- if (!node || stopOnTypes.includes(node.type)) {
5
- return null;
6
- }
7
-
8
- if (node.type === type) {
9
- return node;
10
- }
11
-
12
- return traverseParentByType(node.parent, type, config);
13
- }
14
-
15
- module.exports = { traverseParentByType };
@@ -1,13 +0,0 @@
1
- const { getStoreNameConvention } = require("./get-store-name-convention");
2
-
3
- function validateStoreNameConvention(context) {
4
- const storeNameConvention = getStoreNameConvention(context);
5
-
6
- if (storeNameConvention !== "prefix" && storeNameConvention !== "postfix") {
7
- throw new Error(
8
- "Invalid Configuration of effector-plugin-eslint/enforce-store-naming-convention. The value should be equal to prefix or postfix."
9
- );
10
- }
11
- }
12
-
13
- module.exports = { validateStoreNameConvention };