eslint-plugin-th-rules 2.1.0 → 2.2.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 +14 -0
- package/README.md +11 -9
- package/docs/rules/no-boolean-coercion.md +9 -0
- package/package.json +2 -2
- package/src/index.js +6 -3
- package/src/rules/no-boolean-coercion.js +120 -0
- package/tests/no-boolean-coercion.test.ts +82 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
# [2.2.0](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v2.1.1...v2.2.0) (2026-01-14)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* created new rule no-boolean-coercion ([f9b0803](https://github.com/tomerh2001/eslint-plugin-th-rules/commit/f9b08039f6275e363da10e8e3ca49fb75ad2e48a))
|
|
7
|
+
|
|
8
|
+
## [2.1.1](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v2.1.0...v2.1.1) (2026-01-10)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* update base recommended rules to disable specific TypeScript and lodash rules ([6b4c240](https://github.com/tomerh2001/eslint-plugin-th-rules/commit/6b4c24062199bd8dda59407a844ae545903f8ddc))
|
|
14
|
+
|
|
1
15
|
# [2.1.0](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v2.0.4...v2.1.0) (2026-01-09)
|
|
2
16
|
|
|
3
17
|
|
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.2.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,7 +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
|
-
|
|
68
|
+
'th-rules/no-boolean-coercion': 'error',
|
|
69
69
|
'unicorn/filename-case': 'off',
|
|
70
70
|
'unicorn/no-array-callback-reference': 'off',
|
|
71
71
|
'import/extensions': 'off',
|
|
@@ -78,7 +78,6 @@ const baseRecommended = {
|
|
|
78
78
|
'n/prefer-global/process': 'off',
|
|
79
79
|
'import/no-cycle': 'off',
|
|
80
80
|
camelcase: 'warn',
|
|
81
|
-
|
|
82
81
|
'sonarjs/mouse-events-a11y': 'off',
|
|
83
82
|
'sonarjs/no-unstable-nested-components': 'off',
|
|
84
83
|
'unicorn/prefer-global-this': 'off',
|
|
@@ -86,6 +85,11 @@ const baseRecommended = {
|
|
|
86
85
|
'sonarjs/no-clear-text-protocols': 'off',
|
|
87
86
|
'security/detect-unsafe-regex': 'off',
|
|
88
87
|
'lodash/import-scope': [2, 'full'],
|
|
88
|
+
'@typescript-eslint/ban-types': 'off',
|
|
89
|
+
'@typescript-eslint/naming-convention': 'off',
|
|
90
|
+
'@typescript-eslint/no-restricted-types': 'off',
|
|
91
|
+
'import-x/extensions': 'off',
|
|
92
|
+
'lodash/chaining': ['error', 'implicit'],
|
|
89
93
|
},
|
|
90
94
|
};
|
|
91
95
|
|
|
@@ -110,7 +114,6 @@ plugin.configs['recommended-typescript'] = flatConfigs(
|
|
|
110
114
|
},
|
|
111
115
|
},
|
|
112
116
|
rules: {
|
|
113
|
-
'@typescript-eslint/naming-convention': 'warn',
|
|
114
117
|
'@typescript-eslint/consistent-type-definitions': 'off',
|
|
115
118
|
'@typescript-eslint/no-extraneous-class': 'off',
|
|
116
119
|
'@typescript-eslint/no-duplicate-type-constituents': 'off',
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
const meta = {
|
|
2
|
+
type: 'problem',
|
|
3
|
+
docs: {
|
|
4
|
+
description:
|
|
5
|
+
'Disallow Boolean(variable) or !!variable and enforce explicit _.isNil / _.isEmpty checks',
|
|
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
|
+
};
|
|
13
|
+
|
|
14
|
+
function create(context) {
|
|
15
|
+
const sourceCode = context.getSourceCode();
|
|
16
|
+
const services = context.parserServices;
|
|
17
|
+
const checker = services?.program?.getTypeChecker?.();
|
|
18
|
+
|
|
19
|
+
function isBooleanCall(node) {
|
|
20
|
+
return (
|
|
21
|
+
node.type === 'CallExpression'
|
|
22
|
+
&& node.callee.type === 'Identifier'
|
|
23
|
+
&& node.callee.name === 'Boolean'
|
|
24
|
+
&& node.arguments.length === 1
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isDoubleNegation(node) {
|
|
29
|
+
return (
|
|
30
|
+
node.type === 'UnaryExpression'
|
|
31
|
+
&& node.operator === '!'
|
|
32
|
+
&& node.argument?.type === 'UnaryExpression'
|
|
33
|
+
&& node.argument.operator === '!'
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function isCollectionLikeByTS(node) {
|
|
38
|
+
if (!checker || !services?.esTreeNodeToTSNodeMap) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const tsNode = services.esTreeNodeToTSNodeMap.get(node);
|
|
43
|
+
if (!tsNode) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const type = checker.getTypeAtLocation(tsNode);
|
|
48
|
+
|
|
49
|
+
const typeString = checker.typeToString(type);
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
typeString.includes('[]')
|
|
53
|
+
|| typeString === 'string'
|
|
54
|
+
|| typeString === 'object'
|
|
55
|
+
|| typeString.startsWith('Array<')
|
|
56
|
+
|| typeString.startsWith('ReadonlyArray<')
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function isCollectionLikeBySyntax(node) {
|
|
61
|
+
return (
|
|
62
|
+
node.type === 'ArrayExpression'
|
|
63
|
+
|| node.type === 'ObjectExpression'
|
|
64
|
+
|| node.type === 'Literal' && typeof node.value === 'string'
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function report(node, valueNode) {
|
|
69
|
+
const isCollection
|
|
70
|
+
= isCollectionLikeBySyntax(valueNode)
|
|
71
|
+
|| isCollectionLikeByTS(valueNode);
|
|
72
|
+
|
|
73
|
+
const suggestedFn = isCollection ? '_.isEmpty' : '_.isNil';
|
|
74
|
+
const replacement = `${suggestedFn}(${sourceCode.getText(valueNode)})`;
|
|
75
|
+
|
|
76
|
+
context.report({
|
|
77
|
+
node,
|
|
78
|
+
message:
|
|
79
|
+
'Boolean coercion is not allowed. Use an explicit null/empty check instead.',
|
|
80
|
+
suggest: [
|
|
81
|
+
{
|
|
82
|
+
desc: `Replace with ${replacement}`,
|
|
83
|
+
fix(fixer) {
|
|
84
|
+
return fixer.replaceText(node, replacement);
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
CallExpression(node) {
|
|
93
|
+
if (!isBooleanCall(node)) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const arg = node.arguments[0];
|
|
98
|
+
if (!arg) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
report(node, arg);
|
|
103
|
+
},
|
|
104
|
+
|
|
105
|
+
UnaryExpression(node) {
|
|
106
|
+
if (!isDoubleNegation(node)) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const valueNode = node.argument.argument;
|
|
111
|
+
if (!valueNode) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
report(node, valueNode);
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
module.exports = {meta, create};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
const {RuleTester} = require('eslint');
|
|
2
|
+
const rule = require('../src/rules/no-boolean-coercion');
|
|
3
|
+
|
|
4
|
+
const ruleTester = new RuleTester({});
|
|
5
|
+
|
|
6
|
+
const ERROR_MESSAGE
|
|
7
|
+
= 'Boolean coercion is not allowed. Use an explicit null/empty check instead.';
|
|
8
|
+
|
|
9
|
+
ruleTester.run('no-boolean-coercion', rule, {
|
|
10
|
+
valid: [
|
|
11
|
+
'_.isNil(value);',
|
|
12
|
+
'_.isEmpty(list);',
|
|
13
|
+
'if (value != null) {}',
|
|
14
|
+
'if (list.length > 0) {}',
|
|
15
|
+
'Boolean;',
|
|
16
|
+
'const BooleanValue = true;',
|
|
17
|
+
'const fn = Boolean;',
|
|
18
|
+
],
|
|
19
|
+
|
|
20
|
+
invalid: [
|
|
21
|
+
{
|
|
22
|
+
code: 'Boolean(foo);',
|
|
23
|
+
errors: [
|
|
24
|
+
{
|
|
25
|
+
message: ERROR_MESSAGE,
|
|
26
|
+
suggestions: [
|
|
27
|
+
{
|
|
28
|
+
desc: 'Replace with _.isNil(foo)',
|
|
29
|
+
output: '_.isNil(foo);',
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
{
|
|
37
|
+
code: 'const x = Boolean(bar);',
|
|
38
|
+
errors: [
|
|
39
|
+
{
|
|
40
|
+
message: ERROR_MESSAGE,
|
|
41
|
+
suggestions: [
|
|
42
|
+
{
|
|
43
|
+
desc: 'Replace with _.isNil(bar)',
|
|
44
|
+
output: 'const x = _.isNil(bar);',
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
{
|
|
52
|
+
code: '!!value;',
|
|
53
|
+
errors: [
|
|
54
|
+
{
|
|
55
|
+
message: ERROR_MESSAGE,
|
|
56
|
+
suggestions: [
|
|
57
|
+
{
|
|
58
|
+
desc: 'Replace with _.isNil(value)',
|
|
59
|
+
output: '_.isNil(value);',
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
},
|
|
65
|
+
|
|
66
|
+
{
|
|
67
|
+
code: 'Boolean([]);',
|
|
68
|
+
errors: [
|
|
69
|
+
{
|
|
70
|
+
message: ERROR_MESSAGE,
|
|
71
|
+
suggestions: [
|
|
72
|
+
{
|
|
73
|
+
desc: 'Replace with _.isEmpty([])',
|
|
74
|
+
output: '_.isEmpty([]);',
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
],
|
|
82
|
+
});
|