eslint-plugin-th-rules 3.2.1 → 3.2.2
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/dist/index.d.ts.map +1 -1
- package/dist/plugin.d.ts +2 -2
- package/dist/rules/no-boolean-coercion.d.ts.map +1 -1
- package/dist/rules/no-boolean-coercion.js +20 -2
- package/dist/rules/no-default-export.d.ts.map +1 -1
- package/dist/rules/no-default-export.js +3 -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 +52 -28
- package/dist/rules/types-in-dts.d.ts.map +1 -1
- package/dist/rules/types-in-dts.js +4 -6
- package/package.json +1 -1
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,MAAM,EAAE,KAAK,IAAI,EAAE,MAAM,QAAQ,CAAC;AAQhD,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAEpC,eAAO,MAAM,OAAO;;;;;CAKnB,CAAC;AAEF,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAC9D,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAE,oBAAoB,EAAE,MAAM,oCAAoC,CAAC;AAE1E,QAAA,MAAM,KAAK,EAAoC;IAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,CAAA;CAAE,CAAC;AACpI,eAAe,KAAK,CAAC"}
|
package/dist/plugin.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ export declare const rules: {
|
|
|
17
17
|
}], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
18
18
|
name: string;
|
|
19
19
|
};
|
|
20
|
-
'prefer-is-empty': import("@typescript-eslint/utils/ts-eslint").RuleModule<"useIsEmpty", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
20
|
+
'prefer-is-empty': import("@typescript-eslint/utils/ts-eslint").RuleModule<"useIsEmpty" | "useIsEmptyUnary", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
21
21
|
name: string;
|
|
22
22
|
};
|
|
23
23
|
'schemas-in-schemas-file': import("@typescript-eslint/utils/ts-eslint").RuleModule<"moveSchema", [{
|
|
@@ -60,7 +60,7 @@ declare const plugin: {
|
|
|
60
60
|
}], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
61
61
|
name: string;
|
|
62
62
|
};
|
|
63
|
-
'prefer-is-empty': import("@typescript-eslint/utils/ts-eslint").RuleModule<"useIsEmpty", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
63
|
+
'prefer-is-empty': import("@typescript-eslint/utils/ts-eslint").RuleModule<"useIsEmpty" | "useIsEmptyUnary", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
64
64
|
name: string;
|
|
65
65
|
};
|
|
66
66
|
'schemas-in-schemas-file': import("@typescript-eslint/utils/ts-eslint").RuleModule<"moveSchema", [{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"no-boolean-coercion.d.ts","sourceRoot":"","sources":["../../src/rules/no-boolean-coercion.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"no-boolean-coercion.d.ts","sourceRoot":"","sources":["../../src/rules/no-boolean-coercion.ts"],"names":[],"mappings":"AAEA,OAAO,EAAkB,WAAW,EAAiB,MAAM,0BAA0B,CAAC;AAStF,QAAA,MAAM,iBAAiB;;CAyHrB,CAAC;AAEH,eAAe,iBAAiB,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
2
1
|
/* eslint-disable new-cap */
|
|
3
2
|
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
|
|
4
3
|
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils';
|
|
4
|
+
import * as ts from 'typescript';
|
|
5
5
|
const createRule = ESLintUtils.RuleCreator(() => 'https://github.com/tomerh2001/eslint-plugin-th-rules/blob/main/docs/rules/no-boolean-coercion.md');
|
|
6
6
|
const LODASH_MODULE = 'lodash';
|
|
7
7
|
const LODASH_IDENT = '_';
|
|
@@ -11,7 +11,7 @@ const noBooleanCoercion = createRule({
|
|
|
11
11
|
type: 'problem',
|
|
12
12
|
fixable: 'code',
|
|
13
13
|
docs: {
|
|
14
|
-
description: 'Disallow Boolean(value) or !!value. Enforce explicit checks: !_.isNil(value) for scalars and !_.isEmpty(value) for strings, arrays, and objects.',
|
|
14
|
+
description: '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.',
|
|
15
15
|
},
|
|
16
16
|
schema: [],
|
|
17
17
|
messages: {
|
|
@@ -43,6 +43,14 @@ const noBooleanCoercion = createRule({
|
|
|
43
43
|
function isDoubleNegation(node) {
|
|
44
44
|
return node.type === AST_NODE_TYPES.UnaryExpression && node.operator === '!' && node.argument.type === AST_NODE_TYPES.UnaryExpression && node.argument.operator === '!';
|
|
45
45
|
}
|
|
46
|
+
function isBooleanByTS(node) {
|
|
47
|
+
const tsNode = services.esTreeNodeToTSNodeMap.get(node);
|
|
48
|
+
if (!tsNode) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
const type = checker.getTypeAtLocation(tsNode);
|
|
52
|
+
return (type.flags & ts.TypeFlags.Boolean) !== 0 || (type.flags & ts.TypeFlags.BooleanLiteral) !== 0; // eslint-disable-line no-bitwise
|
|
53
|
+
}
|
|
46
54
|
function isCollectionLikeByTS(node) {
|
|
47
55
|
const tsNode = services.esTreeNodeToTSNodeMap.get(node);
|
|
48
56
|
if (!tsNode) {
|
|
@@ -56,6 +64,16 @@ const noBooleanCoercion = createRule({
|
|
|
56
64
|
return node.type === AST_NODE_TYPES.ArrayExpression || node.type === AST_NODE_TYPES.ObjectExpression || (node.type === AST_NODE_TYPES.Literal && typeof node.value === 'string');
|
|
57
65
|
}
|
|
58
66
|
function report(node, valueNode) {
|
|
67
|
+
if (isBooleanByTS(valueNode)) {
|
|
68
|
+
context.report({
|
|
69
|
+
node,
|
|
70
|
+
messageId: 'useIsNil',
|
|
71
|
+
fix(fixer) {
|
|
72
|
+
return fixer.replaceText(node, context.sourceCode.getText(valueNode));
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
59
77
|
const isCollection = isCollectionLikeBySyntax(valueNode) || isCollectionLikeByTS(valueNode);
|
|
60
78
|
const fnName = isCollection ? 'isEmpty' : 'isNil';
|
|
61
79
|
const replacement = `!${LODASH_IDENT}.${fnName}(${context.sourceCode.getText(valueNode)})`;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"no-default-export.d.ts","sourceRoot":"","sources":["../../src/rules/no-default-export.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"no-default-export.d.ts","sourceRoot":"","sources":["../../src/rules/no-default-export.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAiB,MAAM,0BAA0B,CAAC;AAEtE,QAAA,MAAM,eAAe;;CA2DnB,CAAC;AACH,eAAe,eAAe,CAAC"}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
/* eslint-disable new-cap */
|
|
1
2
|
import * as path from 'node:path';
|
|
3
|
+
import _ from 'lodash';
|
|
2
4
|
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
3
5
|
const noDefaultExport = ESLintUtils.RuleCreator(() => 'https://github.com/tomerh2001/eslint-plugin-th-rules/blob/main/docs/rules/no-default-export.md')({
|
|
4
6
|
name: 'no-default-export',
|
|
@@ -29,7 +31,7 @@ const noDefaultExport = ESLintUtils.RuleCreator(() => 'https://github.com/tomerh
|
|
|
29
31
|
if (node.declaration.type === 'Identifier') {
|
|
30
32
|
return;
|
|
31
33
|
}
|
|
32
|
-
if ('id' in node.declaration && node.declaration.id
|
|
34
|
+
if ('id' in node.declaration && !_.isNull(node.declaration.id)) {
|
|
33
35
|
return;
|
|
34
36
|
}
|
|
35
37
|
const fileName = context.getFilename();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
2
|
-
declare const preferIsEmpty: ESLintUtils.RuleModule<"useIsEmpty", [], unknown, ESLintUtils.RuleListener> & {
|
|
2
|
+
declare const preferIsEmpty: ESLintUtils.RuleModule<"useIsEmpty" | "useIsEmptyUnary", [], 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":"
|
|
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;;CAiHjB,CAAC;AAEH,eAAe,aAAa,CAAC"}
|
|
@@ -1,76 +1,100 @@
|
|
|
1
|
+
/* eslint-disable th-rules/types-in-dts */
|
|
1
2
|
/* eslint-disable new-cap */
|
|
2
3
|
/* eslint-disable complexity */
|
|
4
|
+
import _ from 'lodash';
|
|
3
5
|
import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils';
|
|
4
6
|
const preferIsEmpty = ESLintUtils.RuleCreator(() => 'https://github.com/tomerh2001/eslint-plugin-th-rules/blob/main/docs/rules/prefer-is-empty.md')({
|
|
5
7
|
name: 'prefer-is-empty',
|
|
6
8
|
meta: {
|
|
7
9
|
type: 'problem',
|
|
8
10
|
docs: {
|
|
9
|
-
description: 'Require _.isEmpty instead of length comparisons.',
|
|
11
|
+
description: 'Require _.isEmpty instead of length comparisons or !x.length checks.',
|
|
10
12
|
},
|
|
11
|
-
|
|
13
|
+
fixable: 'code',
|
|
12
14
|
schema: [],
|
|
13
15
|
messages: {
|
|
14
16
|
useIsEmpty: 'Use _.isEmpty({{collection}}) instead of checking {{collection}}.length {{operator}} {{value}}.',
|
|
17
|
+
useIsEmptyUnary: 'Use _.isEmpty({{collection}}) instead of negating {{collection}}.length.',
|
|
15
18
|
},
|
|
16
19
|
},
|
|
17
20
|
defaultOptions: [],
|
|
18
21
|
create(context) {
|
|
22
|
+
const { sourceCode } = context;
|
|
23
|
+
function ensureLodashImport(fixer) {
|
|
24
|
+
const imports = sourceCode.ast.body.filter((node) => node.type === AST_NODE_TYPES.ImportDeclaration);
|
|
25
|
+
const hasLodash = imports.some((imp) => imp.source.value === 'lodash' && imp.specifiers.some((s) => s.type === AST_NODE_TYPES.ImportDefaultSpecifier || s.type === AST_NODE_TYPES.ImportNamespaceSpecifier));
|
|
26
|
+
if (hasLodash)
|
|
27
|
+
return null;
|
|
28
|
+
const firstImport = imports[0];
|
|
29
|
+
return firstImport ? fixer.insertTextBefore(firstImport, `import _ from 'lodash';\n`) : fixer.insertTextBeforeRange([0, 0], `import _ from 'lodash';\n`);
|
|
30
|
+
}
|
|
19
31
|
function isLengthAccess(node) {
|
|
20
|
-
return node
|
|
32
|
+
return !_.isNil(node) && node.type === AST_NODE_TYPES.MemberExpression && node.property.type === AST_NODE_TYPES.Identifier && node.property.name === 'length' && !node.computed;
|
|
21
33
|
}
|
|
22
34
|
function isNumericLiteral(node) {
|
|
23
|
-
return node
|
|
35
|
+
return !_.isNil(node) && node.type === AST_NODE_TYPES.Literal && typeof node.value === 'number';
|
|
24
36
|
}
|
|
25
|
-
function
|
|
26
|
-
const
|
|
27
|
-
const replacement = isEmptyCheck ? `_.isEmpty(${
|
|
37
|
+
function reportBinary(node, lengthNode, operator, value, isEmptyCheck) {
|
|
38
|
+
const collection = sourceCode.getText(lengthNode.object);
|
|
39
|
+
const replacement = isEmptyCheck ? `_.isEmpty(${collection})` : `!_.isEmpty(${collection})`;
|
|
28
40
|
context.report({
|
|
29
41
|
node,
|
|
30
42
|
messageId: 'useIsEmpty',
|
|
31
|
-
data: {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
43
|
+
data: { collection, operator, value },
|
|
44
|
+
fix(fixer) {
|
|
45
|
+
const fixes = [fixer.replaceText(node, replacement)];
|
|
46
|
+
const importFix = ensureLodashImport(fixer);
|
|
47
|
+
if (importFix)
|
|
48
|
+
fixes.push(importFix);
|
|
49
|
+
return fixes;
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
function reportUnary(node, lengthNode) {
|
|
54
|
+
const collection = sourceCode.getText(lengthNode.object);
|
|
55
|
+
context.report({
|
|
56
|
+
node,
|
|
57
|
+
messageId: 'useIsEmptyUnary',
|
|
58
|
+
data: { collection },
|
|
59
|
+
fix(fixer) {
|
|
60
|
+
const fixes = [fixer.replaceText(node, `_.isEmpty(${collection})`)];
|
|
61
|
+
const importFix = ensureLodashImport(fixer);
|
|
62
|
+
if (importFix)
|
|
63
|
+
fixes.push(importFix);
|
|
64
|
+
return fixes;
|
|
35
65
|
},
|
|
36
|
-
suggest: [
|
|
37
|
-
{
|
|
38
|
-
messageId: 'useIsEmpty',
|
|
39
|
-
data: {
|
|
40
|
-
collection: collectionText,
|
|
41
|
-
operator,
|
|
42
|
-
value,
|
|
43
|
-
},
|
|
44
|
-
fix(fixer) {
|
|
45
|
-
return fixer.replaceText(node, replacement);
|
|
46
|
-
},
|
|
47
|
-
},
|
|
48
|
-
],
|
|
49
66
|
});
|
|
50
67
|
}
|
|
51
68
|
return {
|
|
52
69
|
BinaryExpression(node) {
|
|
53
70
|
if (isLengthAccess(node.left) && isNumericLiteral(node.right)) {
|
|
54
71
|
if ((node.operator === '===' && node.right.value === 0) || (node.operator === '<=' && node.right.value === 0) || (node.operator === '<' && node.right.value === 1)) {
|
|
55
|
-
|
|
72
|
+
reportBinary(node, node.left, node.operator, node.right.value, true);
|
|
56
73
|
return;
|
|
57
74
|
}
|
|
58
75
|
if ((node.operator === '>' && node.right.value === 0) ||
|
|
59
76
|
(node.operator === '>=' && node.right.value === 1) ||
|
|
60
77
|
((node.operator === '!=' || node.operator === '!==') && node.right.value === 0)) {
|
|
61
|
-
|
|
78
|
+
reportBinary(node, node.left, node.operator, node.right.value, false);
|
|
62
79
|
}
|
|
63
80
|
}
|
|
64
81
|
if (isNumericLiteral(node.left) && isLengthAccess(node.right)) {
|
|
65
82
|
if ((node.operator === '===' && node.left.value === 0) || (node.operator === '>=' && node.left.value === 0) || (node.operator === '>' && node.left.value === 0)) {
|
|
66
|
-
|
|
83
|
+
reportBinary(node, node.right, node.operator, node.left.value, true);
|
|
67
84
|
return;
|
|
68
85
|
}
|
|
69
86
|
if ((node.operator === '<' && node.left.value === 1) || (node.operator === '<=' && node.left.value === 0)) {
|
|
70
|
-
|
|
87
|
+
reportBinary(node, node.right, node.operator, node.left.value, false);
|
|
71
88
|
}
|
|
72
89
|
}
|
|
73
90
|
},
|
|
91
|
+
UnaryExpression(node) {
|
|
92
|
+
if (node.operator !== '!')
|
|
93
|
+
return;
|
|
94
|
+
if (isLengthAccess(node.argument)) {
|
|
95
|
+
reportUnary(node, node.argument);
|
|
96
|
+
}
|
|
97
|
+
},
|
|
74
98
|
};
|
|
75
99
|
},
|
|
76
100
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types-in-dts.d.ts","sourceRoot":"","sources":["../../src/rules/types-in-dts.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"types-in-dts.d.ts","sourceRoot":"","sources":["../../src/rules/types-in-dts.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAiB,MAAM,0BAA0B,CAAC;AAEtE,QAAA,MAAM,UAAU;;;;;CAkFd,CAAC;AACH,eAAe,UAAU,CAAC"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable new-cap */
|
|
1
2
|
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
2
3
|
const typesInDts = ESLintUtils.RuleCreator(() => 'https://github.com/tomerh2001/eslint-plugin-th-rules/blob/main/docs/rules/types-in-dts.md')({
|
|
3
4
|
name: 'types-in-dts',
|
|
@@ -27,8 +28,6 @@ const typesInDts = ESLintUtils.RuleCreator(() => 'https://github.com/tomerh2001/
|
|
|
27
28
|
},
|
|
28
29
|
],
|
|
29
30
|
create(context, [options]) {
|
|
30
|
-
const allowEnums = Boolean(options.allowEnums);
|
|
31
|
-
const allowDeclare = Boolean(options.allowDeclare);
|
|
32
31
|
function isDtsFile(filename) {
|
|
33
32
|
if (!filename || filename === '<input>') {
|
|
34
33
|
return false;
|
|
@@ -43,11 +42,10 @@ const typesInDts = ESLintUtils.RuleCreator(() => 'https://github.com/tomerh2001/
|
|
|
43
42
|
return modifiers.some((m) => m?.type === 'TSDeclareKeyword');
|
|
44
43
|
}
|
|
45
44
|
function reportIfNotDts(node) {
|
|
46
|
-
|
|
47
|
-
if (isDtsFile(filename)) {
|
|
45
|
+
if (isDtsFile(context.filename)) {
|
|
48
46
|
return;
|
|
49
47
|
}
|
|
50
|
-
if (allowDeclare && hasDeclareModifier(node)) {
|
|
48
|
+
if (options.allowDeclare && hasDeclareModifier(node)) {
|
|
51
49
|
return;
|
|
52
50
|
}
|
|
53
51
|
context.report({
|
|
@@ -63,7 +61,7 @@ const typesInDts = ESLintUtils.RuleCreator(() => 'https://github.com/tomerh2001/
|
|
|
63
61
|
reportIfNotDts(node);
|
|
64
62
|
},
|
|
65
63
|
TSEnumDeclaration(node) {
|
|
66
|
-
if (allowEnums) {
|
|
64
|
+
if (options.allowEnums) {
|
|
67
65
|
return;
|
|
68
66
|
}
|
|
69
67
|
reportIfNotDts(node);
|