@dineroregnskab/eslint-plugin-custom-rules 2.1.1 → 2.1.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dineroregnskab/eslint-plugin-custom-rules",
3
- "version": "2.1.1",
3
+ "version": "2.1.3",
4
4
  "description": "ESLint plugin with custom rules for Dinero Regnskab",
5
5
  "main": "eslint-plugin-custom-rules.js",
6
6
  "scripts": {},
@@ -1,9 +1,11 @@
1
+ const eslintUtils = require("eslint-utils");
2
+
1
3
  module.exports = {
2
4
  meta: {
3
5
  type: "suggestion",
4
6
  docs: {
5
7
  description:
6
- "Replace `first()` with `take(1)` in TypeScript files.",
8
+ "Replace `first()` with `take(1)` and require `filter()` to be used with `take(1)` in pipe.",
7
9
  },
8
10
  fixable: "code",
9
11
  schema: [],
@@ -17,14 +19,99 @@ module.exports = {
17
19
  if (
18
20
  node.callee.type === "Identifier" &&
19
21
  node.callee.name === "first" &&
20
- node.arguments.length === 0 // Ensure `first` is called with no arguments
22
+ node.arguments.length === 0
21
23
  ) {
22
24
  context.report({
23
25
  node,
24
26
  message:
25
27
  "Replace `first()` with `take(1)` to avoid errors if no value is ever emitted.",
28
+ fix(fixer) {
29
+ return fixer.replaceText(node, "take(1)");
30
+ },
26
31
  });
27
32
  }
33
+
34
+ // Check for `pipe()` with `take(1)`
35
+ if (
36
+ node.callee.type === "MemberExpression" &&
37
+ node.callee.property.name === "pipe"
38
+ ) {
39
+ const pipeArguments = node.arguments;
40
+
41
+ // Check if `take(1)` is present
42
+ const hasTakeOne = pipeArguments.some(
43
+ (arg) =>
44
+ arg.type === "CallExpression" &&
45
+ arg.callee.name === "take" &&
46
+ arg.arguments.length === 1 &&
47
+ arg.arguments[0].value === 1
48
+ );
49
+
50
+ // Check if `filter()` is present
51
+ const hasFilter = pipeArguments.some(
52
+ (arg) =>
53
+ arg.type === "CallExpression" &&
54
+ arg.callee.name === "filter"
55
+ );
56
+
57
+ // If `take(1)` is used without `filter()`, autofix to add `filter()`
58
+ if (hasTakeOne && !hasFilter) {
59
+ context.report({
60
+ node,
61
+ message:
62
+ "Using `take(1)` requires `filter()` to be used in the same pipe to avoid `null` or `undefined` values which will trigger an error.",
63
+ fix(fixer) {
64
+ const sourceCode = context.getSourceCode();
65
+ const firstArg = pipeArguments[0];
66
+
67
+ const fixes = [
68
+ // Add the `filter()` inside the pipe
69
+ fixer.insertTextBefore(
70
+ firstArg,
71
+ "filter((value) => Boolean(value ?? false)), "
72
+ ),
73
+ ];
74
+
75
+ // Check if `filter` is imported, if not, add the import
76
+ const importDeclaration =
77
+ sourceCode.ast.body.find(
78
+ (node) =>
79
+ node.type === "ImportDeclaration" &&
80
+ node.source.value ===
81
+ "rxjs/operators"
82
+ );
83
+
84
+ if (importDeclaration) {
85
+ const filterImported =
86
+ importDeclaration.specifiers.some(
87
+ (specifier) =>
88
+ specifier.imported.name ===
89
+ "filter"
90
+ );
91
+
92
+ if (!filterImported) {
93
+ fixes.push(
94
+ fixer.insertTextBefore(
95
+ importDeclaration.specifiers[0],
96
+ "filter, "
97
+ )
98
+ );
99
+ }
100
+ } else {
101
+ // Add a new import if none exist
102
+ fixes.push(
103
+ fixer.insertTextBefore(
104
+ sourceCode.ast.body[0],
105
+ 'import { filter } from "rxjs/operators";\n'
106
+ )
107
+ );
108
+ }
109
+
110
+ return fixes;
111
+ },
112
+ });
113
+ }
114
+ }
28
115
  },
29
116
  };
30
117
  },