-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
no-unnecessary-condition.test.ts
271 lines (258 loc) · 6.31 KB
/
no-unnecessary-condition.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import path from 'path';
import rule, {
Options,
MessageId,
} from '../../src/rules/no-unnecessary-condition';
import { RuleTester } from '../RuleTester';
import {
TestCaseError,
InvalidTestCase,
} from '@typescript-eslint/experimental-utils/dist/ts-eslint';
const rootPath = path.join(process.cwd(), 'tests/fixtures/');
const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: rootPath,
project: './tsconfig.json',
},
});
const ruleError = (
line: number,
column: number,
messageId: MessageId,
): TestCaseError<MessageId> => ({
messageId,
line,
column,
});
const necessaryConditionTest = (condition: string): string => `
declare const b1: ${condition};
declare const b2: boolean;
const t1 = b1 && b2;
`;
const unnecessaryConditionTest = (
condition: string,
messageId: MessageId,
): InvalidTestCase<MessageId, Options> => ({
code: necessaryConditionTest(condition),
errors: [ruleError(4, 12, messageId)],
});
ruleTester.run('no-unnecessary-conditionals', rule, {
valid: [
`
declare const b1: boolean;
declare const b2: boolean;
const t1 = b1 && b2;
const t2 = b1 || b2;
if(b1 && b2) {}
while(b1 && b2) {}
for (let i = 0; (b1 && b2); i++) { break; }
const t1 = (b1 && b2) ? 'yes' : 'no'`,
necessaryConditionTest('false | 5'), // Truthy literal and falsy literal
necessaryConditionTest('boolean | "foo"'), // boolean and truthy literal
necessaryConditionTest('0 | boolean'), // boolean and falsy literal
necessaryConditionTest('boolean | object'), // boolean and always-truthy type
necessaryConditionTest('false | object'), // always truthy type and falsy literal
// always falsy type and always truthy type
necessaryConditionTest('null | object'),
necessaryConditionTest('undefined | true'),
necessaryConditionTest('void | true'),
necessaryConditionTest('any'), // any
necessaryConditionTest('unknown'), // unknown
// Generic type params
`
function test<T extends string>(t: T) {
return t ? 'yes' : 'no'
}`,
`
// Naked type param
function test<T>(t: T) {
return t ? 'yes' : 'no'
}`,
`
// Naked type param in union
function test<T>(t: T | []) {
return t ? 'yes' : 'no'
}`,
// Boolean expressions
`
function test(a: string) {
return a === "a"
}`,
// Nullish coalescing operator
`
function test(a: string | null) {
return a ?? "default";
}`,
`
function test(a: string | undefined) {
return a ?? "default";
}`,
`
function test(a: string | null | undefined) {
return a ?? "default";
}`,
`
function test(a: unknown) {
return a ?? "default";
}`,
// Supports ignoring the RHS
{
code: `
declare const b1: boolean;
declare const b2: true;
if(b1 && b2) {}`,
options: [{ ignoreRhs: true }],
},
{
code: `
while(true) {}
for (;true;) {}
do {} while(true)
`,
options: [{ allowConstantLoopConditions: true }],
},
],
invalid: [
// Ensure that it's checking in all the right places
{
code: `
const b1 = true;
declare const b2: boolean;
const t1 = b1 && b2;
const t2 = b1 || b2;
if(b1 && b2) {}
while(b1 && b2) {}
for (let i = 0; (b1 && b2); i++) { break; }
const t1 = (b1 && b2) ? 'yes' : 'no'`,
errors: [
ruleError(4, 12, 'alwaysTruthy'),
ruleError(5, 12, 'alwaysTruthy'),
ruleError(6, 4, 'alwaysTruthy'),
ruleError(7, 7, 'alwaysTruthy'),
ruleError(8, 18, 'alwaysTruthy'),
ruleError(9, 13, 'alwaysTruthy'),
],
},
// Ensure that it's complaining about the right things
unnecessaryConditionTest('object', 'alwaysTruthy'),
unnecessaryConditionTest('object | true', 'alwaysTruthy'),
unnecessaryConditionTest('"" | false', 'alwaysFalsy'), // Two falsy literals
unnecessaryConditionTest('"always truthy"', 'alwaysTruthy'),
unnecessaryConditionTest(`undefined`, 'alwaysFalsy'),
unnecessaryConditionTest('null', 'alwaysFalsy'),
unnecessaryConditionTest('void', 'alwaysFalsy'),
unnecessaryConditionTest('never', 'never'),
// Generic type params
{
code: `
function test<T extends object>(t: T) {
return t ? 'yes' : 'no'
}`,
errors: [ruleError(3, 10, 'alwaysTruthy')],
},
{
code: `
function test<T extends false>(t: T) {
return t ? 'yes' : 'no'
}`,
errors: [ruleError(3, 10, 'alwaysFalsy')],
},
{
code: `
function test<T extends 'a' | 'b'>(t: T) {
return t ? 'yes' : 'no'
}`,
errors: [ruleError(3, 10, 'alwaysTruthy')],
},
// Boolean expressions
{
code: `
function test(a: "a") {
return a === "a"
}`,
errors: [ruleError(3, 10, 'literalBooleanExpression')],
},
{
code: `
const y = 1;
if (y === 0) {}
`,
errors: [ruleError(3, 5, 'literalBooleanExpression')],
},
{
code: `
enum Foo {
a = 1,
b = 2
}
const x = Foo.a;
if (x === Foo.a) {}
`,
errors: [ruleError(8, 5, 'literalBooleanExpression')],
},
// Nullish coalescing operator
{
code: `
function test(a: string) {
return a ?? 'default';
}`,
errors: [ruleError(3, 10, 'neverNullish')],
},
{
code: `
function test(a: string | false) {
return a ?? 'default';
}`,
errors: [ruleError(3, 10, 'neverNullish')],
},
{
code: `
function test(a: null) {
return a ?? 'default';
}`,
errors: [ruleError(3, 10, 'alwaysNullish')],
},
{
code: `
function test(a: never) {
return a ?? 'default';
}`,
errors: [ruleError(3, 10, 'never')],
},
// Still errors on in the expected locations when ignoring RHS
{
options: [{ ignoreRhs: true }],
code: `
const b1 = true;
const b2 = false;
const t1 = b1 && b2;
const t2 = b1 || b2;
if(b1 && b2) {}
while(b1 && b2) {}
for (let i = 0; (b1 && b2); i++) { break; }
const t1 = (b1 && b2) ? 'yes' : 'no'`,
errors: [
ruleError(4, 12, 'alwaysTruthy'),
ruleError(5, 12, 'alwaysTruthy'),
ruleError(6, 4, 'alwaysTruthy'),
ruleError(7, 7, 'alwaysTruthy'),
ruleError(8, 18, 'alwaysTruthy'),
ruleError(9, 13, 'alwaysTruthy'),
],
},
{
code: `
while(true) {}
for (;true;) {}
do {} while(true)
`,
options: [{ allowConstantLoopConditions: false }],
errors: [
ruleError(2, 7, 'alwaysTruthy'),
ruleError(3, 7, 'alwaysTruthy'),
ruleError(4, 13, 'alwaysTruthy'),
],
},
],
});