eslint-plugin-th-rules 3.2.2 → 3.2.3
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/README.md +13 -13
- package/dist/plugin.d.ts +8 -8
- package/dist/plugin.js +1 -1
- package/dist/rules/no-default-export.js +1 -1
- package/dist/rules/prefer-is-empty.d.ts +1 -1
- package/dist/rules/prefer-is-empty.d.ts.map +1 -1
- package/dist/rules/prefer-is-empty.js +103 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -164,19 +164,19 @@ Do not edit below this line.
|
|
|
164
164
|
⚛️ Set in the `recommendedReact` configuration.\
|
|
165
165
|
🟦 Set in the `recommendedTypescript` configuration.\
|
|
166
166
|
🎲 Set in the `recommendedTypescriptReact` configuration.\
|
|
167
|
-
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix)
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
|
171
|
-
|
|
|
172
|
-
| [
|
|
173
|
-
| [
|
|
174
|
-
| [
|
|
175
|
-
| [
|
|
176
|
-
| [
|
|
177
|
-
| [
|
|
178
|
-
| [
|
|
179
|
-
| [
|
|
167
|
+
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).
|
|
168
|
+
|
|
169
|
+
| Name | Description | 💼 | 🔧 |
|
|
170
|
+
| :--------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------- | :- |
|
|
171
|
+
| [no-boolean-coercion](docs/rules/no-boolean-coercion.md) | Disallow Boolean(value) or !!value. Enforce explicit checks: !_.isNil(value) for scalars and !_.isEmpty(value) for strings, arrays, and objects. If the value is already boolean, remove coercion. | ✅ ⚛️ 🟦 🎲 | 🔧 |
|
|
172
|
+
| [no-comments](docs/rules/no-comments.md) | Disallow comments except for specified allowed patterns. | ✅ ⚛️ 🟦 🎲 | 🔧 |
|
|
173
|
+
| [no-default-export](docs/rules/no-default-export.md) | Convert unnamed default exports to named default exports based on the file name. | ✅ ⚛️ 🟦 🎲 | 🔧 |
|
|
174
|
+
| [no-destructuring](docs/rules/no-destructuring.md) | Disallow destructuring that does not meet certain conditions. | ✅ ⚛️ 🟦 🎲 | |
|
|
175
|
+
| [no-explicit-nil-compare](docs/rules/no-explicit-nil-compare.md) | Disallow direct comparisons to null or undefined. Use _.isNull(x) / _.isUndefined(x) instead. | ✅ ⚛️ 🟦 🎲 | 🔧 |
|
|
176
|
+
| [prefer-is-empty](docs/rules/prefer-is-empty.md) | Require _.isEmpty instead of length comparisons or !x.length checks. | ✅ ⚛️ 🟦 🎲 | 🔧 |
|
|
177
|
+
| [schemas-in-schemas-file](docs/rules/schemas-in-schemas-file.md) | Require Zod schema declarations to be placed in a .schemas.ts file. | ✅ ⚛️ 🟦 🎲 | |
|
|
178
|
+
| [top-level-functions](docs/rules/top-level-functions.md) | Require all top-level functions to be named regular functions. | ✅ ⚛️ 🟦 🎲 | 🔧 |
|
|
179
|
+
| [types-in-dts](docs/rules/types-in-dts.md) | Require TypeScript type declarations (type/interface/enum) to be placed in .d.ts files. | ✅ ⚛️ 🟦 🎲 | |
|
|
180
180
|
|
|
181
181
|
<!-- end auto-generated rules list -->
|
|
182
182
|
|
package/dist/plugin.d.ts
CHANGED
|
@@ -17,7 +17,10 @@ export declare const rules: {
|
|
|
17
17
|
}], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
18
18
|
name: string;
|
|
19
19
|
};
|
|
20
|
-
'
|
|
20
|
+
'no-explicit-nil-compare': import("@typescript-eslint/utils/ts-eslint").RuleModule<"useIsNull" | "useIsUndefined", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
21
|
+
name: string;
|
|
22
|
+
};
|
|
23
|
+
'prefer-is-empty': import("@typescript-eslint/utils/ts-eslint").RuleModule<"useIsEmpty" | "useIsEmptyUnary" | "useIsEmptyBoolean", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
21
24
|
name: string;
|
|
22
25
|
};
|
|
23
26
|
'schemas-in-schemas-file': import("@typescript-eslint/utils/ts-eslint").RuleModule<"moveSchema", [{
|
|
@@ -36,9 +39,6 @@ export declare const rules: {
|
|
|
36
39
|
}], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
37
40
|
name: string;
|
|
38
41
|
};
|
|
39
|
-
'no-explicit-nil-compare': import("@typescript-eslint/utils/ts-eslint").RuleModule<"useIsNull" | "useIsUndefined", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
40
|
-
name: string;
|
|
41
|
-
};
|
|
42
42
|
};
|
|
43
43
|
declare const plugin: {
|
|
44
44
|
rules: {
|
|
@@ -60,7 +60,10 @@ declare const plugin: {
|
|
|
60
60
|
}], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
61
61
|
name: string;
|
|
62
62
|
};
|
|
63
|
-
'
|
|
63
|
+
'no-explicit-nil-compare': import("@typescript-eslint/utils/ts-eslint").RuleModule<"useIsNull" | "useIsUndefined", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
64
|
+
name: string;
|
|
65
|
+
};
|
|
66
|
+
'prefer-is-empty': import("@typescript-eslint/utils/ts-eslint").RuleModule<"useIsEmpty" | "useIsEmptyUnary" | "useIsEmptyBoolean", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
64
67
|
name: string;
|
|
65
68
|
};
|
|
66
69
|
'schemas-in-schemas-file': import("@typescript-eslint/utils/ts-eslint").RuleModule<"moveSchema", [{
|
|
@@ -79,9 +82,6 @@ declare const plugin: {
|
|
|
79
82
|
}], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
80
83
|
name: string;
|
|
81
84
|
};
|
|
82
|
-
'no-explicit-nil-compare': import("@typescript-eslint/utils/ts-eslint").RuleModule<"useIsNull" | "useIsUndefined", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
83
|
-
name: string;
|
|
84
|
-
};
|
|
85
85
|
};
|
|
86
86
|
};
|
|
87
87
|
export default plugin;
|
package/dist/plugin.js
CHANGED
|
@@ -12,11 +12,11 @@ export const rules = {
|
|
|
12
12
|
'no-comments': noComments,
|
|
13
13
|
'no-default-export': noDefaultExport,
|
|
14
14
|
'no-destructuring': noDestructuring,
|
|
15
|
+
'no-explicit-nil-compare': noExplicitNilCompare,
|
|
15
16
|
'prefer-is-empty': preferIsEmpty,
|
|
16
17
|
'schemas-in-schemas-file': schemasInSchemasFile,
|
|
17
18
|
'top-level-functions': topLevelFunctions,
|
|
18
19
|
'types-in-dts': typesInDts,
|
|
19
|
-
'no-explicit-nil-compare': noExplicitNilCompare,
|
|
20
20
|
};
|
|
21
21
|
const plugin = { rules };
|
|
22
22
|
export default plugin;
|
|
@@ -20,7 +20,7 @@ const noDefaultExport = ESLintUtils.RuleCreator(() => 'https://github.com/tomerh
|
|
|
20
20
|
function generateExportNameFromFileName(fileName) {
|
|
21
21
|
const cleaned = fileName.replaceAll(/[^a-zA-Z\d]+/g, ' ');
|
|
22
22
|
const parts = cleaned.trim().split(/\s+/g).filter(Boolean);
|
|
23
|
-
if (parts
|
|
23
|
+
if (_.isEmpty(parts)) {
|
|
24
24
|
return 'defaultExport';
|
|
25
25
|
}
|
|
26
26
|
const [first, ...rest] = parts;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
2
|
-
declare const preferIsEmpty: ESLintUtils.RuleModule<"useIsEmpty" | "useIsEmptyUnary", [], unknown, ESLintUtils.RuleListener> & {
|
|
2
|
+
declare const preferIsEmpty: ESLintUtils.RuleModule<"useIsEmpty" | "useIsEmptyUnary" | "useIsEmptyBoolean", [], unknown, ESLintUtils.RuleListener> & {
|
|
3
3
|
name: string;
|
|
4
4
|
};
|
|
5
5
|
export default preferIsEmpty;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefer-is-empty.d.ts","sourceRoot":"","sources":["../../src/rules/prefer-is-empty.ts"],"names":[],"mappings":"AAKA,OAAO,EAAkB,WAAW,EAAgC,MAAM,0BAA0B,CAAC;AAIrG,QAAA,MAAM,aAAa;;
|
|
1
|
+
{"version":3,"file":"prefer-is-empty.d.ts","sourceRoot":"","sources":["../../src/rules/prefer-is-empty.ts"],"names":[],"mappings":"AAKA,OAAO,EAAkB,WAAW,EAAgC,MAAM,0BAA0B,CAAC;AAIrG,QAAA,MAAM,aAAa;;CA2NjB,CAAC;AAEH,eAAe,aAAa,CAAC"}
|
|
@@ -8,13 +8,14 @@ const preferIsEmpty = ESLintUtils.RuleCreator(() => 'https://github.com/tomerh20
|
|
|
8
8
|
meta: {
|
|
9
9
|
type: 'problem',
|
|
10
10
|
docs: {
|
|
11
|
-
description: 'Require _.isEmpty instead of length comparisons or
|
|
11
|
+
description: 'Require _.isEmpty instead of length comparisons or boolean checks on .length.',
|
|
12
12
|
},
|
|
13
13
|
fixable: 'code',
|
|
14
14
|
schema: [],
|
|
15
15
|
messages: {
|
|
16
16
|
useIsEmpty: 'Use _.isEmpty({{collection}}) instead of checking {{collection}}.length {{operator}} {{value}}.',
|
|
17
17
|
useIsEmptyUnary: 'Use _.isEmpty({{collection}}) instead of negating {{collection}}.length.',
|
|
18
|
+
useIsEmptyBoolean: 'Use _.isEmpty({{collection}}) instead of boolean checking {{collection}}.length.',
|
|
18
19
|
},
|
|
19
20
|
},
|
|
20
21
|
defaultOptions: [],
|
|
@@ -28,12 +29,54 @@ const preferIsEmpty = ESLintUtils.RuleCreator(() => 'https://github.com/tomerh20
|
|
|
28
29
|
const firstImport = imports[0];
|
|
29
30
|
return firstImport ? fixer.insertTextBefore(firstImport, `import _ from 'lodash';\n`) : fixer.insertTextBeforeRange([0, 0], `import _ from 'lodash';\n`);
|
|
30
31
|
}
|
|
32
|
+
function unwrapChain(node) {
|
|
33
|
+
return node?.type === AST_NODE_TYPES.ChainExpression ? node.expression : node;
|
|
34
|
+
}
|
|
31
35
|
function isLengthAccess(node) {
|
|
32
|
-
|
|
36
|
+
const unwrapped = unwrapChain(node);
|
|
37
|
+
return (!_.isNil(unwrapped) &&
|
|
38
|
+
unwrapped.type === AST_NODE_TYPES.MemberExpression &&
|
|
39
|
+
unwrapped.property.type === AST_NODE_TYPES.Identifier &&
|
|
40
|
+
unwrapped.property.name === 'length' &&
|
|
41
|
+
!unwrapped.computed);
|
|
42
|
+
}
|
|
43
|
+
function getLengthMember(node) {
|
|
44
|
+
return unwrapChain(node);
|
|
33
45
|
}
|
|
34
46
|
function isNumericLiteral(node) {
|
|
35
47
|
return !_.isNil(node) && node.type === AST_NODE_TYPES.Literal && typeof node.value === 'number';
|
|
36
48
|
}
|
|
49
|
+
function isDoubleNegationLength(node) {
|
|
50
|
+
return node.operator === '!' && node.argument.type === AST_NODE_TYPES.UnaryExpression && node.argument.operator === '!' && isLengthAccess(node.argument.argument);
|
|
51
|
+
}
|
|
52
|
+
function isBooleanContext(node) {
|
|
53
|
+
const { parent } = node;
|
|
54
|
+
if (!parent)
|
|
55
|
+
return false;
|
|
56
|
+
switch (parent.type) {
|
|
57
|
+
case AST_NODE_TYPES.IfStatement:
|
|
58
|
+
case AST_NODE_TYPES.WhileStatement:
|
|
59
|
+
case AST_NODE_TYPES.DoWhileStatement:
|
|
60
|
+
case AST_NODE_TYPES.ForStatement: {
|
|
61
|
+
return parent.test === node;
|
|
62
|
+
}
|
|
63
|
+
case AST_NODE_TYPES.UnaryExpression: {
|
|
64
|
+
return parent.operator === '!';
|
|
65
|
+
}
|
|
66
|
+
case AST_NODE_TYPES.LogicalExpression: {
|
|
67
|
+
return parent.operator === '&&' || parent.operator === '||';
|
|
68
|
+
}
|
|
69
|
+
case AST_NODE_TYPES.ConditionalExpression: {
|
|
70
|
+
return parent.test === node;
|
|
71
|
+
}
|
|
72
|
+
case AST_NODE_TYPES.CallExpression: {
|
|
73
|
+
return parent.callee.type === AST_NODE_TYPES.Identifier && parent.callee.name === 'Boolean' && parent.arguments.length === 1 && parent.arguments[0] === node;
|
|
74
|
+
}
|
|
75
|
+
default: {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
37
80
|
function reportBinary(node, lengthNode, operator, value, isEmptyCheck) {
|
|
38
81
|
const collection = sourceCode.getText(lengthNode.object);
|
|
39
82
|
const replacement = isEmptyCheck ? `_.isEmpty(${collection})` : `!_.isEmpty(${collection})`;
|
|
@@ -65,34 +108,79 @@ const preferIsEmpty = ESLintUtils.RuleCreator(() => 'https://github.com/tomerh20
|
|
|
65
108
|
},
|
|
66
109
|
});
|
|
67
110
|
}
|
|
111
|
+
function reportBoolean(node, lengthNode) {
|
|
112
|
+
const collection = sourceCode.getText(lengthNode.object);
|
|
113
|
+
context.report({
|
|
114
|
+
node,
|
|
115
|
+
messageId: 'useIsEmptyBoolean',
|
|
116
|
+
data: { collection },
|
|
117
|
+
fix(fixer) {
|
|
118
|
+
const fixes = [fixer.replaceText(node, `!_.isEmpty(${collection})`)];
|
|
119
|
+
const importFix = ensureLodashImport(fixer);
|
|
120
|
+
if (importFix)
|
|
121
|
+
fixes.push(importFix);
|
|
122
|
+
return fixes;
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
}
|
|
68
126
|
return {
|
|
69
127
|
BinaryExpression(node) {
|
|
70
128
|
if (isLengthAccess(node.left) && isNumericLiteral(node.right)) {
|
|
71
|
-
|
|
72
|
-
|
|
129
|
+
const right = node.right.value;
|
|
130
|
+
if ((node.operator === '===' && right === 0) || (node.operator === '<=' && right === 0) || (node.operator === '<' && right === 1)) {
|
|
131
|
+
reportBinary(node, getLengthMember(node.left), node.operator, right, true);
|
|
73
132
|
return;
|
|
74
133
|
}
|
|
75
|
-
if ((node.operator === '>' && node.right.
|
|
76
|
-
(node.
|
|
77
|
-
|
|
78
|
-
reportBinary(node, node.left, node.operator, node.right.value, false);
|
|
134
|
+
if ((node.operator === '>' && right === 0) || (node.operator === '>=' && right === 1) || ((node.operator === '!=' || node.operator === '!==') && right === 0)) {
|
|
135
|
+
reportBinary(node, getLengthMember(node.left), node.operator, right, false);
|
|
136
|
+
return;
|
|
79
137
|
}
|
|
80
138
|
}
|
|
81
139
|
if (isNumericLiteral(node.left) && isLengthAccess(node.right)) {
|
|
82
|
-
|
|
83
|
-
|
|
140
|
+
const left = node.left.value;
|
|
141
|
+
if ((node.operator === '===' && left === 0) || (node.operator === '>=' && left === 0) || (node.operator === '<=' && left === 0)) {
|
|
142
|
+
reportBinary(node, getLengthMember(node.right), node.operator, left, true);
|
|
84
143
|
return;
|
|
85
144
|
}
|
|
86
|
-
if (
|
|
87
|
-
reportBinary(node, node.right, node.operator,
|
|
145
|
+
if (node.operator === '<' && left === 0) {
|
|
146
|
+
reportBinary(node, getLengthMember(node.right), node.operator, left, false);
|
|
88
147
|
}
|
|
89
148
|
}
|
|
90
149
|
},
|
|
91
150
|
UnaryExpression(node) {
|
|
92
|
-
if (node.operator
|
|
151
|
+
if (node.parent?.type === AST_NODE_TYPES.UnaryExpression && node.parent.operator === '!' && isLengthAccess(node.argument)) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (isDoubleNegationLength(node)) {
|
|
155
|
+
const inner = node.argument;
|
|
156
|
+
const lengthNode = getLengthMember(inner.argument);
|
|
157
|
+
reportBoolean(node, lengthNode);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const arg = unwrapChain(node.argument);
|
|
161
|
+
if (!isLengthAccess(arg))
|
|
93
162
|
return;
|
|
94
|
-
if (
|
|
95
|
-
|
|
163
|
+
if (isBooleanContext(node)) {
|
|
164
|
+
reportBoolean(node, getLengthMember(arg));
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
if (node.operator === '!') {
|
|
168
|
+
reportUnary(node, getLengthMember(arg));
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
ConditionalExpression(node) {
|
|
172
|
+
if (isLengthAccess(node.test) && isBooleanContext(node.test)) {
|
|
173
|
+
reportBoolean(node.test, getLengthMember(node.test));
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
LogicalExpression(node) {
|
|
177
|
+
if ((node.operator === '&&' || node.operator === '||') && isLengthAccess(node.left)) {
|
|
178
|
+
reportBoolean(node.left, getLengthMember(node.left));
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
IfStatement(node) {
|
|
182
|
+
if (isLengthAccess(node.test)) {
|
|
183
|
+
reportBoolean(node.test, getLengthMember(node.test));
|
|
96
184
|
}
|
|
97
185
|
},
|
|
98
186
|
};
|