eslint-plugin-crisp 1.7.18 → 1.7.20
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,40 @@
|
|
|
1
1
|
// Pre-compiled patterns for exclusion checks (avoid creating regex in loop)
|
|
2
2
|
const OBJECT_METHODS_PATTERN = /Object\.(keys|values|entries)\(/;
|
|
3
3
|
const ARRAY_METHODS_PATTERN = /Array\.(from)\(/;
|
|
4
|
+
const COMPARISON_OPERATORS = new Set([
|
|
5
|
+
"==",
|
|
6
|
+
"!=",
|
|
7
|
+
"===",
|
|
8
|
+
"!==",
|
|
9
|
+
"<",
|
|
10
|
+
"<=",
|
|
11
|
+
">",
|
|
12
|
+
">="
|
|
13
|
+
]);
|
|
14
|
+
|
|
15
|
+
function findGuardedMemberExpression(node, guardText, sourceCode) {
|
|
16
|
+
let currentNode = node;
|
|
17
|
+
|
|
18
|
+
while (currentNode) {
|
|
19
|
+
if (currentNode.type === "ChainExpression") {
|
|
20
|
+
currentNode = currentNode.expression;
|
|
21
|
+
} else if (currentNode.type === "CallExpression") {
|
|
22
|
+
currentNode = currentNode.callee;
|
|
23
|
+
} else if (currentNode.type === "BinaryExpression") {
|
|
24
|
+
currentNode = currentNode.left;
|
|
25
|
+
} else if (currentNode.type === "MemberExpression") {
|
|
26
|
+
if (sourceCode.getText(currentNode.object) === guardText) {
|
|
27
|
+
return currentNode;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
currentNode = currentNode.object;
|
|
31
|
+
} else {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
4
38
|
|
|
5
39
|
export default {
|
|
6
40
|
meta: {
|
|
@@ -16,54 +50,67 @@ export default {
|
|
|
16
50
|
create(context) {
|
|
17
51
|
const sourceCode = context.sourceCode;
|
|
18
52
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
// Search for patterns like `foo || {}).bar`, `foo || {})[`, \
|
|
26
|
-
// `foo || []).bar` or `foo || [])[`
|
|
27
|
-
// Notice: do not match if the value after the dot is "filter", "find", \
|
|
28
|
-
// "findIndex", "map", "reduce", "length <", "length === 0", or \
|
|
29
|
-
// "length == 0", as the version with optional chaining would be unsafe
|
|
30
|
-
/(.+?)\s\|\|\s(\{\}|\[\])\)(\[|\.(?!some\b|filter\b|find\b|findIndex\b|reduce\b|map\b|indexOf\b|length\s*<|length\s*===\s*0|length\s*==\s*0))\w*/g
|
|
31
|
-
];
|
|
53
|
+
// Search for patterns like `foo || {}).bar`, `foo || {})[`, \
|
|
54
|
+
// `foo || []).bar` or `foo || [])[`
|
|
55
|
+
// Notice: do not match if the value after the dot is "filter", "find", \
|
|
56
|
+
// "findIndex", "map", "reduce", "length <", "length === 0", or \
|
|
57
|
+
// "length == 0", as the version with optional chaining would be unsafe
|
|
58
|
+
const fallbackPattern = /(.+?)\s\|\|\s(\{\}|\[\])\)(\[|\.(?!some\b|filter\b|find\b|findIndex\b|reduce\b|map\b|indexOf\b|length\s*<|length\s*===\s*0|length\s*==\s*0))\w*/g;
|
|
32
59
|
|
|
33
60
|
return {
|
|
34
|
-
|
|
35
|
-
|
|
61
|
+
LogicalExpression(node) {
|
|
62
|
+
if (
|
|
63
|
+
node.operator !== "&&" ||
|
|
64
|
+
(node.right.type === "BinaryExpression" &&
|
|
65
|
+
COMPARISON_OPERATORS.has(node.right.operator))
|
|
66
|
+
) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
36
69
|
|
|
37
|
-
|
|
38
|
-
|
|
70
|
+
const guardText = sourceCode.getText(node.left);
|
|
71
|
+
const memberExpression = findGuardedMemberExpression(
|
|
72
|
+
node.right,
|
|
73
|
+
guardText,
|
|
74
|
+
sourceCode
|
|
75
|
+
);
|
|
39
76
|
|
|
40
|
-
|
|
41
|
-
|
|
77
|
+
if (memberExpression) {
|
|
78
|
+
const accessor = memberExpression.computed ? "[" : ".";
|
|
42
79
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
80
|
+
context.report({
|
|
81
|
+
node,
|
|
82
|
+
message: "Prefer using optional chaining rather than: '{{codeFragment}}'.",
|
|
83
|
+
data: {
|
|
84
|
+
codeFragment: `${guardText} && ${guardText}${accessor}`
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
},
|
|
46
89
|
|
|
47
|
-
|
|
90
|
+
Program() {
|
|
91
|
+
const text = sourceCode.getText();
|
|
92
|
+
let match;
|
|
48
93
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
94
|
+
// Reset lastIndex in case the RegExp object is reused
|
|
95
|
+
fallbackPattern.lastIndex = 0;
|
|
96
|
+
|
|
97
|
+
while ((match = fallbackPattern.exec(text)) !== null) {
|
|
98
|
+
if (
|
|
99
|
+
OBJECT_METHODS_PATTERN.test(match[1]) ||
|
|
100
|
+
ARRAY_METHODS_PATTERN.test(match[1])
|
|
101
|
+
) {
|
|
102
|
+
// Ignore `Object.keys(foo || {})` (and others) as it cannot be \
|
|
103
|
+
// safely transformed to optional chaining
|
|
104
|
+
} else {
|
|
105
|
+
context.report({
|
|
106
|
+
message: "Prefer using optional chaining rather than: '{{codeFragment}}'.",
|
|
107
|
+
data: {
|
|
108
|
+
codeFragment: match[0]
|
|
109
|
+
},
|
|
110
|
+
loc: sourceCode.getLocFromIndex(match.index)
|
|
111
|
+
});
|
|
65
112
|
}
|
|
66
|
-
}
|
|
113
|
+
}
|
|
67
114
|
}
|
|
68
115
|
};
|
|
69
116
|
}
|
|
@@ -7,6 +7,10 @@ ruleTester.run("enforce-optional", rule, {
|
|
|
7
7
|
valid: [
|
|
8
8
|
"const result = foo?.bar;",
|
|
9
9
|
"const result = foo && foo.bar !== null;",
|
|
10
|
+
"const result = foo && foo.bar <= maximum;",
|
|
11
|
+
"const result = foo && foo[bar] === value;",
|
|
12
|
+
"if (_header_value && _header_value.length <= ORIGINAL_HEADER_MAX_LENGTH) {}",
|
|
13
|
+
"if (_value instanceof Object && Object.keys(_value).length > 0) {}",
|
|
10
14
|
"const keys = Object.keys(foo || {});",
|
|
11
15
|
"const filtered = (foo || []).filter(Boolean);"
|
|
12
16
|
],
|