eslint-plugin-th-rules 2.5.1 → 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 CHANGED
@@ -1,3 +1,17 @@
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
+
8
+ # [2.6.0](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v2.5.1...v2.6.0) (2026-01-14)
9
+
10
+
11
+ ### Features
12
+
13
+ * add prefer-explicit-nil-or-empty-check rule and update dependencies ([c8fb3da](https://github.com/tomerh2001/eslint-plugin-th-rules/commit/c8fb3da5be98caf0c31a50fa72f16b07e410eff6))
14
+
1
15
  ## [2.5.1](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v2.5.0...v2.5.1) (2026-01-14)
2
16
 
3
17
 
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                    | Description | 💼 | 🔧 | 💡 |
21
- | :--------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------------- | :------ | :- | :- |
22
- | [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. | ✅ ⚛️ 🟦 | | 💡 |
23
- | [no-comments](docs/rules/no-comments.md) | Disallow comments except for specified allowed patterns. | ✅ ⚛️ 🟦 | 🔧 | |
24
- | [no-default-export](docs/rules/no-default-export.md) | Convert unnamed default exports to named default exports based on the file name. | ✅ ⚛️ 🟦 | 🔧 | |
25
- | [no-destructuring](docs/rules/no-destructuring.md) | Disallow destructuring that does not meet certain conditions | ✅ ⚛️ 🟦 | | |
26
- | [prefer-is-empty](docs/rules/prefer-is-empty.md) | Require _.isEmpty instead of length comparisons | | | |
27
- | [schemas-in-schemas-file](docs/rules/schemas-in-schemas-file.md) | Require Zod schema declarations to be placed in a .schemas.ts file | ✅ ⚛️ 🟦 | | |
28
- | [top-level-functions](docs/rules/top-level-functions.md) | Require all top-level functions to be named/regular functions. | ✅ ⚛️ 🟦 | 🔧 | |
29
- | [types-in-dts](docs/rules/types-in-dts.md) | Require TypeScript type declarations (type/interface/enum) to be placed in .d.ts files | ✅ ⚛️ 🟦 | | |
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-th-rules",
3
- "version": "2.5.1",
3
+ "version": "2.6.1",
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
@@ -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',
@@ -0,0 +1,102 @@
1
+ const meta = {
2
+ type: 'problem',
3
+ docs: {
4
+ description:
5
+ 'Disallow implicit truthy/falsy checks on non-boolean values; require _.isNil or _.isEmpty',
6
+ category: 'Best Practices',
7
+ recommended: true,
8
+ url: 'https://github.com/tomerh2001/eslint-plugin-th-rules/blob/main/docs/rules/prefer-explicit-nil-or-empty-check.md',
9
+ },
10
+ hasSuggestions: true,
11
+ schema: [],
12
+ };
13
+
14
+ function create(context) {
15
+ const sourceCode = context.getSourceCode();
16
+
17
+ function isBooleanLiteral(node) {
18
+ return node.type === 'Literal' && typeof node.value === 'boolean';
19
+ }
20
+
21
+ function isUnaryNot(node) {
22
+ return node.type === 'UnaryExpression' && node.operator === '!';
23
+ }
24
+
25
+ function isImplicitTruthyTarget(node) {
26
+ return (
27
+ node.type === 'Identifier'
28
+ || node.type === 'MemberExpression'
29
+ );
30
+ }
31
+
32
+ function report(node, testedNode, negated) {
33
+ const text = sourceCode.getText(testedNode);
34
+
35
+ const isNilExpr = negated
36
+ ? `!_.isNil(${text})`
37
+ : `_.isNil(${text})`;
38
+
39
+ const isEmptyExpr = negated
40
+ ? `!_.isEmpty(${text})`
41
+ : `_.isEmpty(${text})`;
42
+
43
+ context.report({
44
+ node,
45
+ message:
46
+ 'Implicit truthy/falsy check on a non-boolean value is not allowed; use explicit _.isNil or _.isEmpty checks',
47
+ suggest: [
48
+ {
49
+ desc: `Replace with ${isNilExpr}`,
50
+ fix: fixer => fixer.replaceText(node, isNilExpr),
51
+ },
52
+ {
53
+ desc: `Replace with ${isEmptyExpr}`,
54
+ fix: fixer => fixer.replaceText(node, isEmptyExpr),
55
+ },
56
+ ],
57
+ });
58
+ }
59
+
60
+ function checkTestExpression(node) {
61
+ // If (dataObject)
62
+ if (isImplicitTruthyTarget(node)) {
63
+ report(node, node, true);
64
+ return;
65
+ }
66
+
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
+ }
76
+
77
+ return {
78
+ IfStatement(node) {
79
+ checkTestExpression(node.test);
80
+ },
81
+
82
+ WhileStatement(node) {
83
+ checkTestExpression(node.test);
84
+ },
85
+
86
+ DoWhileStatement(node) {
87
+ checkTestExpression(node.test);
88
+ },
89
+
90
+ ConditionalExpression(node) {
91
+ checkTestExpression(node.test);
92
+ },
93
+
94
+ LogicalExpression(node) {
95
+ if (node.operator === '&&' || node.operator === '||') {
96
+ checkTestExpression(node.left);
97
+ }
98
+ },
99
+ };
100
+ }
101
+
102
+ module.exports = {meta, create};
@@ -1,7 +1,12 @@
1
- const {RuleTester} = require('eslint');
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
- const {RuleTester} = require('eslint');
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: [