eslint-plugin-th-rules 2.4.0 β 2.5.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 +14 -0
- package/README.md +10 -9
- package/docs/rules/no-boolean-coercion.md +1 -1
- package/docs/rules/prefer-is-empty.md +5 -0
- package/package.json +1 -1
- package/src/index.js +1 -0
- package/src/rules/prefer-is-empty.js +104 -0
- package/tests/prefer-is-empty.test.ts +143 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [2.5.1](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v2.5.0...v2.5.1) (2026-01-14)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* add prefer-is-empty rule to enforce _.isEmpty over length comparisons and update documentation ([06ee673](https://github.com/tomerh2001/eslint-plugin-th-rules/commit/06ee673a0fde79b7cc2af1f72807d9716576310c))
|
|
7
|
+
|
|
8
|
+
# [2.5.0](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v2.4.0...v2.5.0) (2026-01-14)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* add prefer-is-empty rule to enforce _.isEmpty over length comparisons and update documentation ([5bc671f](https://github.com/tomerh2001/eslint-plugin-th-rules/commit/5bc671f35599d69a5a1432d173df408082da2db6))
|
|
14
|
+
|
|
1
15
|
# [2.4.0](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v2.3.0...v2.4.0) (2026-01-14)
|
|
2
16
|
|
|
3
17
|
|
package/README.md
CHANGED
|
@@ -17,15 +17,16 @@ This repository contains custom ESLint rules to enhance code quality and consist
|
|
|
17
17
|
π§ Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
|
|
18
18
|
π‘ Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
|
|
19
19
|
|
|
20
|
-
| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | Description
|
|
21
|
-
| :--------------------------------------------------------------- |
|
|
22
|
-
| [no-boolean-coercion](docs/rules/no-boolean-coercion.md) | Disallow Boolean(
|
|
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
|
-
| [
|
|
27
|
-
| [
|
|
28
|
-
| [
|
|
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 | β
βοΈ π¦ | | |
|
|
29
30
|
|
|
30
31
|
<!-- end auto-generated rules list -->
|
|
31
32
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# th-rules/no-boolean-coercion
|
|
2
2
|
|
|
3
|
-
π Disallow Boolean(
|
|
3
|
+
π Disallow Boolean(value) or !!value. Enforce _.isNil(value) for scalar values and _.isEmpty(value) for strings, arrays, and objects.
|
|
4
4
|
|
|
5
5
|
πΌ This rule is enabled in the following configs: β
`recommended`, βοΈ `recommended-react`, π¦ `recommended-typescript`.
|
|
6
6
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -66,6 +66,7 @@ const baseRecommended = {
|
|
|
66
66
|
'th-rules/schemas-in-schemas-file': 'error',
|
|
67
67
|
'th-rules/types-in-dts': 'error',
|
|
68
68
|
'th-rules/no-boolean-coercion': 'error',
|
|
69
|
+
'th-rules/prefer-is-empty': 'error',
|
|
69
70
|
'unicorn/filename-case': 'off',
|
|
70
71
|
'unicorn/no-array-callback-reference': 'off',
|
|
71
72
|
'import/extensions': 'off',
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
const meta = {
|
|
2
|
+
type: 'problem',
|
|
3
|
+
docs: {
|
|
4
|
+
description: 'Require _.isEmpty instead of length comparisons',
|
|
5
|
+
category: 'Best Practices',
|
|
6
|
+
recommended: true,
|
|
7
|
+
url: 'https://github.com/tomerh2001/eslint-plugin-th-rules/blob/main/docs/rules/prefer-is-empty.md',
|
|
8
|
+
},
|
|
9
|
+
hasSuggestions: true,
|
|
10
|
+
schema: [],
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
function create(context) {
|
|
14
|
+
const sourceCode = context.getSourceCode();
|
|
15
|
+
|
|
16
|
+
function isLengthAccess(node) {
|
|
17
|
+
return (
|
|
18
|
+
node
|
|
19
|
+
&& node.type === 'MemberExpression'
|
|
20
|
+
&& !node.computed
|
|
21
|
+
&& node.property.type === 'Identifier'
|
|
22
|
+
&& node.property.name === 'length'
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function isNumericLiteral(node) {
|
|
27
|
+
return node && node.type === 'Literal' && typeof node.value === 'number';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function report(node, collectionNode, operator, value, isEmptyCheck) {
|
|
31
|
+
const collectionText = sourceCode.getText(collectionNode.object);
|
|
32
|
+
const replacement = isEmptyCheck
|
|
33
|
+
? `_.isEmpty(${collectionText})`
|
|
34
|
+
: `!_.isEmpty(${collectionText})`;
|
|
35
|
+
|
|
36
|
+
context.report({
|
|
37
|
+
node,
|
|
38
|
+
message: `Use _.isEmpty(${collectionText}) instead of checking ${collectionText}.length ${operator} ${value}`,
|
|
39
|
+
suggest: [
|
|
40
|
+
{
|
|
41
|
+
desc: `Replace with ${replacement}`,
|
|
42
|
+
fix(fixer) {
|
|
43
|
+
return fixer.replaceText(node, replacement);
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
BinaryExpression(node) {
|
|
52
|
+
const {left, right, operator} = node;
|
|
53
|
+
|
|
54
|
+
// Values.length <op> N
|
|
55
|
+
if (isLengthAccess(left) && isNumericLiteral(right)) {
|
|
56
|
+
const {value} = right;
|
|
57
|
+
|
|
58
|
+
// EMPTY checks
|
|
59
|
+
if (
|
|
60
|
+
(operator === '===' && value === 0)
|
|
61
|
+
|| (operator === '<=' && value === 0)
|
|
62
|
+
|| (operator === '<' && value === 1)
|
|
63
|
+
) {
|
|
64
|
+
report(node, left, operator, value, true);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// NOT EMPTY checks
|
|
69
|
+
if (
|
|
70
|
+
(operator === '>' && value === 0)
|
|
71
|
+
|| (operator === '>=' && value === 1)
|
|
72
|
+
|| ((operator === '!=' || operator === '!==') && value === 0)
|
|
73
|
+
) {
|
|
74
|
+
report(node, left, operator, value, false);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// N <op> values.length (reversed)
|
|
79
|
+
if (isNumericLiteral(left) && isLengthAccess(right)) {
|
|
80
|
+
const {value} = left;
|
|
81
|
+
|
|
82
|
+
// EMPTY checks
|
|
83
|
+
if (
|
|
84
|
+
(operator === '===' && value === 0)
|
|
85
|
+
|| (operator === '>=' && value === 0)
|
|
86
|
+
|| (operator === '>' && value === 0)
|
|
87
|
+
) {
|
|
88
|
+
report(node, right, operator, value, true);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// NOT EMPTY checks
|
|
93
|
+
if (
|
|
94
|
+
(operator === '<' && value === 1)
|
|
95
|
+
|| (operator === '<=' && value === 0)
|
|
96
|
+
) {
|
|
97
|
+
report(node, right, operator, value, false);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
module.exports = {meta, create};
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
const {RuleTester} = require('eslint');
|
|
2
|
+
const rule = require('../src/rules/prefer-is-empty');
|
|
3
|
+
|
|
4
|
+
const ruleTester = new RuleTester({});
|
|
5
|
+
|
|
6
|
+
ruleTester.run('prefer-is-empty', rule, {
|
|
7
|
+
valid: [
|
|
8
|
+
'_.isEmpty(values);',
|
|
9
|
+
'!_.isEmpty(values);',
|
|
10
|
+
'Array.isArray(values);',
|
|
11
|
+
'values.size > 0;', // Non-length property
|
|
12
|
+
],
|
|
13
|
+
|
|
14
|
+
invalid: [
|
|
15
|
+
{
|
|
16
|
+
code: 'values.length === 0;',
|
|
17
|
+
errors: [
|
|
18
|
+
{
|
|
19
|
+
message:
|
|
20
|
+
'Use _.isEmpty(values) instead of checking values.length === 0',
|
|
21
|
+
suggestions: [
|
|
22
|
+
{
|
|
23
|
+
desc: 'Replace with _.isEmpty(values)',
|
|
24
|
+
output: '_.isEmpty(values);',
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
{
|
|
32
|
+
code: 'values.length < 1;',
|
|
33
|
+
errors: [
|
|
34
|
+
{
|
|
35
|
+
message:
|
|
36
|
+
'Use _.isEmpty(values) instead of checking values.length < 1',
|
|
37
|
+
suggestions: [
|
|
38
|
+
{
|
|
39
|
+
desc: 'Replace with _.isEmpty(values)',
|
|
40
|
+
output: '_.isEmpty(values);',
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
{
|
|
48
|
+
code: 'values.length <= 0;',
|
|
49
|
+
errors: [
|
|
50
|
+
{
|
|
51
|
+
message:
|
|
52
|
+
'Use _.isEmpty(values) instead of checking values.length <= 0',
|
|
53
|
+
suggestions: [
|
|
54
|
+
{
|
|
55
|
+
desc: 'Replace with _.isEmpty(values)',
|
|
56
|
+
output: '_.isEmpty(values);',
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
{
|
|
64
|
+
code: 'values.length > 0;',
|
|
65
|
+
errors: [
|
|
66
|
+
{
|
|
67
|
+
message:
|
|
68
|
+
'Use _.isEmpty(values) instead of checking values.length > 0',
|
|
69
|
+
suggestions: [
|
|
70
|
+
{
|
|
71
|
+
desc: 'Replace with !_.isEmpty(values)',
|
|
72
|
+
output: '!_.isEmpty(values);',
|
|
73
|
+
},
|
|
74
|
+
],
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
{
|
|
80
|
+
code: 'values.length >= 1;',
|
|
81
|
+
errors: [
|
|
82
|
+
{
|
|
83
|
+
message:
|
|
84
|
+
'Use _.isEmpty(values) instead of checking values.length >= 1',
|
|
85
|
+
suggestions: [
|
|
86
|
+
{
|
|
87
|
+
desc: 'Replace with !_.isEmpty(values)',
|
|
88
|
+
output: '!_.isEmpty(values);',
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
},
|
|
92
|
+
],
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
{
|
|
96
|
+
code: 'values.length !== 0;',
|
|
97
|
+
errors: [
|
|
98
|
+
{
|
|
99
|
+
message:
|
|
100
|
+
'Use _.isEmpty(values) instead of checking values.length !== 0',
|
|
101
|
+
suggestions: [
|
|
102
|
+
{
|
|
103
|
+
desc: 'Replace with !_.isEmpty(values)',
|
|
104
|
+
output: '!_.isEmpty(values);',
|
|
105
|
+
},
|
|
106
|
+
],
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
},
|
|
110
|
+
|
|
111
|
+
{
|
|
112
|
+
code: 'if (items.length > 0) {}',
|
|
113
|
+
errors: [
|
|
114
|
+
{
|
|
115
|
+
message:
|
|
116
|
+
'Use _.isEmpty(items) instead of checking items.length > 0',
|
|
117
|
+
suggestions: [
|
|
118
|
+
{
|
|
119
|
+
desc: 'Replace with !_.isEmpty(items)',
|
|
120
|
+
output: 'if (!_.isEmpty(items)) {}',
|
|
121
|
+
},
|
|
122
|
+
],
|
|
123
|
+
},
|
|
124
|
+
],
|
|
125
|
+
},
|
|
126
|
+
|
|
127
|
+
{
|
|
128
|
+
code: '0 === items.length;',
|
|
129
|
+
errors: [
|
|
130
|
+
{
|
|
131
|
+
message:
|
|
132
|
+
'Use _.isEmpty(items) instead of checking items.length === 0',
|
|
133
|
+
suggestions: [
|
|
134
|
+
{
|
|
135
|
+
desc: 'Replace with _.isEmpty(items)',
|
|
136
|
+
output: '_.isEmpty(items);',
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
},
|
|
142
|
+
],
|
|
143
|
+
});
|