eslint-plugin-th-rules 2.6.0 → 2.7.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 +14 -0
- package/README.md +1 -1
- package/docs/rules/prefer-is-empty.md +4 -0
- package/package.json +1 -1
- package/src/index.js +0 -1
- package/tests/no-boolean-coercion.test.ts +6 -1
- package/tests/prefer-is-empty.test.ts +6 -1
- package/src/rules/prefer-explicit-nil-or-empty-check.js +0 -115
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
# [2.7.0](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v2.6.1...v2.7.0) (2026-01-14)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* remove prefer-explicit-nil-or-empty-check rule and associated tests ([2aded48](https://github.com/tomerh2001/eslint-plugin-th-rules/commit/2aded48d6f7df8d205cf7817afe3c0fdca39601c))
|
|
7
|
+
|
|
8
|
+
## [2.6.1](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v2.6.0...v2.6.1) (2026-01-14)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* 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))
|
|
14
|
+
|
|
1
15
|
# [2.6.0](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v2.5.1...v2.6.0) (2026-01-14)
|
|
2
16
|
|
|
3
17
|
|
package/README.md
CHANGED
|
@@ -23,7 +23,7 @@ This repository contains custom ESLint rules to enhance code quality and consist
|
|
|
23
23
|
| [no-comments](docs/rules/no-comments.md) | Disallow comments except for specified allowed patterns. | ✅ ⚛️ 🟦 | 🔧 | |
|
|
24
24
|
| [no-default-export](docs/rules/no-default-export.md) | Convert unnamed default exports to named default exports based on the file name. | ✅ ⚛️ 🟦 | 🔧 | |
|
|
25
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 |
|
|
26
|
+
| [prefer-is-empty](docs/rules/prefer-is-empty.md) | Require _.isEmpty instead of length comparisons | ✅ ⚛️ 🟦 | | 💡 |
|
|
27
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
28
|
| [top-level-functions](docs/rules/top-level-functions.md) | Require all top-level functions to be named/regular functions. | ✅ ⚛️ 🟦 | 🔧 | |
|
|
29
29
|
| [types-in-dts](docs/rules/types-in-dts.md) | Require TypeScript type declarations (type/interface/enum) to be placed in .d.ts files | ✅ ⚛️ 🟦 | | |
|
|
@@ -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
|
@@ -115,7 +115,6 @@ plugin.configs['recommended-typescript'] = flatConfigs(
|
|
|
115
115
|
},
|
|
116
116
|
},
|
|
117
117
|
rules: {
|
|
118
|
-
'th-rules/prefer-explicit-nil-or-empty-check': 'off',
|
|
119
118
|
'@typescript-eslint/consistent-type-definitions': 'off',
|
|
120
119
|
'@typescript-eslint/no-extraneous-class': 'off',
|
|
121
120
|
'@typescript-eslint/no-duplicate-type-constituents': 'off',
|
|
@@ -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: [
|
|
@@ -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: [
|
|
@@ -1,115 +0,0 @@
|
|
|
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};
|