eslint-plugin-ts-data-forge 0.1.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/LICENSE +201 -0
- package/README.md +57 -0
- package/package.json +74 -0
- package/src/index.mts +7 -0
- package/src/plugin.mts +9 -0
- package/src/rule-types.mts +326 -0
- package/src/rules/ast-utils.mts +169 -0
- package/src/rules/branded-number-types.mts +47 -0
- package/src/rules/constants.mts +1 -0
- package/src/rules/import-utils.mts +152 -0
- package/src/rules/index.mts +1 -0
- package/src/rules/no-unnecessary-type-guard.mts +515 -0
- package/src/rules/no-unnecessary-type-guard.test.mts +368 -0
- package/src/rules/prefer-arr-is-array.mts +98 -0
- package/src/rules/prefer-arr-is-array.test.mts +62 -0
- package/src/rules/prefer-arr-is-bounded-length-array.mts +143 -0
- package/src/rules/prefer-arr-is-bounded-length-array.test.mts +154 -0
- package/src/rules/prefer-arr-is-fixed-length-array.mts +149 -0
- package/src/rules/prefer-arr-is-fixed-length-array.test.mts +214 -0
- package/src/rules/prefer-arr-is-max-length-array.mts +141 -0
- package/src/rules/prefer-arr-is-max-length-array.test.mts +188 -0
- package/src/rules/prefer-arr-is-min-length-array.mts +141 -0
- package/src/rules/prefer-arr-is-min-length-array.test.mts +202 -0
- package/src/rules/prefer-arr-is-non-empty.mts +131 -0
- package/src/rules/prefer-arr-is-non-empty.test.mts +90 -0
- package/src/rules/prefer-arr-sum.mts +269 -0
- package/src/rules/prefer-arr-sum.test.mts +171 -0
- package/src/rules/prefer-as-int.mts +131 -0
- package/src/rules/prefer-as-int.test.mts +267 -0
- package/src/rules/prefer-canonical-array-slicing.mts +800 -0
- package/src/rules/prefer-canonical-array-slicing.test.mts +588 -0
- package/src/rules/prefer-comparison-over-nullish-guard.mts +141 -0
- package/src/rules/prefer-comparison-over-nullish-guard.test.mts +171 -0
- package/src/rules/prefer-is-non-null-object.mts +145 -0
- package/src/rules/prefer-is-non-null-object.test.mts +156 -0
- package/src/rules/prefer-is-record-and-has-key.mts +156 -0
- package/src/rules/prefer-is-record-and-has-key.test.mts +217 -0
- package/src/rules/prefer-num-safe-parse-float.mts +166 -0
- package/src/rules/prefer-num-safe-parse-float.test.mts +163 -0
- package/src/rules/prefer-num-safe-parse-int.mts +166 -0
- package/src/rules/prefer-num-safe-parse-int.test.mts +137 -0
- package/src/rules/prefer-range-for-loop.mts +179 -0
- package/src/rules/prefer-range-for-loop.test.mts +156 -0
- package/src/rules/rules.mts +36 -0
- package/src/types.mts +4 -0
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import parser from '@typescript-eslint/parser';
|
|
2
|
+
import { RuleTester } from '@typescript-eslint/rule-tester';
|
|
3
|
+
import dedent from 'dedent';
|
|
4
|
+
import { preferArrIsMaxLengthArray } from './prefer-arr-is-max-length-array.mjs';
|
|
5
|
+
|
|
6
|
+
const tester = new RuleTester({
|
|
7
|
+
languageOptions: {
|
|
8
|
+
parser,
|
|
9
|
+
parserOptions: {
|
|
10
|
+
ecmaVersion: 2020,
|
|
11
|
+
sourceType: 'module',
|
|
12
|
+
projectService: {
|
|
13
|
+
allowDefaultProject: ['*.ts*'],
|
|
14
|
+
},
|
|
15
|
+
tsconfigRootDir: `${import.meta.dirname}/../..`,
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe('prefer-arr-is-max-length-array', () => {
|
|
21
|
+
tester.run('prefer-arr-is-max-length-array', preferArrIsMaxLengthArray, {
|
|
22
|
+
valid: [
|
|
23
|
+
{
|
|
24
|
+
name: 'ignores non-array types',
|
|
25
|
+
code: dedent`
|
|
26
|
+
const str = "hello";
|
|
27
|
+
const ok = str.length <= 5;
|
|
28
|
+
`,
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: 'ignores > comparisons',
|
|
32
|
+
code: dedent`
|
|
33
|
+
const xs = [1, 2, 3];
|
|
34
|
+
const ok = xs.length > 0;
|
|
35
|
+
`,
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
name: 'ignores < comparisons',
|
|
39
|
+
code: dedent`
|
|
40
|
+
const xs = [1, 2, 3];
|
|
41
|
+
const ok = xs.length < 5;
|
|
42
|
+
`,
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
name: 'ignores >= comparisons (that is a min bound)',
|
|
46
|
+
code: dedent`
|
|
47
|
+
const xs = [1, 2, 3];
|
|
48
|
+
const ok = xs.length >= 3;
|
|
49
|
+
`,
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
name: 'ignores === comparisons',
|
|
53
|
+
code: dedent`
|
|
54
|
+
const xs = [1, 2, 3];
|
|
55
|
+
const ok = xs.length === 3;
|
|
56
|
+
`,
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: 'ignores comparison with non-const variable',
|
|
60
|
+
code: dedent`
|
|
61
|
+
const xs = [1, 2, 3];
|
|
62
|
+
let n = 3;
|
|
63
|
+
const ok = xs.length <= n;
|
|
64
|
+
`,
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
name: 'ignores comparison with const variable of type number',
|
|
68
|
+
code: dedent`
|
|
69
|
+
const xs = [1, 2, 3];
|
|
70
|
+
const n: number = 3;
|
|
71
|
+
const ok = xs.length <= n;
|
|
72
|
+
`,
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
name: 'defers bounded range to the bounded rule',
|
|
76
|
+
code: dedent`
|
|
77
|
+
const xs: readonly number[] = [1, 2, 3];
|
|
78
|
+
const ok = xs.length >= 1 && xs.length <= 5;
|
|
79
|
+
`,
|
|
80
|
+
},
|
|
81
|
+
],
|
|
82
|
+
invalid: [
|
|
83
|
+
{
|
|
84
|
+
name: 'replaces xs.length <= n with Arr.isMaxLengthArray',
|
|
85
|
+
code: dedent`
|
|
86
|
+
const xs: readonly number[] = [1, 2, 3];
|
|
87
|
+
const ok = xs.length <= 3;
|
|
88
|
+
`,
|
|
89
|
+
output: dedent`
|
|
90
|
+
import { Arr } from 'ts-data-forge';
|
|
91
|
+
const xs: readonly number[] = [1, 2, 3];
|
|
92
|
+
const ok = Arr.isMaxLengthArray(xs, 3);
|
|
93
|
+
`,
|
|
94
|
+
errors: [{ messageId: 'useIsMaxLengthArray' }],
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
name: 'replaces n >= xs.length with Arr.isMaxLengthArray',
|
|
98
|
+
code: dedent`
|
|
99
|
+
const xs: readonly number[] = [1, 2, 3];
|
|
100
|
+
const ok = 3 >= xs.length;
|
|
101
|
+
`,
|
|
102
|
+
output: dedent`
|
|
103
|
+
import { Arr } from 'ts-data-forge';
|
|
104
|
+
const xs: readonly number[] = [1, 2, 3];
|
|
105
|
+
const ok = Arr.isMaxLengthArray(xs, 3);
|
|
106
|
+
`,
|
|
107
|
+
errors: [{ messageId: 'useIsMaxLengthArray' }],
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
name: 'works with no type annotation',
|
|
111
|
+
code: dedent`
|
|
112
|
+
const xs = [1, 2, 3];
|
|
113
|
+
const ok = xs.length <= 1;
|
|
114
|
+
`,
|
|
115
|
+
output: dedent`
|
|
116
|
+
import { Arr } from 'ts-data-forge';
|
|
117
|
+
const xs = [1, 2, 3];
|
|
118
|
+
const ok = Arr.isMaxLengthArray(xs, 1);
|
|
119
|
+
`,
|
|
120
|
+
errors: [{ messageId: 'useIsMaxLengthArray' }],
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
name: 'works with const assertion',
|
|
124
|
+
code: dedent`
|
|
125
|
+
const xs = [1, 2, 3] as const;
|
|
126
|
+
const ok = xs.length <= 2;
|
|
127
|
+
`,
|
|
128
|
+
output: dedent`
|
|
129
|
+
import { Arr } from 'ts-data-forge';
|
|
130
|
+
const xs = [1, 2, 3] as const;
|
|
131
|
+
const ok = Arr.isMaxLengthArray(xs, 2);
|
|
132
|
+
`,
|
|
133
|
+
errors: [{ messageId: 'useIsMaxLengthArray' }],
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
name: 'works with variable length',
|
|
137
|
+
code: dedent`
|
|
138
|
+
const xs = [1, 2, 3];
|
|
139
|
+
const n = 2;
|
|
140
|
+
const ok = xs.length <= n;
|
|
141
|
+
`,
|
|
142
|
+
output: dedent`
|
|
143
|
+
import { Arr } from 'ts-data-forge';
|
|
144
|
+
const xs = [1, 2, 3];
|
|
145
|
+
const n = 2;
|
|
146
|
+
const ok = Arr.isMaxLengthArray(xs, n);
|
|
147
|
+
`,
|
|
148
|
+
errors: [{ messageId: 'useIsMaxLengthArray' }],
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
name: 'keeps existing Arr import',
|
|
152
|
+
code: dedent`
|
|
153
|
+
import { Arr } from 'ts-data-forge';
|
|
154
|
+
|
|
155
|
+
const xs = [1, 2, 3];
|
|
156
|
+
const ok = xs.length <= 1;
|
|
157
|
+
`,
|
|
158
|
+
output: dedent`
|
|
159
|
+
import { Arr } from 'ts-data-forge';
|
|
160
|
+
|
|
161
|
+
const xs = [1, 2, 3];
|
|
162
|
+
const ok = Arr.isMaxLengthArray(xs, 1);
|
|
163
|
+
`,
|
|
164
|
+
errors: [{ messageId: 'useIsMaxLengthArray' }],
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
name: 'replaces multiple checks',
|
|
168
|
+
code: dedent`
|
|
169
|
+
const xs = [1, 2, 3];
|
|
170
|
+
const ys = [4, 5];
|
|
171
|
+
const ok1 = xs.length <= 2;
|
|
172
|
+
const ok2 = ys.length <= 1;
|
|
173
|
+
`,
|
|
174
|
+
output: dedent`
|
|
175
|
+
import { Arr } from 'ts-data-forge';
|
|
176
|
+
const xs = [1, 2, 3];
|
|
177
|
+
const ys = [4, 5];
|
|
178
|
+
const ok1 = Arr.isMaxLengthArray(xs, 2);
|
|
179
|
+
const ok2 = Arr.isMaxLengthArray(ys, 1);
|
|
180
|
+
`,
|
|
181
|
+
errors: [
|
|
182
|
+
{ messageId: 'useIsMaxLengthArray' },
|
|
183
|
+
{ messageId: 'useIsMaxLengthArray' },
|
|
184
|
+
],
|
|
185
|
+
},
|
|
186
|
+
],
|
|
187
|
+
});
|
|
188
|
+
}, 20000);
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { type TSESLint, type TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
import {
|
|
3
|
+
isIntegerLiteralOrConstant,
|
|
4
|
+
isLengthAccess,
|
|
5
|
+
isPartOfBoundedLengthCheck,
|
|
6
|
+
} from './ast-utils.mjs';
|
|
7
|
+
import {
|
|
8
|
+
buildImportFixes,
|
|
9
|
+
getNamedImports,
|
|
10
|
+
getTsDataForgeImport,
|
|
11
|
+
} from './import-utils.mjs';
|
|
12
|
+
|
|
13
|
+
type Options = readonly [];
|
|
14
|
+
|
|
15
|
+
type MessageIds = 'useIsMinLengthArray';
|
|
16
|
+
|
|
17
|
+
export const preferArrIsMinLengthArray: TSESLint.RuleModule<
|
|
18
|
+
MessageIds,
|
|
19
|
+
Options
|
|
20
|
+
> = {
|
|
21
|
+
meta: {
|
|
22
|
+
type: 'suggestion',
|
|
23
|
+
docs: {
|
|
24
|
+
description:
|
|
25
|
+
'Replace `xs.length >= n` with `Arr.isMinLengthArray(xs, n)` from ts-data-forge.',
|
|
26
|
+
},
|
|
27
|
+
fixable: 'code',
|
|
28
|
+
schema: [],
|
|
29
|
+
messages: {
|
|
30
|
+
useIsMinLengthArray:
|
|
31
|
+
'Replace `{{original}}` with `Arr.isMinLengthArray({{arrayName}}, {{length}})` from ts-data-forge.',
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
create: (context) => {
|
|
36
|
+
const sourceCode = context.sourceCode;
|
|
37
|
+
|
|
38
|
+
const program = sourceCode.ast;
|
|
39
|
+
|
|
40
|
+
const tsDataForgeImport = getTsDataForgeImport(program);
|
|
41
|
+
|
|
42
|
+
const services = context.sourceCode.parserServices;
|
|
43
|
+
|
|
44
|
+
const mut_nodesToFix: {
|
|
45
|
+
node: TSESTree.BinaryExpression;
|
|
46
|
+
arrayExpression: TSESTree.Expression;
|
|
47
|
+
lengthExpression: TSESTree.Expression;
|
|
48
|
+
}[] = [];
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
BinaryExpression: (node) => {
|
|
52
|
+
// Check for `xs.length >= n` or `n <= xs.length`
|
|
53
|
+
if (node.operator !== '>=' && node.operator !== '<=') return;
|
|
54
|
+
|
|
55
|
+
// Defer to `prefer-arr-is-bounded-length-array` when this comparison is
|
|
56
|
+
// the lower bound of a `min && max` pair on the same array.
|
|
57
|
+
if (isPartOfBoundedLengthCheck(node, sourceCode)) return;
|
|
58
|
+
|
|
59
|
+
// xs.length >= n or n <= xs.length
|
|
60
|
+
const isLengthOnLeft = node.operator === '>=';
|
|
61
|
+
|
|
62
|
+
const lengthSide = node[isLengthOnLeft ? 'left' : 'right'];
|
|
63
|
+
|
|
64
|
+
const valueSide = node[isLengthOnLeft ? 'right' : 'left'];
|
|
65
|
+
|
|
66
|
+
// Check if lengthSide is accessing .length
|
|
67
|
+
if (!isLengthAccess(lengthSide)) return;
|
|
68
|
+
|
|
69
|
+
// Only match integer literals or const variables initialized with integer literals
|
|
70
|
+
if (!isIntegerLiteralOrConstant(valueSide, sourceCode)) return;
|
|
71
|
+
|
|
72
|
+
// lengthSide is MemberExpression accessing .length
|
|
73
|
+
const arrayExpression = lengthSide.object;
|
|
74
|
+
|
|
75
|
+
// Check if arrayExpression is actually an array type
|
|
76
|
+
if (services?.program !== undefined && services.program !== null) {
|
|
77
|
+
const checker = services.program.getTypeChecker();
|
|
78
|
+
|
|
79
|
+
const tsNode = services.esTreeNodeToTSNodeMap?.get(arrayExpression);
|
|
80
|
+
|
|
81
|
+
if (tsNode !== undefined) {
|
|
82
|
+
const type = checker.getTypeAtLocation(tsNode);
|
|
83
|
+
|
|
84
|
+
// Check if it's an array type or tuple type
|
|
85
|
+
const isArrayType =
|
|
86
|
+
checker.isArrayType(type) || checker.isTupleType(type);
|
|
87
|
+
|
|
88
|
+
if (!isArrayType) return;
|
|
89
|
+
} else {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
} else {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
mut_nodesToFix.push({
|
|
97
|
+
node,
|
|
98
|
+
arrayExpression,
|
|
99
|
+
lengthExpression: valueSide,
|
|
100
|
+
});
|
|
101
|
+
},
|
|
102
|
+
'Program:exit': () => {
|
|
103
|
+
const namedImports = getNamedImports(tsDataForgeImport);
|
|
104
|
+
|
|
105
|
+
const hasArrImport = namedImports.includes('Arr');
|
|
106
|
+
|
|
107
|
+
for (const [
|
|
108
|
+
index,
|
|
109
|
+
{ node, arrayExpression, lengthExpression },
|
|
110
|
+
] of mut_nodesToFix.entries()) {
|
|
111
|
+
const arrayText = sourceCode.getText(arrayExpression);
|
|
112
|
+
|
|
113
|
+
const lengthText = sourceCode.getText(lengthExpression);
|
|
114
|
+
|
|
115
|
+
const originalText = sourceCode.getText(node);
|
|
116
|
+
|
|
117
|
+
context.report({
|
|
118
|
+
node,
|
|
119
|
+
messageId: 'useIsMinLengthArray',
|
|
120
|
+
data: {
|
|
121
|
+
original: originalText,
|
|
122
|
+
arrayName: arrayText,
|
|
123
|
+
length: lengthText,
|
|
124
|
+
},
|
|
125
|
+
fix: (fixer) => {
|
|
126
|
+
const replacement = `Arr.isMinLengthArray(${arrayText}, ${lengthText})`;
|
|
127
|
+
|
|
128
|
+
const importFixes =
|
|
129
|
+
index === 0 && !hasArrImport
|
|
130
|
+
? buildImportFixes(fixer, program, tsDataForgeImport, ['Arr'])
|
|
131
|
+
: [];
|
|
132
|
+
|
|
133
|
+
return [...importFixes, fixer.replaceText(node, replacement)];
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
},
|
|
140
|
+
defaultOptions: [],
|
|
141
|
+
} as const;
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import parser from '@typescript-eslint/parser';
|
|
2
|
+
import { RuleTester } from '@typescript-eslint/rule-tester';
|
|
3
|
+
import dedent from 'dedent';
|
|
4
|
+
import { preferArrIsMinLengthArray } from './prefer-arr-is-min-length-array.mjs';
|
|
5
|
+
|
|
6
|
+
const tester = new RuleTester({
|
|
7
|
+
languageOptions: {
|
|
8
|
+
parser,
|
|
9
|
+
parserOptions: {
|
|
10
|
+
ecmaVersion: 2020,
|
|
11
|
+
sourceType: 'module',
|
|
12
|
+
projectService: {
|
|
13
|
+
allowDefaultProject: ['*.ts*'],
|
|
14
|
+
},
|
|
15
|
+
tsconfigRootDir: `${import.meta.dirname}/../..`,
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe('prefer-arr-is-min-length-array', () => {
|
|
21
|
+
tester.run('prefer-arr-is-min-length-array', preferArrIsMinLengthArray, {
|
|
22
|
+
valid: [
|
|
23
|
+
{
|
|
24
|
+
name: 'ignores non-array types',
|
|
25
|
+
code: dedent`
|
|
26
|
+
const str = "hello";
|
|
27
|
+
const ok = str.length >= 5;
|
|
28
|
+
`,
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: 'ignores < comparisons',
|
|
32
|
+
code: dedent`
|
|
33
|
+
const xs = [1, 2, 3];
|
|
34
|
+
const ok = xs.length < 5;
|
|
35
|
+
`,
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
name: 'ignores > comparisons',
|
|
39
|
+
code: dedent`
|
|
40
|
+
const xs = [1, 2, 3];
|
|
41
|
+
const ok = xs.length > 0;
|
|
42
|
+
`,
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
name: 'ignores === comparisons',
|
|
46
|
+
code: dedent`
|
|
47
|
+
const xs = [1, 2, 3];
|
|
48
|
+
const ok = xs.length === 3;
|
|
49
|
+
`,
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
name: 'ignores comparison with non-const variable',
|
|
53
|
+
code: dedent`
|
|
54
|
+
const xs = [1, 2, 3];
|
|
55
|
+
let n = 3;
|
|
56
|
+
const ok = xs.length >= n;
|
|
57
|
+
`,
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
name: 'ignores comparison with function return value',
|
|
61
|
+
code: dedent`
|
|
62
|
+
const xs = [1, 2, 3];
|
|
63
|
+
const ok = xs.length >= Math.floor(3.5);
|
|
64
|
+
`,
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
name: 'ignores comparison with const variable initialized by non-literal',
|
|
68
|
+
code: dedent`
|
|
69
|
+
const xs = [1, 2, 3];
|
|
70
|
+
const n = Math.floor(3.5);
|
|
71
|
+
const ok = xs.length >= n;
|
|
72
|
+
`,
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
name: 'ignores comparison with const variable of type number',
|
|
76
|
+
code: dedent`
|
|
77
|
+
const xs = [1, 2, 3];
|
|
78
|
+
const n: number = 3;
|
|
79
|
+
const ok = xs.length >= n;
|
|
80
|
+
`,
|
|
81
|
+
},
|
|
82
|
+
],
|
|
83
|
+
invalid: [
|
|
84
|
+
{
|
|
85
|
+
name: 'replaces xs.length >= n with Arr.isMinLengthArray',
|
|
86
|
+
code: dedent`
|
|
87
|
+
const xs: readonly number[] = [1, 2, 3];
|
|
88
|
+
const ok = xs.length >= 3;
|
|
89
|
+
`,
|
|
90
|
+
output: dedent`
|
|
91
|
+
import { Arr } from 'ts-data-forge';
|
|
92
|
+
const xs: readonly number[] = [1, 2, 3];
|
|
93
|
+
const ok = Arr.isMinLengthArray(xs, 3);
|
|
94
|
+
`,
|
|
95
|
+
errors: [{ messageId: 'useIsMinLengthArray' }],
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
name: 'replaces n <= xs.length with Arr.isMinLengthArray',
|
|
99
|
+
code: dedent`
|
|
100
|
+
const xs: readonly number[] = [1, 2, 3];
|
|
101
|
+
const ok = 3 <= xs.length;
|
|
102
|
+
`,
|
|
103
|
+
output: dedent`
|
|
104
|
+
import { Arr } from 'ts-data-forge';
|
|
105
|
+
const xs: readonly number[] = [1, 2, 3];
|
|
106
|
+
const ok = Arr.isMinLengthArray(xs, 3);
|
|
107
|
+
`,
|
|
108
|
+
errors: [{ messageId: 'useIsMinLengthArray' }],
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
name: 'works with no type annotation',
|
|
112
|
+
code: dedent`
|
|
113
|
+
const xs = [1, 2, 3];
|
|
114
|
+
const ok = xs.length >= 1;
|
|
115
|
+
`,
|
|
116
|
+
output: dedent`
|
|
117
|
+
import { Arr } from 'ts-data-forge';
|
|
118
|
+
const xs = [1, 2, 3];
|
|
119
|
+
const ok = Arr.isMinLengthArray(xs, 1);
|
|
120
|
+
`,
|
|
121
|
+
errors: [{ messageId: 'useIsMinLengthArray' }],
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
name: 'works with const assertion',
|
|
125
|
+
code: dedent`
|
|
126
|
+
const xs = [1, 2, 3] as const;
|
|
127
|
+
const ok = xs.length >= 2;
|
|
128
|
+
`,
|
|
129
|
+
output: dedent`
|
|
130
|
+
import { Arr } from 'ts-data-forge';
|
|
131
|
+
const xs = [1, 2, 3] as const;
|
|
132
|
+
const ok = Arr.isMinLengthArray(xs, 2);
|
|
133
|
+
`,
|
|
134
|
+
errors: [{ messageId: 'useIsMinLengthArray' }],
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
name: 'works with variable length',
|
|
138
|
+
code: dedent`
|
|
139
|
+
const xs = [1, 2, 3];
|
|
140
|
+
const n = 2;
|
|
141
|
+
const ok = xs.length >= n;
|
|
142
|
+
`,
|
|
143
|
+
output: dedent`
|
|
144
|
+
import { Arr } from 'ts-data-forge';
|
|
145
|
+
const xs = [1, 2, 3];
|
|
146
|
+
const n = 2;
|
|
147
|
+
const ok = Arr.isMinLengthArray(xs, n);
|
|
148
|
+
`,
|
|
149
|
+
errors: [{ messageId: 'useIsMinLengthArray' }],
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
name: 'keeps existing Arr import',
|
|
153
|
+
code: dedent`
|
|
154
|
+
import { Arr } from 'ts-data-forge';
|
|
155
|
+
|
|
156
|
+
const xs = [1, 2, 3];
|
|
157
|
+
const ok = xs.length >= 1;
|
|
158
|
+
`,
|
|
159
|
+
output: dedent`
|
|
160
|
+
import { Arr } from 'ts-data-forge';
|
|
161
|
+
|
|
162
|
+
const xs = [1, 2, 3];
|
|
163
|
+
const ok = Arr.isMinLengthArray(xs, 1);
|
|
164
|
+
`,
|
|
165
|
+
errors: [{ messageId: 'useIsMinLengthArray' }],
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
name: 'replaces multiple checks',
|
|
169
|
+
code: dedent`
|
|
170
|
+
const xs = [1, 2, 3];
|
|
171
|
+
const ys = [4, 5];
|
|
172
|
+
const ok1 = xs.length >= 2;
|
|
173
|
+
const ok2 = ys.length >= 1;
|
|
174
|
+
`,
|
|
175
|
+
output: dedent`
|
|
176
|
+
import { Arr } from 'ts-data-forge';
|
|
177
|
+
const xs = [1, 2, 3];
|
|
178
|
+
const ys = [4, 5];
|
|
179
|
+
const ok1 = Arr.isMinLengthArray(xs, 2);
|
|
180
|
+
const ok2 = Arr.isMinLengthArray(ys, 1);
|
|
181
|
+
`,
|
|
182
|
+
errors: [
|
|
183
|
+
{ messageId: 'useIsMinLengthArray' },
|
|
184
|
+
{ messageId: 'useIsMinLengthArray' },
|
|
185
|
+
],
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
name: 'works with >= 0 (checking non-empty)',
|
|
189
|
+
code: dedent`
|
|
190
|
+
const xs = [1, 2, 3];
|
|
191
|
+
const ok = xs.length >= 1;
|
|
192
|
+
`,
|
|
193
|
+
output: dedent`
|
|
194
|
+
import { Arr } from 'ts-data-forge';
|
|
195
|
+
const xs = [1, 2, 3];
|
|
196
|
+
const ok = Arr.isMinLengthArray(xs, 1);
|
|
197
|
+
`,
|
|
198
|
+
errors: [{ messageId: 'useIsMinLengthArray' }],
|
|
199
|
+
},
|
|
200
|
+
],
|
|
201
|
+
});
|
|
202
|
+
}, 20000);
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AST_NODE_TYPES,
|
|
3
|
+
type TSESLint,
|
|
4
|
+
type TSESTree,
|
|
5
|
+
} from '@typescript-eslint/utils';
|
|
6
|
+
import {
|
|
7
|
+
buildImportFixes,
|
|
8
|
+
getNamedImports,
|
|
9
|
+
getTsDataForgeImport,
|
|
10
|
+
} from './import-utils.mjs';
|
|
11
|
+
|
|
12
|
+
type Options = readonly [];
|
|
13
|
+
|
|
14
|
+
type MessageIds = 'useIsNonEmpty';
|
|
15
|
+
|
|
16
|
+
export const preferArrIsNonEmpty: TSESLint.RuleModule<MessageIds, Options> = {
|
|
17
|
+
meta: {
|
|
18
|
+
type: 'suggestion',
|
|
19
|
+
docs: {
|
|
20
|
+
description:
|
|
21
|
+
'Replace `xs.length > 0` with `Arr.isNonEmpty(xs)` from ts-data-forge.',
|
|
22
|
+
},
|
|
23
|
+
fixable: 'code',
|
|
24
|
+
schema: [],
|
|
25
|
+
messages: {
|
|
26
|
+
useIsNonEmpty:
|
|
27
|
+
'Replace `{{original}}` with `Arr.isNonEmpty({{arrayName}})` from ts-data-forge.',
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
create: (context) => {
|
|
32
|
+
const sourceCode = context.sourceCode;
|
|
33
|
+
|
|
34
|
+
const program = sourceCode.ast;
|
|
35
|
+
|
|
36
|
+
const tsDataForgeImport = getTsDataForgeImport(program);
|
|
37
|
+
|
|
38
|
+
const services = context.sourceCode.parserServices;
|
|
39
|
+
|
|
40
|
+
const mut_nodesToFix: {
|
|
41
|
+
node: TSESTree.BinaryExpression;
|
|
42
|
+
arrayExpression: TSESTree.Expression;
|
|
43
|
+
}[] = [];
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
BinaryExpression: (node) => {
|
|
47
|
+
// Check for `xs.length > 0` or `0 < xs.length`
|
|
48
|
+
if (node.operator !== '>' && node.operator !== '<') return;
|
|
49
|
+
|
|
50
|
+
const isLengthOnLeft = node.operator === '>';
|
|
51
|
+
|
|
52
|
+
const lengthSide = node[isLengthOnLeft ? 'left' : 'right'];
|
|
53
|
+
|
|
54
|
+
const numberSide = node[isLengthOnLeft ? 'right' : 'left'];
|
|
55
|
+
|
|
56
|
+
// Check if one side is `.length` and the other is 0
|
|
57
|
+
if (
|
|
58
|
+
lengthSide.type !== AST_NODE_TYPES.MemberExpression ||
|
|
59
|
+
lengthSide.property.type !== AST_NODE_TYPES.Identifier ||
|
|
60
|
+
lengthSide.property.name !== 'length' ||
|
|
61
|
+
numberSide.type !== AST_NODE_TYPES.Literal ||
|
|
62
|
+
numberSide.value !== 0
|
|
63
|
+
) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const arrayExpression = lengthSide.object;
|
|
68
|
+
|
|
69
|
+
// Check if arrayExpression is actually an array type
|
|
70
|
+
if (services?.program !== undefined && services.program !== null) {
|
|
71
|
+
const checker = services.program.getTypeChecker();
|
|
72
|
+
|
|
73
|
+
const tsNode = services.esTreeNodeToTSNodeMap?.get(arrayExpression);
|
|
74
|
+
|
|
75
|
+
if (tsNode !== undefined) {
|
|
76
|
+
const type = checker.getTypeAtLocation(tsNode);
|
|
77
|
+
|
|
78
|
+
// Check if it's an array type or tuple type
|
|
79
|
+
const isArrayType =
|
|
80
|
+
checker.isArrayType(type) || checker.isTupleType(type);
|
|
81
|
+
|
|
82
|
+
if (!isArrayType) return;
|
|
83
|
+
} else {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
} else {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
mut_nodesToFix.push({
|
|
91
|
+
node,
|
|
92
|
+
arrayExpression,
|
|
93
|
+
});
|
|
94
|
+
},
|
|
95
|
+
'Program:exit': () => {
|
|
96
|
+
const namedImports = getNamedImports(tsDataForgeImport);
|
|
97
|
+
|
|
98
|
+
const hasArrImport = namedImports.includes('Arr');
|
|
99
|
+
|
|
100
|
+
for (const [
|
|
101
|
+
index,
|
|
102
|
+
{ node, arrayExpression },
|
|
103
|
+
] of mut_nodesToFix.entries()) {
|
|
104
|
+
const arrayText = sourceCode.getText(arrayExpression);
|
|
105
|
+
|
|
106
|
+
const originalText = sourceCode.getText(node);
|
|
107
|
+
|
|
108
|
+
context.report({
|
|
109
|
+
node,
|
|
110
|
+
messageId: 'useIsNonEmpty',
|
|
111
|
+
data: {
|
|
112
|
+
original: originalText,
|
|
113
|
+
arrayName: arrayText,
|
|
114
|
+
},
|
|
115
|
+
fix: (fixer) => {
|
|
116
|
+
const replacement = `Arr.isNonEmpty(${arrayText})`;
|
|
117
|
+
|
|
118
|
+
const importFixes =
|
|
119
|
+
index === 0 && !hasArrImport
|
|
120
|
+
? buildImportFixes(fixer, program, tsDataForgeImport, ['Arr'])
|
|
121
|
+
: [];
|
|
122
|
+
|
|
123
|
+
return [...importFixes, fixer.replaceText(node, replacement)];
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
},
|
|
130
|
+
defaultOptions: [],
|
|
131
|
+
} as const;
|