]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Tests for no-constant-condition rule. | |
3 | * @author Christian Schulz <http://rndm.de> | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
12 | const rule = require("../../../lib/rules/no-constant-condition"), | |
13 | { RuleTester } = require("../../../lib/rule-tester"); | |
14 | ||
15 | //------------------------------------------------------------------------------ | |
16 | // Tests | |
17 | //------------------------------------------------------------------------------ | |
18 | ||
456be15e | 19 | const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2021 } }); |
eb39fafa DC |
20 | |
21 | ruleTester.run("no-constant-condition", rule, { | |
22 | valid: [ | |
23 | "if(a);", | |
24 | "if(a == 0);", | |
25 | "if(a = f());", | |
456be15e TL |
26 | "if(a += 1);", |
27 | "if(a |= 1);", | |
28 | "if(a |= true);", | |
29 | "if(a |= false);", | |
30 | "if(a &= 1);", | |
31 | "if(a &= true);", | |
32 | "if(a &= false);", | |
33 | "if(a >>= 1);", | |
34 | "if(a >>= true);", | |
35 | "if(a >>= false);", | |
36 | "if(a >>>= 1);", | |
37 | "if(a ??= 1);", | |
38 | "if(a ??= true);", | |
39 | "if(a ??= false);", | |
40 | "if(a ||= b);", | |
41 | "if(a ||= false);", | |
42 | "if(a ||= 0);", | |
43 | "if(a ||= void 0);", | |
44 | "if(+(a ||= 1));", | |
45 | "if(f(a ||= true));", | |
46 | "if((a ||= 1) + 2);", | |
47 | "if(1 + (a ||= true));", | |
48 | "if(a ||= '' || false);", | |
49 | "if(a ||= void 0 || null);", | |
50 | "if((a ||= false) || b);", | |
51 | "if(a || (b ||= false));", | |
52 | "if((a ||= true) && b);", | |
53 | "if(a && (b ||= true));", | |
54 | "if(a &&= b);", | |
55 | "if(a &&= true);", | |
56 | "if(a &&= 1);", | |
57 | "if(a &&= 'foo');", | |
58 | "if((a &&= '') + false);", | |
59 | "if('' + (a &&= null));", | |
60 | "if(a &&= 1 && 2);", | |
61 | "if((a &&= true) && b);", | |
62 | "if(a && (b &&= true));", | |
63 | "if((a &&= false) || b);", | |
64 | "if(a || (b &&= false));", | |
65 | "if(a ||= b ||= false);", | |
66 | "if(a &&= b &&= true);", | |
67 | "if(a ||= b &&= false);", | |
68 | "if(a ||= b &&= true);", | |
69 | "if(a &&= b ||= false);", | |
70 | "if(a &&= b ||= true);", | |
eb39fafa DC |
71 | "if(1, a);", |
72 | "if ('every' in []);", | |
73 | "if (`\\\n${a}`) {}", | |
74 | "if (`${a}`);", | |
75 | "if (`${foo()}`);", | |
76 | "if (`${a === 'b' && b==='a'}`);", | |
77 | "if (`foo${a}` === 'fooa');", | |
78 | "if (tag`a`);", | |
79 | "if (tag`${a}`);", | |
456be15e TL |
80 | "if (+(a || true));", |
81 | "if (-(a || true));", | |
82 | "if (~(a || 1));", | |
83 | "if (+(a && 0) === +(b && 0));", | |
eb39fafa DC |
84 | "while(~!a);", |
85 | "while(a = b);", | |
86 | "while(`${a}`);", | |
87 | "for(;x < 10;);", | |
88 | "for(;;);", | |
89 | "for(;`${a}`;);", | |
90 | "do{ }while(x)", | |
91 | "q > 0 ? 1 : 2;", | |
92 | "`${a}` === a ? 1 : 2", | |
93 | "`foo${a}` === a ? 1 : 2", | |
94 | "tag`a` === a ? 1 : 2", | |
95 | "tag`${a}` === a ? 1 : 2", | |
96 | "while(x += 3) {}", | |
97 | "while(tag`a`) {}", | |
98 | "while(tag`${a}`) {}", | |
99 | "while(`\\\n${a}`) {}", | |
100 | ||
101 | // #5228, typeof conditions | |
102 | "if(typeof x === 'undefined'){}", | |
103 | "if(`${typeof x}` === 'undefined'){}", | |
104 | "if(a === 'str' && typeof b){}", | |
105 | "typeof a == typeof b", | |
106 | "typeof 'a' === 'string'|| typeof b === 'string'", | |
107 | "`${typeof 'a'}` === 'string'|| `${typeof b}` === 'string'", | |
108 | ||
109 | // #5726, void conditions | |
110 | "if (void a || a);", | |
111 | "if (a || void a);", | |
112 | ||
113 | // #5693 | |
114 | "if(xyz === 'str1' && abc==='str2'){}", | |
115 | "if(xyz === 'str1' || abc==='str2'){}", | |
116 | "if(xyz === 'str1' || abc==='str2' && pqr === 5){}", | |
117 | "if(typeof abc === 'string' && abc==='str2'){}", | |
118 | "if(false || abc==='str'){}", | |
119 | "if(true && abc==='str'){}", | |
120 | "if(typeof 'str' && abc==='str'){}", | |
121 | "if(abc==='str' || false || def ==='str'){}", | |
122 | "if(true && abc==='str' || def ==='str'){}", | |
123 | "if(true && typeof abc==='string'){}", | |
124 | ||
125 | // #11181, string literals | |
eb39fafa DC |
126 | "if('str1' && a){}", |
127 | "if(a && 'str'){}", | |
eb39fafa DC |
128 | |
129 | // #11306 | |
6f036462 | 130 | "if ((foo || true) === 'baz') {}", |
eb39fafa DC |
131 | "if ((foo || 'bar') === 'baz') {}", |
132 | "if ((foo || 'bar') !== 'baz') {}", | |
133 | "if ((foo || 'bar') == 'baz') {}", | |
134 | "if ((foo || 'bar') != 'baz') {}", | |
135 | "if ((foo || 233) > 666) {}", | |
136 | "if ((foo || 233) < 666) {}", | |
137 | "if ((foo || 233) >= 666) {}", | |
138 | "if ((foo || 233) <= 666) {}", | |
139 | "if ((key || 'k') in obj) {}", | |
140 | "if ((foo || {}) instanceof obj) {}", | |
6f036462 TL |
141 | "if ((foo || 'bar' || 'bar') === 'bar');", |
142 | { | |
143 | code: "if ((foo || 1n) === 'baz') {}", | |
144 | parserOptions: { ecmaVersion: 11 } | |
145 | }, | |
146 | { | |
147 | code: "if (a && 0n || b);", | |
148 | parserOptions: { ecmaVersion: 11 } | |
149 | }, | |
150 | { | |
151 | code: "if(1n && a){};", | |
152 | parserOptions: { ecmaVersion: 11 } | |
153 | }, | |
eb39fafa DC |
154 | |
155 | // #12225 | |
156 | "if ('' + [y] === '' + [ty]) {}", | |
157 | "if ('a' === '' + [ty]) {}", | |
158 | "if ('' + [y, m, d] === 'a') {}", | |
159 | "if ('' + [y, 'm'] === '' + [ty, 'tm']) {}", | |
160 | "if ('' + [y, 'm'] === '' + ['ty']) {}", | |
161 | "if ([,] in\n\n($2))\n ;\nelse\n ;", | |
162 | "if ([...x]+'' === 'y'){}", | |
163 | ||
164 | // { checkLoops: false } | |
165 | { code: "while(true);", options: [{ checkLoops: false }] }, | |
166 | { code: "for(;true;);", options: [{ checkLoops: false }] }, | |
167 | { code: "do{}while(true)", options: [{ checkLoops: false }] }, | |
168 | ||
169 | "function* foo(){while(true){yield 'foo';}}", | |
170 | "function* foo(){for(;true;){yield 'foo';}}", | |
171 | "function* foo(){do{yield 'foo';}while(true)}", | |
172 | "function* foo(){while (true) { while(true) {yield;}}}", | |
173 | "function* foo() {for (; yield; ) {}}", | |
174 | "function* foo() {for (; ; yield) {}}", | |
175 | "function* foo() {while (true) {function* foo() {yield;}yield;}}", | |
176 | "function* foo() { for (let x = yield; x < 10; x++) {yield;}yield;}", | |
34eeec05 | 177 | "function* foo() { for (let x = yield; ; x++) { yield; }}", |
8f9d1d4d DC |
178 | "if (new Number(x) + 1 === 2) {}", |
179 | ||
180 | // #15467 | |
181 | "if([a]==[b]) {}", | |
182 | "if (+[...a]) {}", | |
183 | "if (+[...[...a]]) {}", | |
184 | "if (`${[...a]}`) {}", | |
185 | "if (`${[a]}`) {}", | |
186 | "if (+[a]) {}", | |
187 | "if (0 - [a]) {}", | |
188 | "if (1 * [a]) {}", | |
189 | ||
190 | // Boolean function | |
191 | "if (Boolean(a)) {}", | |
192 | "if (Boolean(...args)) {}", | |
193 | "if (foo.Boolean(1)) {}", | |
194 | "function foo(Boolean) { if (Boolean(1)) {} }", | |
195 | "const Boolean = () => {}; if (Boolean(1)) {}", | |
196 | { code: "if (Boolean()) {}", globals: { Boolean: "off" } }, | |
197 | "const undefined = 'lol'; if (undefined) {}", | |
198 | { code: "if (undefined) {}", globals: { undefined: "off" } } | |
eb39fafa DC |
199 | ], |
200 | invalid: [ | |
201 | { code: "for(;true;);", errors: [{ messageId: "unexpected", type: "Literal" }] }, | |
202 | { code: "for(;``;);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, | |
203 | { code: "for(;`foo`;);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, | |
204 | { code: "for(;`foo${bar}`;);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, | |
205 | { code: "do{}while(true)", errors: [{ messageId: "unexpected", type: "Literal" }] }, | |
6f036462 TL |
206 | { code: "do{}while('1')", errors: [{ messageId: "unexpected", type: "Literal" }] }, |
207 | { code: "do{}while(0)", errors: [{ messageId: "unexpected", type: "Literal" }] }, | |
eb39fafa DC |
208 | { code: "do{}while(t = -2)", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] }, |
209 | { code: "do{}while(``)", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, | |
210 | { code: "do{}while(`foo`)", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, | |
211 | { code: "do{}while(`foo${bar}`)", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, | |
212 | { code: "true ? 1 : 2;", errors: [{ messageId: "unexpected", type: "Literal" }] }, | |
6f036462 | 213 | { code: "1 ? 1 : 2;", errors: [{ messageId: "unexpected", type: "Literal" }] }, |
eb39fafa DC |
214 | { code: "q = 0 ? 1 : 2;", errors: [{ messageId: "unexpected", type: "Literal" }] }, |
215 | { code: "(q = 0) ? 1 : 2;", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] }, | |
216 | { code: "`` ? 1 : 2;", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, | |
217 | { code: "`foo` ? 1 : 2;", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, | |
218 | { code: "`foo${bar}` ? 1 : 2;", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, | |
219 | { code: "if(-2);", errors: [{ messageId: "unexpected", type: "UnaryExpression" }] }, | |
220 | { code: "if(true);", errors: [{ messageId: "unexpected", type: "Literal" }] }, | |
6f036462 | 221 | { code: "if(1);", errors: [{ messageId: "unexpected", type: "Literal" }] }, |
eb39fafa DC |
222 | { code: "if({});", errors: [{ messageId: "unexpected", type: "ObjectExpression" }] }, |
223 | { code: "if(0 < 1);", errors: [{ messageId: "unexpected", type: "BinaryExpression" }] }, | |
224 | { code: "if(0 || 1);", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
225 | { code: "if(a, 1);", errors: [{ messageId: "unexpected", type: "SequenceExpression" }] }, | |
226 | { code: "if(`foo`);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, | |
227 | { code: "if(``);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, | |
228 | { code: "if(`\\\n`);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, | |
229 | { code: "if(`${'bar'}`);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, | |
230 | { code: "if(`${'bar' + `foo`}`);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, | |
231 | { code: "if(`foo${false || true}`);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, | |
6f036462 | 232 | { code: "if(`foo${0 || 1}`);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, |
eb39fafa DC |
233 | { code: "if(`foo${bar}`);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, |
234 | { code: "if(`${bar}foo`);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, | |
456be15e TL |
235 | { code: "if(!(true || a));", errors: [{ messageId: "unexpected", type: "UnaryExpression" }] }, |
236 | { code: "if(!(a && void b && c));", errors: [{ messageId: "unexpected", type: "UnaryExpression" }] }, | |
237 | { code: "if(0 || !(a && null));", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
238 | { code: "if(1 + !(a || true));", errors: [{ messageId: "unexpected", type: "BinaryExpression" }] }, | |
239 | { code: "if(!(null && a) > 1);", errors: [{ messageId: "unexpected", type: "BinaryExpression" }] }, | |
240 | { code: "if(+(!(a && 0)));", errors: [{ messageId: "unexpected", type: "UnaryExpression" }] }, | |
241 | { code: "if(!typeof a === 'string');", errors: [{ messageId: "unexpected", type: "BinaryExpression" }] }, | |
242 | { code: "if(-('foo' || a));", errors: [{ messageId: "unexpected", type: "UnaryExpression" }] }, | |
243 | { code: "if(+(void a && b) === ~(1 || c));", errors: [{ messageId: "unexpected", type: "BinaryExpression" }] }, | |
244 | { code: "if(a ||= true);", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] }, | |
245 | { code: "if(a ||= 5);", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] }, | |
246 | { code: "if(a ||= 'foo' || b);", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] }, | |
247 | { code: "if(a ||= b || /regex/);", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] }, | |
248 | { code: "if(a ||= b ||= true);", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] }, | |
249 | { code: "if(a ||= b ||= c || 1);", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] }, | |
250 | { code: "if(!(a ||= true));", errors: [{ messageId: "unexpected", type: "UnaryExpression" }] }, | |
251 | { code: "if(!(a ||= 'foo') === true);", errors: [{ messageId: "unexpected", type: "BinaryExpression" }] }, | |
252 | { code: "if(!(a ||= 'foo') === false);", errors: [{ messageId: "unexpected", type: "BinaryExpression" }] }, | |
253 | { code: "if(a || (b ||= true));", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
254 | { code: "if((a ||= 1) || b);", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
255 | { code: "if((a ||= true) && true);", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
256 | { code: "if(true && (a ||= true));", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
257 | { code: "if(a &&= false);", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] }, | |
258 | { code: "if(a &&= null);", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] }, | |
259 | { code: "if(a &&= void b);", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] }, | |
260 | { code: "if(a &&= 0 && b);", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] }, | |
261 | { code: "if(a &&= b && '');", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] }, | |
262 | { code: "if(a &&= b &&= false);", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] }, | |
263 | { code: "if(a &&= b &&= c && false);", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] }, | |
264 | { code: "if(!(a &&= false));", errors: [{ messageId: "unexpected", type: "UnaryExpression" }] }, | |
265 | { code: "if(!(a &&= 0) + 1);", errors: [{ messageId: "unexpected", type: "BinaryExpression" }] }, | |
266 | { code: "if(a && (b &&= false));", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
267 | { code: "if((a &&= null) && b);", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
268 | { code: "if(false || (a &&= false));", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
269 | { code: "if((a &&= false) || false);", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
eb39fafa DC |
270 | |
271 | { code: "while([]);", errors: [{ messageId: "unexpected", type: "ArrayExpression" }] }, | |
272 | { code: "while(~!0);", errors: [{ messageId: "unexpected", type: "UnaryExpression" }] }, | |
273 | { code: "while(x = 1);", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] }, | |
274 | { code: "while(function(){});", errors: [{ messageId: "unexpected", type: "FunctionExpression" }] }, | |
275 | { code: "while(true);", errors: [{ messageId: "unexpected", type: "Literal" }] }, | |
6f036462 | 276 | { code: "while(1);", errors: [{ messageId: "unexpected", type: "Literal" }] }, |
eb39fafa DC |
277 | { code: "while(() => {});", errors: [{ messageId: "unexpected", type: "ArrowFunctionExpression" }] }, |
278 | { code: "while(`foo`);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, | |
279 | { code: "while(``);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, | |
280 | { code: "while(`${'foo'}`);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, | |
281 | { code: "while(`${'foo' + 'bar'}`);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] }, | |
282 | ||
283 | // #5228 , typeof conditions | |
284 | { code: "if(typeof x){}", errors: [{ messageId: "unexpected", type: "UnaryExpression" }] }, | |
eb39fafa DC |
285 | { code: "if(typeof 'abc' === 'string'){}", errors: [{ messageId: "unexpected", type: "BinaryExpression" }] }, |
286 | { code: "if(a = typeof b){}", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] }, | |
287 | { code: "if(a, typeof b){}", errors: [{ messageId: "unexpected", type: "SequenceExpression" }] }, | |
288 | { code: "if(typeof 'a' == 'string' || typeof 'b' == 'string'){}", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
289 | { code: "while(typeof x){}", errors: [{ messageId: "unexpected", type: "UnaryExpression" }] }, | |
290 | ||
291 | // #5726, void conditions | |
292 | { code: "if(1 || void x);", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
293 | { code: "if(void x);", errors: [{ messageId: "unexpected", type: "UnaryExpression" }] }, | |
294 | { code: "if(y = void x);", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] }, | |
295 | { code: "if(x, void x);", errors: [{ messageId: "unexpected", type: "SequenceExpression" }] }, | |
296 | { code: "if(void x === void y);", errors: [{ messageId: "unexpected", type: "BinaryExpression" }] }, | |
297 | { code: "if(void x && a);", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
298 | { code: "if(a && void x);", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
299 | ||
300 | // #5693 | |
301 | { code: "if(false && abc==='str'){}", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
302 | { code: "if(true || abc==='str'){}", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
6f036462 | 303 | { code: "if(1 || abc==='str'){}", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, |
eb39fafa DC |
304 | { code: "if(abc==='str' || true){}", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, |
305 | { code: "if(abc==='str' || true || def ==='str'){}", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
306 | { code: "if(false || true){}", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
307 | { code: "if(typeof abc==='str' || true){}", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
308 | ||
309 | // #11181, string literals | |
6f036462 TL |
310 | { code: "if('str' || a){}", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, |
311 | { code: "if('str' || abc==='str'){}", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
eb39fafa DC |
312 | { code: "if('str1' || 'str2'){}", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, |
313 | { code: "if('str1' && 'str2'){}", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
314 | { code: "if(abc==='str' || 'str'){}", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
315 | { code: "if(a || 'str'){}", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, | |
316 | ||
317 | { | |
318 | code: "function* foo(){while(true){} yield 'foo';}", | |
319 | errors: [{ messageId: "unexpected", type: "Literal" }] | |
320 | }, | |
321 | { | |
322 | code: "function* foo(){while(true){if (true) {yield 'foo';}}}", | |
323 | errors: [{ messageId: "unexpected", type: "Literal" }] | |
324 | }, | |
325 | { | |
326 | code: "function* foo(){while(true){yield 'foo';} while(true) {}}", | |
327 | errors: [{ messageId: "unexpected", type: "Literal" }] | |
328 | }, | |
329 | { | |
330 | code: "var a = function* foo(){while(true){} yield 'foo';}", | |
331 | errors: [{ messageId: "unexpected", type: "Literal" }] | |
332 | }, | |
333 | { | |
334 | code: "while (true) { function* foo() {yield;}}", | |
335 | errors: [{ messageId: "unexpected", type: "Literal" }] | |
336 | }, | |
337 | { | |
338 | code: "function* foo(){if (true) {yield 'foo';}}", | |
339 | errors: [{ messageId: "unexpected", type: "Literal" }] | |
340 | }, | |
341 | { | |
342 | code: "function* foo() {for (let foo = yield; true;) {}}", | |
343 | errors: [{ messageId: "unexpected", type: "Literal" }] | |
344 | }, | |
345 | { | |
346 | code: "function* foo() {for (foo = yield; true;) {}}", | |
347 | errors: [{ messageId: "unexpected", type: "Literal" }] | |
348 | }, | |
349 | { | |
350 | code: "function foo() {while (true) {function* bar() {while (true) {yield;}}}}", | |
351 | errors: [{ messageId: "unexpected", type: "Literal" }] | |
352 | }, | |
353 | { | |
354 | code: "function foo() {while (true) {const bar = function*() {while (true) {yield;}}}}", | |
355 | errors: [{ messageId: "unexpected", type: "Literal" }] | |
356 | }, | |
357 | { | |
358 | code: "function* foo() { for (let foo = 1 + 2 + 3 + (yield); true; baz) {}}", | |
359 | errors: [{ messageId: "unexpected", type: "Literal" }] | |
360 | }, | |
361 | ||
362 | // #12225 | |
363 | { | |
364 | code: "if([a]) {}", | |
365 | errors: [{ messageId: "unexpected", type: "ArrayExpression" }] | |
366 | }, | |
367 | { | |
368 | code: "if([]) {}", | |
369 | errors: [{ messageId: "unexpected", type: "ArrayExpression" }] | |
370 | }, | |
371 | { | |
372 | code: "if(''+['a']) {}", | |
373 | errors: [{ messageId: "unexpected", type: "BinaryExpression" }] | |
374 | }, | |
375 | { | |
376 | code: "if(''+[]) {}", | |
377 | errors: [{ messageId: "unexpected", type: "BinaryExpression" }] | |
378 | }, | |
eb39fafa DC |
379 | { |
380 | code: "if(+1) {}", | |
381 | errors: [{ messageId: "unexpected", type: "UnaryExpression" }] | |
382 | }, | |
383 | { | |
384 | code: "if ([,] + ''){}", | |
385 | errors: [{ messageId: "unexpected", type: "BinaryExpression" }] | |
6f036462 TL |
386 | }, |
387 | ||
388 | // #13238 | |
389 | { code: "if(/foo/ui);", parserOptions: { ecmaVersion: 11 }, errors: [{ messageId: "unexpected", type: "Literal" }] }, | |
390 | { code: "if(0n);", parserOptions: { ecmaVersion: 11 }, errors: [{ messageId: "unexpected", type: "Literal" }] }, | |
391 | { code: "if(0b0n);", parserOptions: { ecmaVersion: 11 }, errors: [{ messageId: "unexpected", type: "Literal" }] }, | |
392 | { code: "if(0o0n);", parserOptions: { ecmaVersion: 11 }, errors: [{ messageId: "unexpected", type: "Literal" }] }, | |
393 | { code: "if(0x0n);", parserOptions: { ecmaVersion: 11 }, errors: [{ messageId: "unexpected", type: "Literal" }] }, | |
394 | { code: "if(0b1n);", parserOptions: { ecmaVersion: 11 }, errors: [{ messageId: "unexpected", type: "Literal" }] }, | |
395 | { code: "if(0o1n);", parserOptions: { ecmaVersion: 11 }, errors: [{ messageId: "unexpected", type: "Literal" }] }, | |
396 | { code: "if(0x1n);", parserOptions: { ecmaVersion: 11 }, errors: [{ messageId: "unexpected", type: "Literal" }] }, | |
34eeec05 TL |
397 | { code: "if(0x1n || foo);", parserOptions: { ecmaVersion: 11 }, errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }, |
398 | ||
399 | // Classes and instances are always truthy | |
400 | { code: "if(class {}) {}", errors: [{ messageId: "unexpected" }] }, | |
401 | { code: "if(new Foo()) {}", errors: [{ messageId: "unexpected" }] }, | |
402 | ||
403 | // Boxed primitives are always truthy | |
404 | { code: "if(new Boolean(foo)) {}", errors: [{ messageId: "unexpected" }] }, | |
405 | { code: "if(new String(foo)) {}", errors: [{ messageId: "unexpected" }] }, | |
8f9d1d4d DC |
406 | { code: "if(new Number(foo)) {}", errors: [{ messageId: "unexpected" }] }, |
407 | ||
408 | // Spreading a constant array | |
409 | { code: "if(`${[...['a']]}`) {}", errors: [{ messageId: "unexpected" }] }, | |
410 | ||
411 | /* | |
412 | * undefined is always falsy (except in old browsers that let you | |
413 | * re-assign, but that's an obscure enough edge case to not worry about) | |
414 | */ | |
415 | { code: "if (undefined) {}", errors: [{ messageId: "unexpected" }] }, | |
34eeec05 | 416 | |
8f9d1d4d DC |
417 | // Coercion to boolean via Boolean function |
418 | { code: "if (Boolean(1)) {}", errors: [{ messageId: "unexpected" }] }, | |
419 | { code: "if (Boolean()) {}", errors: [{ messageId: "unexpected" }] }, | |
420 | { code: "if (Boolean([a])) {}", errors: [{ messageId: "unexpected" }] }, | |
421 | { code: "if (Boolean(1)) { function Boolean() {}}", errors: [{ messageId: "unexpected" }] } | |
eb39fafa DC |
422 | ] |
423 | }); |