]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-multi-spaces.js
first commit
[pve-eslint.git] / eslint / tests / lib / rules / no-multi-spaces.js
1 /**
2 * @fileoverview tests for checking multiple spaces.
3 * @author Vignesh Anand aka vegetableman
4 */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Requirements
9 //------------------------------------------------------------------------------
10
11 const rule = require("../../../lib/rules/no-multi-spaces"),
12 { RuleTester } = require("../../../lib/rule-tester");
13
14 //------------------------------------------------------------------------------
15 // Tests
16 //------------------------------------------------------------------------------
17
18 const ruleTester = new RuleTester();
19
20 ruleTester.run("no-multi-spaces", rule, {
21
22 valid: [
23 "var a = 1;",
24 "var a=1;",
25 "var a = 1, b = 2;",
26 "var arr = [1, 2];",
27 "var arr = [ (1), (2) ];",
28 "var obj = {'a': 1, 'b': (2)};",
29 "\t\tvar x = 5,\n\t\t y = 2;",
30 "a, b",
31 "a >>> b",
32 "a ^ b",
33 "(a) | (b)",
34 "a & b",
35 "a << b",
36 "a !== b",
37 "a >>>= b",
38 "if (a & b) { }",
39 "function foo(a,b) {}",
40 "function foo(a, b) {}",
41 "if ( a === 3 && b === 4) {}",
42 "if ( a === 3||b === 4 ) {}",
43 "if ( a <= 4) {}",
44 "var foo = bar === 1 ? 2: 3",
45 "[1, , 3]",
46 "[1, ]",
47 "[ ( 1 ) , ( 2 ) ]",
48 "a = 1, b = 2;",
49 "(function(a, b){})",
50 "x.in = 0;",
51 "(function(a,/* b, */c){})",
52 "(function(a,/*b,*/c){})",
53 "(function(a, /*b,*/c){})",
54 "(function(a,/*b,*/ c){})",
55 "(function(a, /*b,*/ c){})",
56 "(function(/*a, b, */c){})",
57 "(function(/*a, */b, c){})",
58 "(function(a, b/*, c*/){})",
59 "(function(a, b/*,c*/){})",
60 "(function(a, b /*,c*/){})",
61 "(function(a/*, b ,c*/){})",
62 "(function(a /*, b ,c*/){})",
63 "(function(a /*, b ,c*/){})",
64 "/**\n * hello\n * @param {foo} int hi\n * set.\n * @private\n*/",
65 "/**\n * hello\n * @param {foo} int hi\n * set.\n * set.\n * @private\n*/",
66 "var a,/* b,*/c;",
67 "var foo = [1,/* 2,*/3];",
68 "var bar = {a: 1,/* b: 2*/c: 3};",
69 "var foo = \"hello world\";",
70 "function foo() {\n return;\n}",
71 "function foo() {\n if (foo) {\n return;\n }\n}",
72 { code: "var foo = `hello world`;", parserOptions: { ecmaVersion: 6 } },
73 "({ a: b })",
74 {
75 code: "var answer = 6 * 7;",
76 options: [{ exceptions: { VariableDeclaration: true, BinaryExpression: true } }]
77 },
78
79 // https://github.com/eslint/eslint/issues/7693
80 "var x = 5; // comment",
81 "var x = 5; /* multiline\n * comment\n */",
82 "var x = 5;\n // comment",
83 "var x = 5; \n// comment",
84 "var x = 5;\n /* multiline\n * comment\n */",
85 "var x = 5; \n/* multiline\n * comment\n */",
86 { code: "var x = 5; // comment", options: [{ ignoreEOLComments: false }] },
87 { code: "var x = 5; /* multiline\n * comment\n */", options: [{ ignoreEOLComments: false }] },
88 { code: "var x = 5;\n // comment", options: [{ ignoreEOLComments: false }] },
89 { code: "var x = 5; \n// comment", options: [{ ignoreEOLComments: false }] },
90 { code: "var x = 5;\n /* multiline\n * comment\n */", options: [{ ignoreEOLComments: false }] },
91 { code: "var x = 5; \n/* multiline\n * comment\n */", options: [{ ignoreEOLComments: false }] },
92 { code: "var x = 5; // comment", options: [{ ignoreEOLComments: true }] },
93 { code: "var x = 5; /* multiline\n * comment\n */", options: [{ ignoreEOLComments: true }] },
94 { code: "var x = 5;\n // comment", options: [{ ignoreEOLComments: true }] },
95 { code: "var x = 5; \n// comment", options: [{ ignoreEOLComments: true }] },
96 { code: "var x = 5;\n /* multiline\n * comment\n */", options: [{ ignoreEOLComments: true }] },
97 { code: "var x = 5; \n/* multiline\n * comment\n */", options: [{ ignoreEOLComments: true }] },
98
99 "foo\n\f bar",
100 "foo\n\u2003 bar",
101 "foo\n \f bar",
102
103 // https://github.com/eslint/eslint/issues/9001
104 "a".repeat(2e5),
105
106 "foo\t\t+bar"
107 ],
108
109 invalid: [
110 {
111 code: "function foo(a, b) {}",
112 output: "function foo(a, b) {}",
113 errors: [{
114 messageId: "multipleSpaces",
115 data: { displayValue: "b" },
116 type: "Identifier",
117 column: 16,
118 endColumn: 18
119 }]
120 },
121 {
122 code: "var foo = (a, b) => {}",
123 output: "var foo = (a, b) => {}",
124 parserOptions: { ecmaVersion: 6 },
125 errors: [{
126 messageId: "multipleSpaces",
127 data: { displayValue: "b" },
128 type: "Identifier",
129 column: 14,
130 endColumn: 16
131 }]
132 },
133 {
134 code: "var a = 1",
135 output: "var a = 1",
136 errors: [{
137 messageId: "multipleSpaces",
138 data: { displayValue: "1" },
139 type: "Numeric",
140 column: 8,
141 endColumn: 10
142 }]
143 },
144 {
145 code: "var a = 1, b = 2;",
146 output: "var a = 1, b = 2;",
147 errors: [{
148 messageId: "multipleSpaces",
149 data: { displayValue: "b" },
150 type: "Identifier"
151 }]
152 },
153 {
154 code: "a << b",
155 output: "a << b",
156 errors: [{
157 messageId: "multipleSpaces",
158 data: { displayValue: "b" },
159 type: "Identifier"
160 }]
161 },
162 {
163 code: "var arr = {'a': 1, 'b': 2};",
164 output: "var arr = {'a': 1, 'b': 2};",
165 errors: [{
166 messageId: "multipleSpaces",
167 data: { displayValue: "'b'" },
168 type: "String",
169 column: 19,
170 endColumn: 21
171 }]
172 },
173 {
174 code: "if (a & b) { }",
175 output: "if (a & b) { }",
176 errors: [{
177 messageId: "multipleSpaces",
178 data: { displayValue: "b" },
179 type: "Identifier"
180 }]
181 },
182 {
183 code: "if ( a === 3 && b === 4) {}",
184 output: "if ( a === 3 && b === 4) {}",
185 errors: [{
186 messageId: "multipleSpaces",
187 data: { displayValue: "&&" },
188 type: "Punctuator"
189 }, {
190 messageId: "multipleSpaces",
191 data: { displayValue: "b" },
192 type: "Identifier"
193 }]
194 },
195 {
196 code: "var foo = bar === 1 ? 2: 3",
197 output: "var foo = bar === 1 ? 2: 3",
198 errors: [{
199 messageId: "multipleSpaces",
200 data: { displayValue: "2" },
201 type: "Numeric"
202 }, {
203 messageId: "multipleSpaces",
204 data: { displayValue: "3" },
205 type: "Numeric"
206 }]
207 },
208 {
209 code: "var a = [1, 2, 3, 4]",
210 output: "var a = [1, 2, 3, 4]",
211 errors: [{
212 messageId: "multipleSpaces",
213 data: { displayValue: "2" },
214 type: "Numeric"
215 }, {
216 messageId: "multipleSpaces",
217 data: { displayValue: "3" },
218 type: "Numeric"
219 }, {
220 messageId: "multipleSpaces",
221 data: { displayValue: "4" },
222 type: "Numeric"
223 }]
224 },
225 {
226 code: "var arr = [1, 2];",
227 output: "var arr = [1, 2];",
228 errors: [{
229 messageId: "multipleSpaces",
230 data: { displayValue: "2" },
231 type: "Numeric"
232 }]
233 },
234 {
235 code: "[ , 1, , 3, , ]",
236 output: "[ , 1, , 3, , ]",
237 errors: [{
238 messageId: "multipleSpaces",
239 data: { displayValue: "," },
240 type: "Punctuator",
241 column: 2,
242 endColumn: 4
243 }, {
244 messageId: "multipleSpaces",
245 data: { displayValue: "," },
246 type: "Punctuator",
247 column: 8,
248 endColumn: 10
249 }, {
250 messageId: "multipleSpaces",
251 data: { displayValue: "," },
252 type: "Punctuator",
253 column: 14,
254 endColumn: 16
255 }, {
256 messageId: "multipleSpaces",
257 data: { displayValue: "]" },
258 type: "Punctuator",
259 column: 17,
260 endColumn: 19
261 }]
262 },
263 {
264 code: "a >>> b",
265 output: "a >>> b",
266 errors: [{
267 messageId: "multipleSpaces",
268 data: { displayValue: "b" },
269 type: "Identifier"
270 }]
271 },
272 {
273 code: "a = 1, b = 2;",
274 output: "a = 1, b = 2;",
275 errors: [{
276 messageId: "multipleSpaces",
277 data: { displayValue: "b" },
278 type: "Identifier"
279 }, {
280 messageId: "multipleSpaces",
281 data: { displayValue: "2" },
282 type: "Numeric"
283 }]
284 },
285 {
286 code: "(function(a, b){})",
287 output: "(function(a, b){})",
288 errors: [{
289 messageId: "multipleSpaces",
290 data: { displayValue: "b" },
291 type: "Identifier"
292 }]
293 },
294 {
295 code: "function foo(a, b){}",
296 output: "function foo(a, b){}",
297 errors: [{
298 messageId: "multipleSpaces",
299 data: { displayValue: "b" },
300 type: "Identifier"
301 }]
302 },
303 {
304 code: "var o = { fetch: function () {} };",
305 output: "var o = { fetch: function () {} };",
306 errors: [{
307 messageId: "multipleSpaces",
308 data: { displayValue: "(" },
309 type: "Punctuator"
310 }]
311 },
312 {
313 code: "function foo () {}",
314 output: "function foo () {}",
315 errors: [{
316 messageId: "multipleSpaces",
317 data: { displayValue: "(" },
318 type: "Punctuator",
319 column: 13,
320 endColumn: 19
321 }]
322 },
323 {
324 code: "if (foo) {}",
325 output: "if (foo) {}",
326 errors: [{
327 messageId: "multipleSpaces",
328 data: { displayValue: "{" },
329 type: "Punctuator"
330 }]
331 },
332 {
333 code: "function foo(){}",
334 output: "function foo(){}",
335 errors: [{
336 messageId: "multipleSpaces",
337 data: { displayValue: "foo" },
338 type: "Identifier"
339 }]
340 },
341 {
342 code: "if (foo) {}",
343 output: "if (foo) {}",
344 errors: [{
345 messageId: "multipleSpaces",
346 data: { displayValue: "(" },
347 type: "Punctuator"
348 }]
349 },
350 {
351 code: "try {} catch(ex) {}",
352 output: "try {} catch(ex) {}",
353 errors: [{
354 messageId: "multipleSpaces",
355 data: { displayValue: "{" },
356 type: "Punctuator"
357 }]
358 },
359 {
360 code: "try {} catch (ex) {}",
361 output: "try {} catch (ex) {}",
362 errors: [{
363 messageId: "multipleSpaces",
364 data: { displayValue: "(" },
365 type: "Punctuator"
366 }]
367 },
368 {
369 code: "throw error;",
370 output: "throw error;",
371 errors: [{
372 messageId: "multipleSpaces",
373 data: { displayValue: "error" },
374 type: "Identifier"
375 }]
376 },
377 {
378 code: "function foo() { return bar; }",
379 output: "function foo() { return bar; }",
380 errors: [{
381 messageId: "multipleSpaces",
382 data: { displayValue: "bar" },
383 type: "Identifier"
384 }]
385 },
386 {
387 code: "switch (a) {default: foo(); break;}",
388 output: "switch (a) {default: foo(); break;}",
389 errors: [{
390 messageId: "multipleSpaces",
391 data: { displayValue: "(" },
392 type: "Punctuator"
393 }]
394 },
395 {
396 code: "var answer = 6 * 7;",
397 output: "var answer = 6 * 7;",
398 errors: [{
399 messageId: "multipleSpaces",
400 data: { displayValue: "answer" },
401 type: "Identifier"
402 }, {
403 messageId: "multipleSpaces",
404 data: { displayValue: "7" },
405 type: "Numeric"
406 }]
407 },
408 {
409 code: "({ a: 6 * 7 })",
410 output: "({ a: 6 * 7 })",
411 errors: [{
412 messageId: "multipleSpaces",
413 data: { displayValue: "*" },
414 type: "Punctuator"
415 }]
416 },
417 {
418 code: "({ a: b })",
419 output: "({ a: b })",
420 options: [{ exceptions: { Property: false } }],
421 errors: [{
422 messageId: "multipleSpaces",
423 data: { displayValue: "b" },
424 type: "Identifier"
425 }]
426 },
427 {
428 code: "var foo = { bar: function() { return 1 + 2; } };",
429 output: "var foo = { bar: function() { return 1 + 2; } };",
430 errors: [{
431 messageId: "multipleSpaces",
432 data: { displayValue: "+" },
433 type: "Punctuator"
434 }]
435 },
436 {
437 code: "\t\tvar x = 5,\n\t\t y = 2;",
438 output: "\t\tvar x = 5,\n\t\t y = 2;",
439 errors: [{
440 messageId: "multipleSpaces",
441 data: { displayValue: "2" },
442 type: "Numeric"
443 }]
444 },
445 {
446 code: "var x =\t 5;",
447 output: "var x = 5;",
448 errors: [{
449 messageId: "multipleSpaces",
450 data: { displayValue: "5" },
451 type: "Numeric",
452 column: 8,
453 endColumn: 11
454 }]
455 },
456
457 // https://github.com/eslint/eslint/issues/7693
458 {
459 code: "var x = /* comment */ 5;",
460 output: "var x = /* comment */ 5;",
461 errors: [{
462 messageId: "multipleSpaces",
463 data: { displayValue: "/* comment */" },
464 type: "Block"
465 }]
466 },
467 {
468 code: "var x = /* comment */ 5;",
469 output: "var x = /* comment */ 5;",
470 errors: [{
471 messageId: "multipleSpaces",
472 data: { displayValue: "5" },
473 type: "Numeric"
474 }]
475 },
476 {
477 code: "var x = 5; // comment",
478 output: "var x = 5; // comment",
479 errors: [{
480 messageId: "multipleSpaces",
481 data: { displayValue: "// comment" },
482 type: "Line",
483 column: 11,
484 endColumn: 13
485 }]
486 },
487 {
488 code: "var x = 5; // comment\nvar y = 6;",
489 output: "var x = 5; // comment\nvar y = 6;",
490 errors: [{
491 messageId: "multipleSpaces",
492 data: { displayValue: "// comment" },
493 type: "Line"
494 }]
495 },
496 {
497 code: "var x = 5; /* multiline\n * comment\n */",
498 output: "var x = 5; /* multiline\n * comment\n */",
499 errors: [{
500 messageId: "multipleSpaces",
501 data: { displayValue: "/* multiline...*/" },
502 type: "Block"
503 }]
504 },
505 {
506 code: "var x = 5; /* multiline\n * comment\n */\nvar y = 6;",
507 output: "var x = 5; /* multiline\n * comment\n */\nvar y = 6;",
508 errors: [{
509 messageId: "multipleSpaces",
510 data: { displayValue: "/* multiline...*/" },
511 type: "Block"
512 }]
513 },
514 {
515 code: "var x = 5; // this is a long comment",
516 output: "var x = 5; // this is a long comment",
517 errors: [{
518 messageId: "multipleSpaces",
519 data: { displayValue: "// this is a l..." },
520 type: "Line"
521 }]
522 },
523 {
524 code: "var x = /* comment */ 5;",
525 output: "var x = /* comment */ 5;",
526 options: [{ ignoreEOLComments: false }],
527 errors: [{
528 messageId: "multipleSpaces",
529 data: { displayValue: "/* comment */" },
530 type: "Block"
531 }]
532 },
533 {
534 code: "var x = /* comment */ 5;",
535 output: "var x = /* comment */ 5;",
536 options: [{ ignoreEOLComments: false }],
537 errors: [{
538 messageId: "multipleSpaces",
539 data: { displayValue: "5" },
540 type: "Numeric"
541 }]
542 },
543 {
544 code: "var x = 5; // comment",
545 output: "var x = 5; // comment",
546 options: [{ ignoreEOLComments: false }],
547 errors: [{
548 messageId: "multipleSpaces",
549 data: { displayValue: "// comment" },
550 type: "Line"
551 }]
552 },
553 {
554 code: "var x = 5; // comment\nvar y = 6;",
555 output: "var x = 5; // comment\nvar y = 6;",
556 options: [{ ignoreEOLComments: false }],
557 errors: [{
558 messageId: "multipleSpaces",
559 data: { displayValue: "// comment" },
560 type: "Line"
561 }]
562 },
563 {
564 code: "var x = 5; /* multiline\n * comment\n */",
565 output: "var x = 5; /* multiline\n * comment\n */",
566 options: [{ ignoreEOLComments: false }],
567 errors: [{
568 messageId: "multipleSpaces",
569 data: { displayValue: "/* multiline...*/" },
570 type: "Block"
571 }]
572 },
573 {
574 code: "var x = 5; /* multiline\n * comment\n */\nvar y = 6;",
575 output: "var x = 5; /* multiline\n * comment\n */\nvar y = 6;",
576 options: [{ ignoreEOLComments: false }],
577 errors: [{
578 messageId: "multipleSpaces",
579 data: { displayValue: "/* multiline...*/" },
580 type: "Block"
581 }]
582 },
583 {
584 code: "var x = 5; // this is a long comment",
585 output: "var x = 5; // this is a long comment",
586 options: [{ ignoreEOLComments: false }],
587 errors: [{
588 messageId: "multipleSpaces",
589 data: { displayValue: "// this is a l..." },
590 type: "Line"
591 }]
592 },
593 {
594 code: "var x = /* comment */ 5; // EOL comment",
595 output: "var x = /* comment */ 5; // EOL comment",
596 options: [{ ignoreEOLComments: true }],
597 errors: [{
598 messageId: "multipleSpaces",
599 data: { displayValue: "/* comment */" },
600 type: "Block"
601 }]
602 },
603 {
604 code: "var x = /* comment */ 5; // EOL comment\nvar y = 6;",
605 output: "var x = /* comment */ 5; // EOL comment\nvar y = 6;",
606 options: [{ ignoreEOLComments: true }],
607 errors: [{
608 messageId: "multipleSpaces",
609 data: { displayValue: "/* comment */" },
610 type: "Block"
611 }]
612 },
613 {
614 code: "var x = /* comment */ 5; /* EOL comment */",
615 output: "var x = /* comment */ 5; /* EOL comment */",
616 options: [{ ignoreEOLComments: true }],
617 errors: [{
618 messageId: "multipleSpaces",
619 data: { displayValue: "5" },
620 type: "Numeric"
621 }]
622 },
623 {
624 code: "var x = /* comment */ 5; /* EOL comment */\nvar y = 6;",
625 output: "var x = /* comment */ 5; /* EOL comment */\nvar y = 6;",
626 options: [{ ignoreEOLComments: true }],
627 errors: [{
628 messageId: "multipleSpaces",
629 data: { displayValue: "5" },
630 type: "Numeric"
631 }]
632 },
633 {
634 code: "var x = /*comment without spaces*/ 5;",
635 output: "var x = /*comment without spaces*/ 5;",
636 options: [{ ignoreEOLComments: true }],
637 errors: [{
638 messageId: "multipleSpaces",
639 data: { displayValue: "/*comment with...*/" },
640 type: "Block"
641 }]
642 },
643 {
644 code: "var x = 5; //comment without spaces",
645 output: "var x = 5; //comment without spaces",
646 options: [{ ignoreEOLComments: false }],
647 errors: [{
648 messageId: "multipleSpaces",
649 data: { displayValue: "//comment with..." },
650 type: "Line"
651 }]
652 },
653 {
654 code: "var x = 5; /*comment without spaces*/",
655 output: "var x = 5; /*comment without spaces*/",
656 options: [{ ignoreEOLComments: false }],
657 errors: [{
658 messageId: "multipleSpaces",
659 data: { displayValue: "/*comment with...*/" },
660 type: "Block"
661 }]
662 },
663 {
664 code: "var x = 5; /*comment\n without spaces*/",
665 output: "var x = 5; /*comment\n without spaces*/",
666 options: [{ ignoreEOLComments: false }],
667 errors: [{
668 messageId: "multipleSpaces",
669 data: { displayValue: "/*comment...*/" },
670 type: "Block",
671 column: 11,
672 endColumn: 13
673 }]
674 },
675 {
676 code: "foo\n\f bar + baz",
677 output: "foo\n\f bar + baz",
678 errors: [{
679 messageId: "multipleSpaces",
680 data: { displayValue: "+" },
681 type: "Punctuator"
682 }]
683 }
684 ]
685 });