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