eslint-plugin-effector 0.8.1 → 0.9.1

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/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.1",
3
+ "version": "0.9.1",
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: {
@@ -85,21 +86,24 @@ module.exports = {
85
86
  continue;
86
87
  }
87
88
 
89
+ const parentNode = traverseParentByType(node, "VariableDeclarator", [
90
+ "Program",
91
+ ]);
92
+
88
93
  const resultSavedInVariable =
89
- node.parent.type === "VariableDeclarator";
94
+ parentNode?.type === "VariableDeclarator";
90
95
  if (!resultSavedInVariable) {
91
96
  continue;
92
97
  }
93
98
 
94
- const storeName = node.parent.id.name;
95
-
99
+ const storeName = parentNode.id.name;
96
100
  if (namingOf.store.isValid({ name: storeName, context })) {
97
101
  continue;
98
102
  }
99
103
 
100
104
  reportStoreNameConventionViolation({
101
105
  context,
102
- node: node.parent,
106
+ node: parentNode,
103
107
  storeName,
104
108
  });
105
109
  return;
@@ -117,6 +121,7 @@ module.exports = {
117
121
 
118
122
  const resultSavedInVariable =
119
123
  node.parent.type === "VariableDeclarator";
124
+
120
125
  if (!resultSavedInVariable) {
121
126
  return;
122
127
  }
@@ -140,13 +145,17 @@ module.exports = {
140
145
  if (
141
146
  STORE_IN_DOMAIN_CREATION_METHODS.includes(node.callee?.property?.name)
142
147
  ) {
148
+ const parentNode = traverseParentByType(node, "VariableDeclarator", [
149
+ "Program",
150
+ ]);
151
+
143
152
  const resultSavedInVariable =
144
- node.parent.type === "VariableDeclarator";
153
+ parentNode.type === "VariableDeclarator";
145
154
  if (!resultSavedInVariable) {
146
155
  return;
147
156
  }
148
157
 
149
- const storeName = node.parent.id.name;
158
+ const storeName = parentNode.id.name;
150
159
 
151
160
  if (namingOf.store.isValid({ name: storeName, context })) {
152
161
  return;
@@ -154,7 +163,7 @@ module.exports = {
154
163
 
155
164
  reportStoreNameConventionViolation({
156
165
  context,
157
- node: node.parent,
166
+ node: parentNode,
158
167
  storeName,
159
168
  });
160
169
  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;