eslint-plugin-th-rules 2.1.1 → 2.4.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/.github/workflows/main.yml +3 -0
- package/CHANGELOG.md +32 -0
- package/README.md +11 -9
- package/docs/rules/no-boolean-coercion.md +9 -0
- package/package.json +2 -2
- package/src/index.js +1 -0
- package/src/rules/no-boolean-coercion.js +125 -0
- package/tests/no-boolean-coercion.test.ts +78 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,35 @@
|
|
|
1
|
+
# [2.4.0](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v2.3.0...v2.4.0) (2026-01-14)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* fixed git tags ([822f503](https://github.com/tomerh2001/eslint-plugin-th-rules/commit/822f5033769f89fabbd6cf7177ccef74311ec2e5))
|
|
7
|
+
|
|
8
|
+
# [2.2.0](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v2.1.1...v2.2.0) (2026-01-14)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* enhance no-boolean-coercion rule with detailed messages and update tests ([c79ad92](https://github.com/tomerh2001/eslint-plugin-th-rules/commit/c79ad921e2a6c4dabe0f0e74b80393b01525c3d7))
|
|
14
|
+
* enhance no-boolean-coercion rule with detailed messages and update tests ([686d620](https://github.com/tomerh2001/eslint-plugin-th-rules/commit/686d6204bd46f757a4490656521b1094f3e3237e))
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Features
|
|
18
|
+
|
|
19
|
+
* created new rule no-boolean-coercion ([f9b0803](https://github.com/tomerh2001/eslint-plugin-th-rules/commit/f9b08039f6275e363da10e8e3ca49fb75ad2e48a))
|
|
20
|
+
|
|
21
|
+
# [2.2.0](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v2.1.1...v2.2.0) (2026-01-14)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
### Bug Fixes
|
|
25
|
+
|
|
26
|
+
* enhance no-boolean-coercion rule with detailed messages and update tests ([686d620](https://github.com/tomerh2001/eslint-plugin-th-rules/commit/686d6204bd46f757a4490656521b1094f3e3237e))
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
### Features
|
|
30
|
+
|
|
31
|
+
* created new rule no-boolean-coercion ([f9b0803](https://github.com/tomerh2001/eslint-plugin-th-rules/commit/f9b08039f6275e363da10e8e3ca49fb75ad2e48a))
|
|
32
|
+
|
|
1
33
|
## [2.1.1](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v2.1.0...v2.1.1) (2026-01-10)
|
|
2
34
|
|
|
3
35
|
|
package/README.md
CHANGED
|
@@ -14,16 +14,18 @@ This repository contains custom ESLint rules to enhance code quality and consist
|
|
|
14
14
|
✅ Set in the `recommended` configuration.\
|
|
15
15
|
⚛️ Set in the `recommended-react` configuration.\
|
|
16
16
|
🟦 Set in the `recommended-typescript` configuration.\
|
|
17
|
-
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix)
|
|
17
|
+
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
|
|
18
|
+
💡 Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
|
|
18
19
|
|
|
19
|
-
| Name | Description
|
|
20
|
-
| :--------------------------------------------------------------- |
|
|
21
|
-
| [no-
|
|
22
|
-
| [no-
|
|
23
|
-
| [no-
|
|
24
|
-
| [
|
|
25
|
-
| [
|
|
26
|
-
| [
|
|
20
|
+
| Name | Description | 💼 | 🔧 | 💡 |
|
|
21
|
+
| :--------------------------------------------------------------- | :--------------------------------------------------------------------------------------- | :------ | :- | :- |
|
|
22
|
+
| [no-boolean-coercion](docs/rules/no-boolean-coercion.md) | Disallow Boolean(variable) or !!variable and enforce explicit _.isNil / _.isEmpty checks | ✅ ⚛️ 🟦 | | 💡 |
|
|
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
|
+
| [schemas-in-schemas-file](docs/rules/schemas-in-schemas-file.md) | Require Zod schema declarations to be placed in a .schemas.ts file | ✅ ⚛️ 🟦 | | |
|
|
27
|
+
| [top-level-functions](docs/rules/top-level-functions.md) | Require all top-level functions to be named/regular functions. | ✅ ⚛️ 🟦 | 🔧 | |
|
|
28
|
+
| [types-in-dts](docs/rules/types-in-dts.md) | Require TypeScript type declarations (type/interface/enum) to be placed in .d.ts files | ✅ ⚛️ 🟦 | | |
|
|
27
29
|
|
|
28
30
|
<!-- end auto-generated rules list -->
|
|
29
31
|
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# th-rules/no-boolean-coercion
|
|
2
|
+
|
|
3
|
+
📝 Disallow Boolean(variable) or !!variable and enforce explicit _.isNil / _.isEmpty checks.
|
|
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
|
+
|
|
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.
|
|
3
|
+
"version": "2.4.0",
|
|
4
4
|
"description": "A List of custom ESLint rules created by Tomer Horowitz",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|
|
@@ -14,7 +14,6 @@
|
|
|
14
14
|
"lint": "npm-run-all \"lint:*\"",
|
|
15
15
|
"lint:eslint-docs": "npm-run-all \"update:eslint-docs -- --check\"",
|
|
16
16
|
"lint:js": "eslint .",
|
|
17
|
-
"test": "xo && mocha tests --recursive",
|
|
18
17
|
"update:eslint-docs": "eslint-doc-generator --config-emoji \"recommended,✅\" --config-emoji \"recommended-react,⚛️\" --config-emoji \"recommended-typescript,🟦\""
|
|
19
18
|
},
|
|
20
19
|
"dependencies": {
|
|
@@ -34,6 +33,7 @@
|
|
|
34
33
|
"eslint-plugin-sonarjs": "^3.0.5",
|
|
35
34
|
"eslint-plugin-unicorn": "^62.0.0",
|
|
36
35
|
"globals": "^17.0.0",
|
|
36
|
+
"jest": "^30.2.0",
|
|
37
37
|
"lodash": "^4.17.21",
|
|
38
38
|
"requireindex": "^1.2.0"
|
|
39
39
|
},
|
package/src/index.js
CHANGED
|
@@ -65,6 +65,7 @@ const baseRecommended = {
|
|
|
65
65
|
'th-rules/top-level-functions': 'error',
|
|
66
66
|
'th-rules/schemas-in-schemas-file': 'error',
|
|
67
67
|
'th-rules/types-in-dts': 'error',
|
|
68
|
+
'th-rules/no-boolean-coercion': 'error',
|
|
68
69
|
'unicorn/filename-case': 'off',
|
|
69
70
|
'unicorn/no-array-callback-reference': 'off',
|
|
70
71
|
'import/extensions': 'off',
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
const meta = {
|
|
2
|
+
type: 'problem',
|
|
3
|
+
docs: {
|
|
4
|
+
description:
|
|
5
|
+
'Disallow Boolean(value) or !!value. Enforce _.isNil(value) for scalar values and _.isEmpty(value) for strings, arrays, and objects.',
|
|
6
|
+
category: 'Best Practices',
|
|
7
|
+
recommended: true,
|
|
8
|
+
url: 'https://github.com/tomerh2001/eslint-plugin-th-rules/blob/main/docs/rules/no-boolean-coercion.md',
|
|
9
|
+
},
|
|
10
|
+
hasSuggestions: true,
|
|
11
|
+
schema: [],
|
|
12
|
+
messages: {
|
|
13
|
+
useIsEmpty:
|
|
14
|
+
'Boolean coercion is not allowed. Use _.isEmpty(value) for strings, arrays, and objects.',
|
|
15
|
+
useIsNil:
|
|
16
|
+
'Boolean coercion is not allowed. Use _.isNil(value) for scalar values.',
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
function create(context) {
|
|
21
|
+
const sourceCode = context.getSourceCode();
|
|
22
|
+
const services = context.parserServices;
|
|
23
|
+
const checker = services?.program?.getTypeChecker?.();
|
|
24
|
+
|
|
25
|
+
function isBooleanCall(node) {
|
|
26
|
+
return (
|
|
27
|
+
node.type === 'CallExpression'
|
|
28
|
+
&& node.callee.type === 'Identifier'
|
|
29
|
+
&& node.callee.name === 'Boolean'
|
|
30
|
+
&& node.arguments.length === 1
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isDoubleNegation(node) {
|
|
35
|
+
return (
|
|
36
|
+
node.type === 'UnaryExpression'
|
|
37
|
+
&& node.operator === '!'
|
|
38
|
+
&& node.argument?.type === 'UnaryExpression'
|
|
39
|
+
&& node.argument.operator === '!'
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function isCollectionLikeByTS(node) {
|
|
44
|
+
if (!checker || !services?.esTreeNodeToTSNodeMap) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const tsNode = services.esTreeNodeToTSNodeMap.get(node);
|
|
49
|
+
if (!tsNode) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const type = checker.getTypeAtLocation(tsNode);
|
|
54
|
+
|
|
55
|
+
const typeString = checker.typeToString(type);
|
|
56
|
+
|
|
57
|
+
return (
|
|
58
|
+
typeString.includes('[]')
|
|
59
|
+
|| typeString === 'string'
|
|
60
|
+
|| typeString === 'object'
|
|
61
|
+
|| typeString.startsWith('Array<')
|
|
62
|
+
|| typeString.startsWith('ReadonlyArray<')
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function isCollectionLikeBySyntax(node) {
|
|
67
|
+
return (
|
|
68
|
+
node.type === 'ArrayExpression'
|
|
69
|
+
|| node.type === 'ObjectExpression'
|
|
70
|
+
|| node.type === 'Literal' && typeof node.value === 'string'
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function report(node, valueNode) {
|
|
75
|
+
const isCollection
|
|
76
|
+
= isCollectionLikeBySyntax(valueNode)
|
|
77
|
+
|| isCollectionLikeByTS(valueNode);
|
|
78
|
+
|
|
79
|
+
const suggestedFn = isCollection ? '_.isEmpty' : '_.isNil';
|
|
80
|
+
const replacement = `${suggestedFn}(${sourceCode.getText(valueNode)})`;
|
|
81
|
+
|
|
82
|
+
context.report({
|
|
83
|
+
node,
|
|
84
|
+
messageId: isCollection ? 'useIsEmpty' : 'useIsNil',
|
|
85
|
+
suggest: [
|
|
86
|
+
{
|
|
87
|
+
desc: `Replace with ${replacement}`,
|
|
88
|
+
fix(fixer) {
|
|
89
|
+
return fixer.replaceText(node, replacement);
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
],
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
CallExpression(node) {
|
|
98
|
+
if (!isBooleanCall(node)) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const arg = node.arguments[0];
|
|
103
|
+
if (!arg) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
report(node, arg);
|
|
108
|
+
},
|
|
109
|
+
|
|
110
|
+
UnaryExpression(node) {
|
|
111
|
+
if (!isDoubleNegation(node)) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const valueNode = node.argument.argument;
|
|
116
|
+
if (!valueNode) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
report(node, valueNode);
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
module.exports = {meta, create};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
const {RuleTester} = require('eslint');
|
|
2
|
+
const rule = require('../src/rules/no-boolean-coercion');
|
|
3
|
+
|
|
4
|
+
const ruleTester = new RuleTester({});
|
|
5
|
+
|
|
6
|
+
ruleTester.run('no-boolean-coercion', rule, {
|
|
7
|
+
valid: [
|
|
8
|
+
'_.isNil(value);',
|
|
9
|
+
'_.isEmpty(list);',
|
|
10
|
+
'if (value != null) {}',
|
|
11
|
+
'if (list.length > 0) {}',
|
|
12
|
+
'Boolean;',
|
|
13
|
+
'const BooleanValue = true;',
|
|
14
|
+
'const fn = Boolean;',
|
|
15
|
+
],
|
|
16
|
+
|
|
17
|
+
invalid: [
|
|
18
|
+
{
|
|
19
|
+
code: 'Boolean(foo);',
|
|
20
|
+
errors: [
|
|
21
|
+
{
|
|
22
|
+
messageId: 'useIsNil',
|
|
23
|
+
suggestions: [
|
|
24
|
+
{
|
|
25
|
+
desc: 'Replace with _.isNil(foo)',
|
|
26
|
+
output: '_.isNil(foo);',
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
},
|
|
32
|
+
|
|
33
|
+
{
|
|
34
|
+
code: 'const x = Boolean(bar);',
|
|
35
|
+
errors: [
|
|
36
|
+
{
|
|
37
|
+
messageId: 'useIsNil',
|
|
38
|
+
suggestions: [
|
|
39
|
+
{
|
|
40
|
+
desc: 'Replace with _.isNil(bar)',
|
|
41
|
+
output: 'const x = _.isNil(bar);',
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
{
|
|
49
|
+
code: '!!value;',
|
|
50
|
+
errors: [
|
|
51
|
+
{
|
|
52
|
+
messageId: 'useIsNil',
|
|
53
|
+
suggestions: [
|
|
54
|
+
{
|
|
55
|
+
desc: 'Replace with _.isNil(value)',
|
|
56
|
+
output: '_.isNil(value);',
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
{
|
|
64
|
+
code: 'Boolean([]);',
|
|
65
|
+
errors: [
|
|
66
|
+
{
|
|
67
|
+
messageId: 'useIsEmpty',
|
|
68
|
+
suggestions: [
|
|
69
|
+
{
|
|
70
|
+
desc: 'Replace with _.isEmpty([])',
|
|
71
|
+
output: '_.isEmpty([]);',
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
},
|
|
77
|
+
],
|
|
78
|
+
});
|