This repository has been archived by the owner on Feb 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathnoConstantConditionRuleTests.ts
225 lines (209 loc) · 5.63 KB
/
noConstantConditionRuleTests.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
/// <reference path='../../../typings/mocha/mocha.d.ts' />
import { makeTest } from './helper';
const rule = 'no-constant-condition';
const scripts = {
variables: [
'const a = "1"; const b = "2"; if (+a > +b) {}',
'const a = { value: true }; if (a.value) {}',
'if (foo === true) {}',
'if (!foo === true) {}',
'if (bar === false) {}',
'if (!bar === false) {}',
'if (baz) {}',
'if (!baz) {}',
'if (qux == true) {}',
'if (!(qux == true)) {}',
'if (true == x) {}',
'if (!(true == x)) {}',
'if (false === y) {}',
'if (!(false === y)) {}',
'if (y === x) {}',
'if (!(y === x)) {}',
'if (x > 0) {}',
'if (!(x > 0)) {}',
'if (100 > x) {}',
'if (!(100 > x)) {}',
'if (x === -y) {}',
'if (!(x === -y)) {}',
'if (len--)'
],
booleans: [
'if (true) {}',
'if (!true) {}',
'if (false) {}',
'if (!false) {}'
],
numbers: [
'if (0) {}',
'if (!0) {}',
'if (1) {}',
'if (!1) {}',
'if (100) {}',
'if (!100) {}',
'if (30.33) {}',
'if (!30.33) {}',
'if (-1) {}',
'if (!-1) {}',
'if (x = 1) {}',
'if (!(x = 1)) {}'
],
objects: [
'if ({}) {}',
'if (!{}) {}',
'if ({ foo: "bar" }) {}',
'if (!{ foo: "bar" }) {}'
],
arrays: [
'if ([]) {}',
'if (![]) {}',
'if ([1, 2, 3]) {}',
'if (![1, 2, 3]) {}'
],
binary: [
'if (true === true) {}',
'if (!(true === true)) {}',
'if (100 > -5) {}',
'if (!(100 > -5)) {}',
'if (false != true) {}',
'if (!(false != true)) {}',
'if (false !== true && true === true) {}',
'if (!(false !== true && true === true)) {}',
'if (!(false !== true) && true === true) {}',
'if (false !== true && !(true === true)) {}',
'if (!(false !== true) && !(true === true)) {}'
],
ternary: [
'let foo = true ? 1 : 0;',
'let foo = !true ? 1 : 0;',
'let bar = false ? "a" : "b";',
'let bar = !false ? "a" : "b";',
'let baz = 100 ? "x" : "z";',
'let baz = !100 ? "x" : "z";',
'let qux = true === true ? "p": "w";',
'let qux = !(true === true) ? "p": "w";'
],
whileVars: [
'while (y === x) {}',
'while (!(y === x)) {}',
'while (x > -5) {}',
'while (!(x > -5)) {}',
'while (100 > x) {}',
'while (!(100 > x)) {}',
'while (foo) {}',
'while (!foo) {}'
],
whileLiterals: [
'while (true) {}',
'while (!true) {}',
'while (false) {}',
'while (!false) {}',
'while (-5) {}',
'while (!-5) {}',
'while (1) {}',
'while (!1) {}',
'while ({}) {}',
'while (!{}) {}',
'while ([]) {}',
'while (![]) {}'
],
doWhileVars: [
'do {} while (y === x);',
'do {} while (!(y === x);',
'do {} while (x > -5);',
'do {} while (!(x > -5));',
'do {} while (100 > x);',
'do {} while (!(100 > x));',
'do {} while (foo);',
'do {} while (!foo);'
],
doWhileLiterals: [
'do {} while (true);',
'do {} while (!true);',
'do {} while (false);',
'do {} while (!false);',
'do {} while (-5);',
'do {} while (!-5);',
'do {} while (1);',
'do {} while (!1);',
'do {} while ({});',
'do {} while (!{});',
'do {} while ([]);',
'do {} while (![]);'
],
forVars: [
'for (;y === x;) {}',
'for (;(!y === x);) {}',
'for (;x > -5;) {}',
'for (;!(x > -5);) {}',
'for (;100 > x;) {}',
'for (;!(100 > x);) {}',
'for (;foo;) {}',
'for (;!foo;) {}'
],
forLiterals: [
'for (;true;) {}',
'for (;!true;) {}',
'for (;false;) {}',
'for (;!false;) {}',
'for (;-5;) {}',
'for (;!-5;) {}',
'for (;1;) {}',
'for (;!1;) {}',
'for (;{};) {}',
'for (;!{};) {}',
'for (;[];) {}',
'for (;![];) {}'
]
};
describe(rule, function test() {
// if-tests
it('should pass when using variables', function testVariables() {
makeTest(rule, scripts.variables, true);
});
it('should fail with literal booleans', function testBooleans() {
makeTest(rule, scripts.booleans, false);
});
it('should fail with literal numbers', function testNumbers() {
makeTest(rule, scripts.numbers, false);
});
it('should fail with literal objects', function testObjects() {
makeTest(rule, scripts.objects, false);
});
it('should fail with literal arrays', function testArrays() {
makeTest(rule, scripts.arrays, false);
});
it('should fail with literal on both sides of a binary expression', function testBinary() {
makeTest(rule, scripts.binary, false);
});
// ternary tests
it('should fail on ternary literals (booleans / numbers)', function testTernary() {
makeTest(rule, scripts.ternary, false);
});
// while-tests
it('should pass on while variables', function testWhileVariables() {
makeTest(rule, scripts.whileVars, true);
});
it('should fail on while literals', function testWhileLiterals() {
makeTest(rule, scripts.whileLiterals, false);
});
// do-while-tests
it('should pass on do-while variables', function testDoWhileVariables() {
makeTest(rule, scripts.doWhileVars, true);
});
it('should fail on do-while literals', function testDoWhileLiterals() {
makeTest(rule, scripts.doWhileLiterals, false);
});
// for-tests
it('should pass on for variables', function testForVariables() {
makeTest(rule, scripts.forVars, true);
});
it('should fail on for literals', function testForLiterals() {
makeTest(rule, scripts.forLiterals, false);
});
it('should pass for literals in loops when checkLoops is false', function testCheckLoopsFalse() {
const config = {
rules: { 'no-constant-condition': [true, { checkLoops: false }] }
};
makeTest(rule, scripts.forLiterals, true, config);
});
});