@dineroregnskab/eslint-plugin-custom-rules 2.1.1 → 2.1.2
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
|
@@ -3,7 +3,7 @@ module.exports = {
|
|
|
3
3
|
type: "suggestion",
|
|
4
4
|
docs: {
|
|
5
5
|
description:
|
|
6
|
-
"Replace `first()` with `take(1)` in
|
|
6
|
+
"Replace `first()` with `take(1)` and require `filter()` to be used with `take(1)` in pipe.",
|
|
7
7
|
},
|
|
8
8
|
fixable: "code",
|
|
9
9
|
schema: [],
|
|
@@ -17,7 +17,7 @@ module.exports = {
|
|
|
17
17
|
if (
|
|
18
18
|
node.callee.type === "Identifier" &&
|
|
19
19
|
node.callee.name === "first" &&
|
|
20
|
-
node.arguments.length === 0
|
|
20
|
+
node.arguments.length === 0
|
|
21
21
|
) {
|
|
22
22
|
context.report({
|
|
23
23
|
node,
|
|
@@ -25,6 +25,39 @@ module.exports = {
|
|
|
25
25
|
"Replace `first()` with `take(1)` to avoid errors if no value is ever emitted.",
|
|
26
26
|
});
|
|
27
27
|
}
|
|
28
|
+
|
|
29
|
+
// Check for `pipe()` with `take(1)`
|
|
30
|
+
if (
|
|
31
|
+
node.callee.type === "MemberExpression" &&
|
|
32
|
+
node.callee.property.name === "pipe"
|
|
33
|
+
) {
|
|
34
|
+
const pipeArguments = node.arguments;
|
|
35
|
+
|
|
36
|
+
// Check if `take(1)` is present
|
|
37
|
+
const hasTakeOne = pipeArguments.some(
|
|
38
|
+
(arg) =>
|
|
39
|
+
arg.type === "CallExpression" &&
|
|
40
|
+
arg.callee.name === "take" &&
|
|
41
|
+
arg.arguments.length === 1 &&
|
|
42
|
+
arg.arguments[0].value === 1
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
// Check if `filter()` is present
|
|
46
|
+
const hasFilter = pipeArguments.some(
|
|
47
|
+
(arg) =>
|
|
48
|
+
arg.type === "CallExpression" &&
|
|
49
|
+
arg.callee.name === "filter"
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
// Report an error if `take(1)` is used without `filter()`
|
|
53
|
+
if (hasTakeOne && !hasFilter) {
|
|
54
|
+
context.report({
|
|
55
|
+
node,
|
|
56
|
+
message:
|
|
57
|
+
"Using `take(1)` requires `filter()` to be used in the same pipe to avoid `null` or `undefined` values which will trigger an error. Example using the nullish coalescing operator (??): `filter((value) => value ?? false),`",
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
28
61
|
},
|
|
29
62
|
};
|
|
30
63
|
},
|