eslint-plugin-th-rules 2.5.1 → 2.6.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/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [2.6.0](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v2.5.1...v2.6.0) (2026-01-14)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* add prefer-explicit-nil-or-empty-check rule and update dependencies ([c8fb3da](https://github.com/tomerh2001/eslint-plugin-th-rules/commit/c8fb3da5be98caf0c31a50fa72f16b07e410eff6))
|
|
7
|
+
|
|
1
8
|
## [2.5.1](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v2.5.0...v2.5.1) (2026-01-14)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-th-rules",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.0",
|
|
4
4
|
"description": "A List of custom ESLint rules created by Tomer Horowitz",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|
|
@@ -51,6 +51,7 @@
|
|
|
51
51
|
"@types/lodash": "^4",
|
|
52
52
|
"@types/requireindex": "^1.2.4",
|
|
53
53
|
"@types/xo": "^0.39.9",
|
|
54
|
+
"@typescript-eslint/rule-tester": "^8.53.0",
|
|
54
55
|
"bun-types": "latest",
|
|
55
56
|
"eslint": "^9.39.2",
|
|
56
57
|
"eslint-doc-generator": "^3.0.2",
|
package/src/index.js
CHANGED
|
@@ -115,6 +115,7 @@ plugin.configs['recommended-typescript'] = flatConfigs(
|
|
|
115
115
|
},
|
|
116
116
|
},
|
|
117
117
|
rules: {
|
|
118
|
+
'th-rules/prefer-explicit-nil-or-empty-check': 'off',
|
|
118
119
|
'@typescript-eslint/consistent-type-definitions': 'off',
|
|
119
120
|
'@typescript-eslint/no-extraneous-class': 'off',
|
|
120
121
|
'@typescript-eslint/no-duplicate-type-constituents': 'off',
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const ts = require('typescript');
|
|
4
|
+
|
|
5
|
+
const meta = {
|
|
6
|
+
type: 'problem',
|
|
7
|
+
docs: {
|
|
8
|
+
description:
|
|
9
|
+
'Disallow implicit truthy/falsy checks on non-boolean values; require _.isNil or _.isEmpty',
|
|
10
|
+
category: 'Best Practices',
|
|
11
|
+
recommended: true,
|
|
12
|
+
url: 'https://github.com/tomerh2001/eslint-plugin-th-rules/blob/main/docs/rules/prefer-explicit-nil-or-empty-check.md',
|
|
13
|
+
},
|
|
14
|
+
hasSuggestions: true,
|
|
15
|
+
schema: [],
|
|
16
|
+
requiresTypeChecking: true,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function create(context) {
|
|
20
|
+
const sourceCode = context.getSourceCode();
|
|
21
|
+
const {parserServices} = context;
|
|
22
|
+
|
|
23
|
+
// Safety: rule is disabled if type info is unavailable
|
|
24
|
+
if (!parserServices?.program || !parserServices.esTreeNodeToTSNodeMap) {
|
|
25
|
+
return {};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const checker = parserServices.program.getTypeChecker();
|
|
29
|
+
|
|
30
|
+
function isImplicitTruthyTarget(node) {
|
|
31
|
+
return (
|
|
32
|
+
node.type === 'Identifier'
|
|
33
|
+
|| node.type === 'MemberExpression'
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function isBooleanType(node) {
|
|
38
|
+
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
|
|
39
|
+
if (!tsNode) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const type = checker.getTypeAtLocation(tsNode);
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
(type.flags & ts.TypeFlags.Boolean) !== 0
|
|
47
|
+
|| (type.flags & ts.TypeFlags.BooleanLiteral) !== 0
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function report(node, testedNode, negated) {
|
|
52
|
+
const text = sourceCode.getText(testedNode);
|
|
53
|
+
|
|
54
|
+
const isNilExpr = negated
|
|
55
|
+
? `!_.isNil(${text})`
|
|
56
|
+
: `_.isNil(${text})`;
|
|
57
|
+
|
|
58
|
+
const isEmptyExpr = negated
|
|
59
|
+
? `!_.isEmpty(${text})`
|
|
60
|
+
: `_.isEmpty(${text})`;
|
|
61
|
+
|
|
62
|
+
context.report({
|
|
63
|
+
node,
|
|
64
|
+
message:
|
|
65
|
+
'Avoid implicit truthy/falsy checks on non-boolean values; use _.isNil or _.isEmpty instead',
|
|
66
|
+
suggest: [
|
|
67
|
+
{
|
|
68
|
+
desc: `Replace with ${isNilExpr}`,
|
|
69
|
+
fix(fixer) {
|
|
70
|
+
return fixer.replaceText(node, isNilExpr);
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
desc: `Replace with ${isEmptyExpr}`,
|
|
75
|
+
fix(fixer) {
|
|
76
|
+
return fixer.replaceText(node, isEmptyExpr);
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
IfStatement(node) {
|
|
85
|
+
const {test} = node;
|
|
86
|
+
|
|
87
|
+
// If (!value)
|
|
88
|
+
if (test.type === 'UnaryExpression' && test.operator === '!') {
|
|
89
|
+
const arg = test.argument;
|
|
90
|
+
|
|
91
|
+
if (!isImplicitTruthyTarget(arg)) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (isBooleanType(arg)) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
report(test, arg, true);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// If (value)
|
|
104
|
+
if (isImplicitTruthyTarget(test)) {
|
|
105
|
+
if (isBooleanType(test)) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
report(test, test, false);
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
module.exports = {meta, create};
|