eslint-plugin-th-rules 2.6.0 → 2.6.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/CHANGELOG.md +7 -0
- package/README.md +12 -10
- package/docs/rules/prefer-explicit-nil-or-empty-check.md +9 -0
- package/docs/rules/prefer-is-empty.md +4 -0
- package/package.json +1 -1
- package/src/index.js +1 -1
- package/src/rules/prefer-explicit-nil-or-empty-check.js +39 -52
- package/tests/no-boolean-coercion.test.ts +6 -1
- package/tests/prefer-explicit-nil-or-empty-check.test.ts +113 -0
- package/tests/prefer-is-empty.test.ts +6 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
## [2.6.1](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v2.6.0...v2.6.1) (2026-01-14)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* refactor prefer-explicit-nil-or-empty-check rule and add tests for implicit checks ([d153066](https://github.com/tomerh2001/eslint-plugin-th-rules/commit/d1530664708cf31025d5ebf1b92f6b68401c5448))
|
|
7
|
+
|
|
1
8
|
# [2.6.0](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v2.5.1...v2.6.0) (2026-01-14)
|
|
2
9
|
|
|
3
10
|
|
package/README.md
CHANGED
|
@@ -11,22 +11,24 @@ This repository contains custom ESLint rules to enhance code quality and consist
|
|
|
11
11
|
<!-- begin auto-generated rules list -->
|
|
12
12
|
|
|
13
13
|
💼 Configurations enabled in.\
|
|
14
|
+
🚫 Configurations disabled in.\
|
|
14
15
|
✅ Set in the `recommended` configuration.\
|
|
15
16
|
⚛️ Set in the `recommended-react` configuration.\
|
|
16
17
|
🟦 Set in the `recommended-typescript` configuration.\
|
|
17
18
|
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
|
|
18
19
|
💡 Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
|
|
19
20
|
|
|
20
|
-
| Name
|
|
21
|
-
|
|
|
22
|
-
| [no-boolean-coercion](docs/rules/no-boolean-coercion.md)
|
|
23
|
-
| [no-comments](docs/rules/no-comments.md)
|
|
24
|
-
| [no-default-export](docs/rules/no-default-export.md)
|
|
25
|
-
| [no-destructuring](docs/rules/no-destructuring.md)
|
|
26
|
-
| [prefer-
|
|
27
|
-
| [
|
|
28
|
-
| [
|
|
29
|
-
| [
|
|
21
|
+
| Name | Description | 💼 | 🚫 | 🔧 | 💡 |
|
|
22
|
+
| :------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------------- | :------ | :- | :- | :- |
|
|
23
|
+
| [no-boolean-coercion](docs/rules/no-boolean-coercion.md) | Disallow Boolean(value) or !!value. Enforce _.isNil(value) for scalar values and _.isEmpty(value) for strings, arrays, and objects. | ✅ ⚛️ 🟦 | | | 💡 |
|
|
24
|
+
| [no-comments](docs/rules/no-comments.md) | Disallow comments except for specified allowed patterns. | ✅ ⚛️ 🟦 | | 🔧 | |
|
|
25
|
+
| [no-default-export](docs/rules/no-default-export.md) | Convert unnamed default exports to named default exports based on the file name. | ✅ ⚛️ 🟦 | | 🔧 | |
|
|
26
|
+
| [no-destructuring](docs/rules/no-destructuring.md) | Disallow destructuring that does not meet certain conditions | ✅ ⚛️ 🟦 | | | |
|
|
27
|
+
| [prefer-explicit-nil-or-empty-check](docs/rules/prefer-explicit-nil-or-empty-check.md) | Disallow implicit truthy/falsy checks on non-boolean values; require _.isNil or _.isEmpty | | 🟦 | | 💡 |
|
|
28
|
+
| [prefer-is-empty](docs/rules/prefer-is-empty.md) | Require _.isEmpty instead of length comparisons | ✅ ⚛️ 🟦 | | | 💡 |
|
|
29
|
+
| [schemas-in-schemas-file](docs/rules/schemas-in-schemas-file.md) | Require Zod schema declarations to be placed in a .schemas.ts file | ✅ ⚛️ 🟦 | | | |
|
|
30
|
+
| [top-level-functions](docs/rules/top-level-functions.md) | Require all top-level functions to be named/regular functions. | ✅ ⚛️ 🟦 | | 🔧 | |
|
|
31
|
+
| [types-in-dts](docs/rules/types-in-dts.md) | Require TypeScript type declarations (type/interface/enum) to be placed in .d.ts files | ✅ ⚛️ 🟦 | | | |
|
|
30
32
|
|
|
31
33
|
<!-- end auto-generated rules list -->
|
|
32
34
|
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# th-rules/prefer-explicit-nil-or-empty-check
|
|
2
|
+
|
|
3
|
+
📝 Disallow implicit truthy/falsy checks on non-boolean values; require _.isNil or _.isEmpty.
|
|
4
|
+
|
|
5
|
+
🚫 This rule is _disabled_ in the 🟦 `recommended-typescript` config.
|
|
6
|
+
|
|
7
|
+
💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
|
|
8
|
+
|
|
9
|
+
<!-- end auto-generated rule header -->
|
|
@@ -2,4 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
📝 Require _.isEmpty instead of length comparisons.
|
|
4
4
|
|
|
5
|
+
💼 This rule is enabled in the following configs: ✅ `recommended`, ⚛️ `recommended-react`, 🟦 `recommended-typescript`.
|
|
6
|
+
|
|
7
|
+
💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
|
|
8
|
+
|
|
5
9
|
<!-- end auto-generated rule header -->
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -67,6 +67,7 @@ const baseRecommended = {
|
|
|
67
67
|
'th-rules/types-in-dts': 'error',
|
|
68
68
|
'th-rules/no-boolean-coercion': 'error',
|
|
69
69
|
'th-rules/prefer-is-empty': 'error',
|
|
70
|
+
'th-rules/prefer-explicit-nil-or-empty-check': 'off',
|
|
70
71
|
'unicorn/filename-case': 'off',
|
|
71
72
|
'unicorn/no-array-callback-reference': 'off',
|
|
72
73
|
'import/extensions': 'off',
|
|
@@ -115,7 +116,6 @@ plugin.configs['recommended-typescript'] = flatConfigs(
|
|
|
115
116
|
},
|
|
116
117
|
},
|
|
117
118
|
rules: {
|
|
118
|
-
'th-rules/prefer-explicit-nil-or-empty-check': 'off',
|
|
119
119
|
'@typescript-eslint/consistent-type-definitions': 'off',
|
|
120
120
|
'@typescript-eslint/no-extraneous-class': 'off',
|
|
121
121
|
'@typescript-eslint/no-duplicate-type-constituents': 'off',
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const ts = require('typescript');
|
|
4
|
-
|
|
5
1
|
const meta = {
|
|
6
2
|
type: 'problem',
|
|
7
3
|
docs: {
|
|
@@ -13,19 +9,18 @@ const meta = {
|
|
|
13
9
|
},
|
|
14
10
|
hasSuggestions: true,
|
|
15
11
|
schema: [],
|
|
16
|
-
requiresTypeChecking: true,
|
|
17
12
|
};
|
|
18
13
|
|
|
19
14
|
function create(context) {
|
|
20
15
|
const sourceCode = context.getSourceCode();
|
|
21
|
-
const {parserServices} = context;
|
|
22
16
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return {};
|
|
17
|
+
function isBooleanLiteral(node) {
|
|
18
|
+
return node.type === 'Literal' && typeof node.value === 'boolean';
|
|
26
19
|
}
|
|
27
20
|
|
|
28
|
-
|
|
21
|
+
function isUnaryNot(node) {
|
|
22
|
+
return node.type === 'UnaryExpression' && node.operator === '!';
|
|
23
|
+
}
|
|
29
24
|
|
|
30
25
|
function isImplicitTruthyTarget(node) {
|
|
31
26
|
return (
|
|
@@ -34,20 +29,6 @@ function create(context) {
|
|
|
34
29
|
);
|
|
35
30
|
}
|
|
36
31
|
|
|
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
32
|
function report(node, testedNode, negated) {
|
|
52
33
|
const text = sourceCode.getText(testedNode);
|
|
53
34
|
|
|
@@ -62,51 +43,57 @@ function create(context) {
|
|
|
62
43
|
context.report({
|
|
63
44
|
node,
|
|
64
45
|
message:
|
|
65
|
-
'
|
|
46
|
+
'Implicit truthy/falsy check on a non-boolean value is not allowed; use explicit _.isNil or _.isEmpty checks',
|
|
66
47
|
suggest: [
|
|
67
48
|
{
|
|
68
49
|
desc: `Replace with ${isNilExpr}`,
|
|
69
|
-
fix(
|
|
70
|
-
return fixer.replaceText(node, isNilExpr);
|
|
71
|
-
},
|
|
50
|
+
fix: fixer => fixer.replaceText(node, isNilExpr),
|
|
72
51
|
},
|
|
73
52
|
{
|
|
74
53
|
desc: `Replace with ${isEmptyExpr}`,
|
|
75
|
-
fix(
|
|
76
|
-
return fixer.replaceText(node, isEmptyExpr);
|
|
77
|
-
},
|
|
54
|
+
fix: fixer => fixer.replaceText(node, isEmptyExpr),
|
|
78
55
|
},
|
|
79
56
|
],
|
|
80
57
|
});
|
|
81
58
|
}
|
|
82
59
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
60
|
+
function checkTestExpression(node) {
|
|
61
|
+
// If (dataObject)
|
|
62
|
+
if (isImplicitTruthyTarget(node)) {
|
|
63
|
+
report(node, node, true);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
86
66
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
67
|
+
// If (!dataObject)
|
|
68
|
+
if (
|
|
69
|
+
isUnaryNot(node)
|
|
70
|
+
&& isImplicitTruthyTarget(node.argument)
|
|
71
|
+
&& !isBooleanLiteral(node.argument)
|
|
72
|
+
) {
|
|
73
|
+
report(node, node.argument, false);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
90
76
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
77
|
+
return {
|
|
78
|
+
IfStatement(node) {
|
|
79
|
+
checkTestExpression(node.test);
|
|
80
|
+
},
|
|
94
81
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
82
|
+
WhileStatement(node) {
|
|
83
|
+
checkTestExpression(node.test);
|
|
84
|
+
},
|
|
98
85
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
86
|
+
DoWhileStatement(node) {
|
|
87
|
+
checkTestExpression(node.test);
|
|
88
|
+
},
|
|
102
89
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
90
|
+
ConditionalExpression(node) {
|
|
91
|
+
checkTestExpression(node.test);
|
|
92
|
+
},
|
|
108
93
|
|
|
109
|
-
|
|
94
|
+
LogicalExpression(node) {
|
|
95
|
+
if (node.operator === '&&' || node.operator === '||') {
|
|
96
|
+
checkTestExpression(node.left);
|
|
110
97
|
}
|
|
111
98
|
},
|
|
112
99
|
};
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
const {RuleTester} = require('@typescript-eslint/rule-tester');
|
|
2
2
|
const rule = require('../src/rules/no-boolean-coercion');
|
|
3
3
|
|
|
4
|
-
const ruleTester = new RuleTester({
|
|
4
|
+
const ruleTester = new RuleTester({
|
|
5
|
+
languageOptions: {
|
|
6
|
+
ecmaVersion: 2020,
|
|
7
|
+
sourceType: 'module',
|
|
8
|
+
},
|
|
9
|
+
});
|
|
5
10
|
|
|
6
11
|
ruleTester.run('no-boolean-coercion', rule, {
|
|
7
12
|
valid: [
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
const {RuleTester} = require('@typescript-eslint/rule-tester');
|
|
2
|
+
const rule = require('../src/rules/prefer-explicit-nil-or-empty-check');
|
|
3
|
+
|
|
4
|
+
const ruleTester = new RuleTester({
|
|
5
|
+
languageOptions: {
|
|
6
|
+
ecmaVersion: 2020,
|
|
7
|
+
sourceType: 'module',
|
|
8
|
+
},
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
ruleTester.run('prefer-explicit-nil-or-empty-check', rule, {
|
|
12
|
+
valid: [
|
|
13
|
+
// Explicit checks – allowed
|
|
14
|
+
'if (_.isNil(value)) {}',
|
|
15
|
+
'if (!_.isNil(value)) {}',
|
|
16
|
+
'if (_.isEmpty(list)) {}',
|
|
17
|
+
'if (!_.isEmpty(list)) {}',
|
|
18
|
+
|
|
19
|
+
// Explicit comparisons – allowed
|
|
20
|
+
'if (value != null) {}',
|
|
21
|
+
'if (list.length > 0) {}',
|
|
22
|
+
|
|
23
|
+
// Boolean values – allowed
|
|
24
|
+
'if (true) {}',
|
|
25
|
+
'if (false) {}',
|
|
26
|
+
|
|
27
|
+
// Non-control-flow usage – allowed
|
|
28
|
+
'value;',
|
|
29
|
+
'doSomething(value);',
|
|
30
|
+
],
|
|
31
|
+
|
|
32
|
+
invalid: [
|
|
33
|
+
{
|
|
34
|
+
code: 'if (dataObject) {}',
|
|
35
|
+
errors: [
|
|
36
|
+
{
|
|
37
|
+
message:
|
|
38
|
+
'Implicit truthy/falsy check on a non-boolean value is not allowed; use explicit _.isNil or _.isEmpty checks',
|
|
39
|
+
suggestions: [
|
|
40
|
+
{
|
|
41
|
+
desc: 'Replace with !_.isNil(dataObject)',
|
|
42
|
+
output: 'if (!_.isNil(dataObject)) {}',
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
desc: 'Replace with !_.isEmpty(dataObject)',
|
|
46
|
+
output: 'if (!_.isEmpty(dataObject)) {}',
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
{
|
|
54
|
+
code: 'if (!dataObject) {}',
|
|
55
|
+
errors: [
|
|
56
|
+
{
|
|
57
|
+
message:
|
|
58
|
+
'Implicit truthy/falsy check on a non-boolean value is not allowed; use explicit _.isNil or _.isEmpty checks',
|
|
59
|
+
suggestions: [
|
|
60
|
+
{
|
|
61
|
+
desc: 'Replace with _.isNil(dataObject)',
|
|
62
|
+
output: 'if (_.isNil(dataObject)) {}',
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
desc: 'Replace with _.isEmpty(dataObject)',
|
|
66
|
+
output: 'if (_.isEmpty(dataObject)) {}',
|
|
67
|
+
},
|
|
68
|
+
],
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
{
|
|
74
|
+
code: 'while (items) {}',
|
|
75
|
+
errors: [
|
|
76
|
+
{
|
|
77
|
+
message:
|
|
78
|
+
'Implicit truthy/falsy check on a non-boolean value is not allowed; use explicit _.isNil or _.isEmpty checks',
|
|
79
|
+
suggestions: [
|
|
80
|
+
{
|
|
81
|
+
desc: 'Replace with !_.isNil(items)',
|
|
82
|
+
output: 'while (!_.isNil(items)) {}',
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
desc: 'Replace with !_.isEmpty(items)',
|
|
86
|
+
output: 'while (!_.isEmpty(items)) {}',
|
|
87
|
+
},
|
|
88
|
+
],
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
},
|
|
92
|
+
|
|
93
|
+
{
|
|
94
|
+
code: 'data && doSomething();',
|
|
95
|
+
errors: [
|
|
96
|
+
{
|
|
97
|
+
message:
|
|
98
|
+
'Implicit truthy/falsy check on a non-boolean value is not allowed; use explicit _.isNil or _.isEmpty checks',
|
|
99
|
+
suggestions: [
|
|
100
|
+
{
|
|
101
|
+
desc: 'Replace with !_.isNil(data)',
|
|
102
|
+
output: '!_.isNil(data) && doSomething();',
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
desc: 'Replace with !_.isEmpty(data)',
|
|
106
|
+
output: '!_.isEmpty(data) && doSomething();',
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
},
|
|
110
|
+
],
|
|
111
|
+
},
|
|
112
|
+
],
|
|
113
|
+
});
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
const {RuleTester} = require('@typescript-eslint/rule-tester');
|
|
2
2
|
const rule = require('../src/rules/prefer-is-empty');
|
|
3
3
|
|
|
4
|
-
const ruleTester = new RuleTester({
|
|
4
|
+
const ruleTester = new RuleTester({
|
|
5
|
+
languageOptions: {
|
|
6
|
+
ecmaVersion: 2020,
|
|
7
|
+
sourceType: 'module',
|
|
8
|
+
},
|
|
9
|
+
});
|
|
5
10
|
|
|
6
11
|
ruleTester.run('prefer-is-empty', rule, {
|
|
7
12
|
valid: [
|