eslint-plugin-effector 0.8.0 → 0.9.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/README.md CHANGED
@@ -89,6 +89,12 @@ This preset contains rules, which enforce _future-effector_ code-style.
89
89
  - [effector/no-forward](rules/no-forward/no-forward.md)
90
90
  - [effector/no-guard](rules/no-guard/no-guard.md)
91
91
 
92
+ #### plugin:effector/patronum
93
+
94
+ This preset is recommended for projects that use [Patronum](https://patronum.effector.dev/).
95
+
96
+ - [effector/no-patronum-debug](rules/no-patronum-debug/no-patronum-debug.md)
97
+
92
98
  ## Maintenance
93
99
 
94
100
  ### Release flow
@@ -0,0 +1,5 @@
1
+ module.exports = {
2
+ rules: {
3
+ "effector/no-patronum-debug": "error",
4
+ },
5
+ };
package/config/react.js CHANGED
@@ -2,6 +2,6 @@ module.exports = {
2
2
  rules: {
3
3
  "effector/enforce-gate-naming-convention": "error",
4
4
  "effector/mandatory-scope-binding": "error",
5
- "effector/prefer-useUnit": "warning",
5
+ "effector/prefer-useUnit": "warn",
6
6
  },
7
7
  };
package/index.js CHANGED
@@ -17,11 +17,13 @@ module.exports = {
17
17
  "no-guard": require("./rules/no-guard/no-guard"),
18
18
  "mandatory-scope-binding": require("./rules/mandatory-scope-binding/mandatory-scope-binding"),
19
19
  "prefer-useUnit": require("./rules/prefer-useUnit/prefer-useUnit"),
20
+ "no-patronum-debug": require("./rules/no-patronum-debug/no-patronum-debug"),
20
21
  },
21
22
  configs: {
22
23
  recommended: require("./config/recommended"),
23
24
  scope: require("./config/scope"),
24
25
  react: require("./config/react"),
25
26
  future: require("./config/future"),
27
+ patronum: require("./config/patronum"),
26
28
  },
27
29
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-effector",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "Enforcing best practices for Effector",
5
5
  "keywords": [
6
6
  "eslint",
@@ -11,6 +11,7 @@ const {
11
11
  } = require("../../utils/get-corrected-store-name");
12
12
  const { createLinkToRule } = require("../../utils/create-link-to-rule");
13
13
  const { nodeTypeIs } = require("../../utils/node-type-is");
14
+ const { traverseParentByType } = require("../../utils/traverse-parent-by-type");
14
15
 
15
16
  module.exports = {
16
17
  meta: {
@@ -75,31 +76,31 @@ module.exports = {
75
76
  // Store creation with method
76
77
  const STORE_CREATION_METHODS = ["createStore", "restore", "combine"];
77
78
  for (const method of STORE_CREATION_METHODS) {
78
- const localMethod = importedFromEffector.get(method);
79
+ const localMethod = importedFromEffector.get(method);
79
80
  if (!localMethod) {
80
81
  continue;
81
82
  }
82
-
83
+
83
84
  const isEffectorStoreCreation = node.callee.name === localMethod;
84
85
  if (!isEffectorStoreCreation) {
85
86
  continue;
86
87
  }
87
88
 
88
- const resultSavedInVariable =
89
- node.parent.type === "VariableDeclarator";
89
+ const parentNode = traverseParentByType(node, "VariableDeclarator", ["Program"]);
90
+
91
+ const resultSavedInVariable = parentNode.type === "VariableDeclarator";
90
92
  if (!resultSavedInVariable) {
91
93
  continue;
92
94
  }
93
95
 
94
- const storeName = node.parent.id.name;
95
-
96
+ const storeName = parentNode.id.name;
96
97
  if (namingOf.store.isValid({ name: storeName, context })) {
97
98
  continue;
98
99
  }
99
100
 
100
101
  reportStoreNameConventionViolation({
101
102
  context,
102
- node: node.parent,
103
+ node: parentNode,
103
104
  storeName,
104
105
  });
105
106
  return;
@@ -115,8 +116,8 @@ module.exports = {
115
116
  return;
116
117
  }
117
118
 
118
- const resultSavedInVariable =
119
- node.parent.type === "VariableDeclarator";
119
+ const resultSavedInVariable = node.parent.type === "VariableDeclarator";
120
+
120
121
  if (!resultSavedInVariable) {
121
122
  return;
122
123
  }
@@ -140,13 +141,15 @@ module.exports = {
140
141
  if (
141
142
  STORE_IN_DOMAIN_CREATION_METHODS.includes(node.callee?.property?.name)
142
143
  ) {
143
- const resultSavedInVariable =
144
- node.parent.type === "VariableDeclarator";
144
+
145
+ const parentNode = traverseParentByType(node, "VariableDeclarator", ["Program"]);
146
+
147
+ const resultSavedInVariable = parentNode.type === "VariableDeclarator";
145
148
  if (!resultSavedInVariable) {
146
149
  return;
147
150
  }
148
151
 
149
- const storeName = node.parent.id.name;
152
+ const storeName = parentNode.id.name;
150
153
 
151
154
  if (namingOf.store.isValid({ name: storeName, context })) {
152
155
  return;
@@ -154,7 +157,7 @@ module.exports = {
154
157
 
155
158
  reportStoreNameConventionViolation({
156
159
  context,
157
- node: node.parent,
160
+ node: parentNode,
158
161
  storeName,
159
162
  });
160
163
  return;
@@ -0,0 +1,126 @@
1
+ const { createLinkToRule } = require("../../utils/create-link-to-rule");
2
+ const { extractImportedFrom } = require("../../utils/extract-imported-from");
3
+
4
+ module.exports = {
5
+ meta: {
6
+ type: "suggestion",
7
+ docs: {
8
+ description: "Disallow the use of patronum `debug`",
9
+ category: "Quality",
10
+ recommended: false,
11
+ url: createLinkToRule("no-patronum-debug"),
12
+ },
13
+ messages: {
14
+ noPatronumDebug: "Unexpected patronum `debug` statement",
15
+ removePatronumDebug: "Remove this `debug` from patronum",
16
+ },
17
+ schema: [],
18
+ hasSuggestions: true,
19
+ },
20
+ create(context) {
21
+ const importedFromPatronum = new Map();
22
+ const importNodes = new Map();
23
+
24
+ return {
25
+ ImportDeclaration(node) {
26
+ extractImportedFrom({
27
+ packageName: ["patronum", "patronum/debug"],
28
+ importMap: importedFromPatronum,
29
+ nodeMap: importNodes,
30
+ node,
31
+ });
32
+ },
33
+ CallExpression(node) {
34
+ const currentMethodName = node.callee?.name ?? node.callee?.object.name;
35
+ const importedDebugFromPatronum = importedFromPatronum.get("debug");
36
+
37
+ if (currentMethodName !== importedDebugFromPatronum) {
38
+ return;
39
+ }
40
+
41
+ context.report({
42
+ messageId: "noPatronumDebug",
43
+ node,
44
+ suggest: [
45
+ {
46
+ messageId: "removePatronumDebug",
47
+ *fix(fixer) {
48
+ yield* removeDebugFromPatronum({
49
+ fixer,
50
+ node,
51
+ context,
52
+ importNodes,
53
+ });
54
+ },
55
+ },
56
+ ],
57
+ });
58
+ },
59
+ };
60
+ },
61
+ };
62
+
63
+ function* removeDebugFromPatronum({
64
+ fixer,
65
+ node,
66
+ context,
67
+ importNodes,
68
+ targetMethod = "debug",
69
+ }) {
70
+ const sourceCode = context.getSourceCode();
71
+ const startToken = sourceCode.getTokenBefore(node);
72
+
73
+ // remove line with debug
74
+ yield fixer.removeRange([startToken.range[1], node.range[1] + 1]);
75
+
76
+ const importDebugNode = importNodes.get(targetMethod);
77
+
78
+ if (!importDebugNode) {
79
+ return null;
80
+ }
81
+
82
+ // remove import with debug
83
+ const importParentNode = importDebugNode.parent;
84
+
85
+ /**
86
+ * import { debug } from 'patronum'
87
+ * import { debug } from 'patronum/debug'
88
+ */
89
+ if (importParentNode.specifiers.length === 1) {
90
+ yield fixer.removeRange([
91
+ importParentNode.range[0],
92
+ importParentNode.range[1] + 1,
93
+ ]);
94
+
95
+ return null;
96
+ }
97
+
98
+ const amountImportFromPatronum = importParentNode.specifiers.length;
99
+ const importLast = importParentNode.specifiers[amountImportFromPatronum - 1];
100
+
101
+ const filterTokenComma = { filter: (token) => token.value === "," };
102
+
103
+ /**
104
+ * import { debug, timeout } from 'patronum'
105
+ * import { condition, debug, throttle } from 'patronum'
106
+ */
107
+ if (importDebugNode !== importLast) {
108
+ const prevNode = sourceCode.getTokenBefore(importDebugNode);
109
+ const comma = sourceCode.getTokenAfter(importDebugNode, filterTokenComma);
110
+
111
+ yield fixer.removeRange([prevNode.range[1], importDebugNode.range[0]]);
112
+ yield fixer.remove(importDebugNode);
113
+ yield fixer.remove(comma);
114
+
115
+ return null;
116
+ }
117
+
118
+ /**
119
+ * import { condition, debug } from 'patronum'
120
+ */
121
+ const comma = sourceCode.getTokenBefore(importDebugNode, filterTokenComma);
122
+
123
+ yield fixer.removeRange([comma.range[1], importDebugNode.range[0]]);
124
+ yield fixer.remove(importDebugNode);
125
+ yield fixer.remove(comma);
126
+ }
@@ -15,7 +15,7 @@ module.exports = {
15
15
  },
16
16
  messages: {
17
17
  abusiveCall:
18
- "Method `.watch` leads to imperative code. Try to replace it with operators (`sample`, `guard`, etc) or use the `target` parameter of the operators.",
18
+ "Method `.watch` leads to imperative code. Try to replace it with operator (`sample`) or use the `target` parameter of the operator.",
19
19
  },
20
20
  schema: [],
21
21
  },
@@ -1,5 +1,9 @@
1
1
  function extractImportedFrom({ importMap, nodeMap, node, packageName }) {
2
- if (node.source.value === packageName) {
2
+ const normalizePackageName = Array.isArray(packageName)
3
+ ? packageName
4
+ : [packageName];
5
+
6
+ if (normalizePackageName.includes(node.source.value)) {
3
7
  for (const s of node.specifiers) {
4
8
  if (s.type === "ImportDefaultSpecifier") {
5
9
  continue;