]> git.proxmox.com Git - pve-eslint.git/blame - eslint/tests/lib/rules/no-lone-blocks.js
import 8.3.0 source
[pve-eslint.git] / eslint / tests / lib / rules / no-lone-blocks.js
CommitLineData
eb39fafa
DC
1/**
2 * @fileoverview Tests for no-lone-blocks rule.
3 * @author Brandon Mills
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const rule = require("../../../lib/rules/no-lone-blocks"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15//------------------------------------------------------------------------------
609c276f 16// Tests
eb39fafa
DC
17//------------------------------------------------------------------------------
18
19const ruleTester = new RuleTester();
20
21ruleTester.run("no-lone-blocks", rule, {
22 valid: [
23 "if (foo) { if (bar) { baz(); } }",
24 "do { bar(); } while (foo)",
25 "function foo() { while (bar) { baz() } }",
26
27 // Block-level bindings
28 { code: "{ let x = 1; }", parserOptions: { ecmaVersion: 6 } },
29 { code: "{ const x = 1; }", parserOptions: { ecmaVersion: 6 } },
30 { code: "'use strict'; { function bar() {} }", parserOptions: { ecmaVersion: 6 } },
31 { code: "{ function bar() {} }", parserOptions: { ecmaVersion: 6, ecmaFeatures: { impliedStrict: true } } },
32 { code: "{ class Bar {} }", parserOptions: { ecmaVersion: 6 } },
33
34 { code: "{ {let y = 1;} let x = 1; }", parserOptions: { ecmaVersion: 6 } },
35 `
36 switch (foo) {
37 case bar: {
38 baz;
39 }
40 }
41 `,
42 `
43 switch (foo) {
44 case bar: {
45 baz;
46 }
47 case qux: {
48 boop;
49 }
50 }
51 `,
52 `
53 switch (foo) {
54 case bar:
55 {
56 baz;
57 }
58 }
59 `,
609c276f
TL
60 { code: "function foo() { { const x = 4 } const x = 3 }", parserOptions: { ecmaVersion: 6 } },
61
62 { code: "class C { static {} }", parserOptions: { ecmaVersion: 2022 } },
63 { code: "class C { static { foo; } }", parserOptions: { ecmaVersion: 2022 } },
64 { code: "class C { static { if (foo) { block; } } }", parserOptions: { ecmaVersion: 2022 } },
65 { code: "class C { static { lbl: { block; } } }", parserOptions: { ecmaVersion: 2022 } },
66 { code: "class C { static { { let block; } something; } }", parserOptions: { ecmaVersion: 2022 } },
67 { code: "class C { static { something; { const block = 1; } } }", parserOptions: { ecmaVersion: 2022 } },
68 { code: "class C { static { { function block(){} } something; } }", parserOptions: { ecmaVersion: 2022 } },
69 { code: "class C { static { something; { class block {} } } }", parserOptions: { ecmaVersion: 2022 } }
eb39fafa
DC
70 ],
71 invalid: [
72 {
73 code: "{}",
74 errors: [{
75 messageId: "redundantBlock",
76 type: "BlockStatement"
77 }]
78 },
79 {
80 code: "{var x = 1;}",
81 errors: [{
82 messageId: "redundantBlock",
83 type: "BlockStatement"
84 }]
85 },
86 {
87 code: "foo(); {} bar();",
88 errors: [{
89 messageId: "redundantBlock",
90 type: "BlockStatement"
91 }]
92 },
93 {
94 code: "if (foo) { bar(); {} baz(); }",
95 errors: [{
96 messageId: "redundantNestedBlock",
97 type: "BlockStatement"
98 }]
99 },
100 {
101 code: "{ \n{ } }",
102 errors: [
103 {
104 messageId: "redundantBlock",
105 type: "BlockStatement",
106 line: 1
107 },
108 {
109 messageId: "redundantNestedBlock",
110 type: "BlockStatement",
111 line: 2
112 }
113 ]
114 },
115 {
116 code: "function foo() { bar(); {} baz(); }",
117 errors: [{
118 messageId: "redundantNestedBlock",
119 type: "BlockStatement"
120 }]
121 },
122 {
123 code: "while (foo) { {} }",
124 errors: [{
125 messageId: "redundantNestedBlock",
126 type: "BlockStatement"
127 }]
128 },
129
130 // Non-block-level bindings, even in ES6
131 {
132 code: "{ function bar() {} }",
133 parserOptions: { ecmaVersion: 6 },
134 errors: [{
135 messageId: "redundantBlock",
136 type: "BlockStatement"
137 }]
138 },
139 {
140 code: "{var x = 1;}",
141 parserOptions: { ecmaVersion: 6 },
142 errors: [{
143 messageId: "redundantBlock",
144 type: "BlockStatement"
145 }]
146 },
147
148 {
149 code: "{ \n{var x = 1;}\n let y = 2; } {let z = 1;}",
150 parserOptions: { ecmaVersion: 6 },
151 errors: [{
152 messageId: "redundantNestedBlock",
153 type: "BlockStatement",
154 line: 2
155 }]
156 },
157 {
158 code: "{ \n{let x = 1;}\n var y = 2; } {let z = 1;}",
159 parserOptions: { ecmaVersion: 6 },
160 errors: [{
161 messageId: "redundantBlock",
162 type: "BlockStatement",
163 line: 1
164 }]
165 },
166 {
167 code: "{ \n{var x = 1;}\n var y = 2; }\n {var z = 1;}",
168 parserOptions: { ecmaVersion: 6 },
169 errors: [
170 {
171 messageId: "redundantBlock",
172 type: "BlockStatement",
173 line: 1
174 },
175 {
176 messageId: "redundantNestedBlock",
177 type: "BlockStatement",
178 line: 2
179 },
180 {
181 messageId: "redundantBlock",
182 type: "BlockStatement",
183 line: 4
184 }
185 ]
186 },
187 {
188 code: `
189 switch (foo) {
190 case 1:
191 foo();
192 {
193 bar;
194 }
195 }
196 `,
197 errors: [{
198 messageId: "redundantBlock",
199 type: "BlockStatement",
200 line: 5
201 }]
202 },
203 {
204 code: `
205 switch (foo) {
206 case 1:
207 {
208 bar;
209 }
210 foo();
211 }
212 `,
213 errors: [{
214 messageId: "redundantBlock",
215 type: "BlockStatement",
216 line: 4
217 }]
218 },
219 {
220 code: `
221 function foo () {
222 {
223 const x = 4;
224 }
225 }
226 `,
227 parserOptions: { ecmaVersion: 6 },
228 errors: [{
229 messageId: "redundantNestedBlock",
230 type: "BlockStatement",
231 line: 3
232 }]
233 },
234 {
235 code: `
236 function foo () {
237 {
238 var x = 4;
239 }
240 }
241 `,
242 errors: [{
243 messageId: "redundantNestedBlock",
244 type: "BlockStatement",
245 line: 3
246 }]
609c276f
TL
247 },
248 {
249 code: `
250 class C {
251 static {
252 if (foo) {
253 {
254 let block;
255 }
256 }
257 }
258 }
259 `,
260 parserOptions: { ecmaVersion: 2022 },
261 errors: [{
262 messageId: "redundantNestedBlock",
263 type: "BlockStatement",
264 line: 5
265 }]
266 },
267 {
268 code: `
269 class C {
270 static {
271 if (foo) {
272 {
273 block;
274 }
275 something;
276 }
277 }
278 }
279 `,
280 parserOptions: { ecmaVersion: 2022 },
281 errors: [{
282 messageId: "redundantNestedBlock",
283 type: "BlockStatement",
284 line: 5
285 }]
286 },
287 {
288 code: `
289 class C {
290 static {
291 {
292 block;
293 }
294 }
295 }
296 `,
297 parserOptions: { ecmaVersion: 2022 },
298 errors: [{
299 messageId: "redundantNestedBlock",
300 type: "BlockStatement",
301 line: 4
302 }]
303 },
304 {
305 code: `
306 class C {
307 static {
308 {
309 let block;
310 }
311 }
312 }
313 `,
314 parserOptions: { ecmaVersion: 2022 },
315 errors: [{
316 messageId: "redundantNestedBlock",
317 type: "BlockStatement",
318 line: 4
319 }]
320 },
321 {
322 code: `
323 class C {
324 static {
325 {
326 const block = 1;
327 }
328 }
329 }
330 `,
331 parserOptions: { ecmaVersion: 2022 },
332 errors: [{
333 messageId: "redundantNestedBlock",
334 type: "BlockStatement",
335 line: 4
336 }]
337 },
338 {
339 code: `
340 class C {
341 static {
342 {
343 function block() {}
344 }
345 }
346 }
347 `,
348 parserOptions: { ecmaVersion: 2022 },
349 errors: [{
350 messageId: "redundantNestedBlock",
351 type: "BlockStatement",
352 line: 4
353 }]
354 },
355 {
356 code: `
357 class C {
358 static {
359 {
360 class block {}
361 }
362 }
363 }
364 `,
365 parserOptions: { ecmaVersion: 2022 },
366 errors: [{
367 messageId: "redundantNestedBlock",
368 type: "BlockStatement",
369 line: 4
370 }]
371 },
372 {
373 code: `
374 class C {
375 static {
376 {
377 var block;
378 }
379 something;
380 }
381 }
382 `,
383 parserOptions: { ecmaVersion: 2022 },
384 errors: [{
385 messageId: "redundantNestedBlock",
386 type: "BlockStatement",
387 line: 4
388 }]
389 },
390 {
391 code: `
392 class C {
393 static {
394 something;
395 {
396 var block;
397 }
398 }
399 }
400 `,
401 parserOptions: { ecmaVersion: 2022 },
402 errors: [{
403 messageId: "redundantNestedBlock",
404 type: "BlockStatement",
405 line: 5
406 }]
407 },
408 {
409 code: `
410 class C {
411 static {
412 {
413 block;
414 }
415 something;
416 }
417 }
418 `,
419 parserOptions: { ecmaVersion: 2022 },
420 errors: [{
421 messageId: "redundantNestedBlock",
422 type: "BlockStatement",
423 line: 4
424 }]
425 },
426 {
427 code: `
428 class C {
429 static {
430 something;
431 {
432 block;
433 }
434 }
435 }
436 `,
437 parserOptions: { ecmaVersion: 2022 },
438 errors: [{
439 messageId: "redundantNestedBlock",
440 type: "BlockStatement",
441 line: 5
442 }]
eb39fafa
DC
443 }
444 ]
445});