]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-trailing-spaces.js
first commit
[pve-eslint.git] / eslint / tests / lib / rules / no-trailing-spaces.js
1 /**
2 * @fileoverview Disallow trailing spaces at the end of lines.
3 * @author Nodeca Team <https://github.com/nodeca>
4 */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Requirements
9 //------------------------------------------------------------------------------
10
11 const rule = require("../../../lib/rules/no-trailing-spaces"),
12 { RuleTester } = require("../../../lib/rule-tester");
13
14 //------------------------------------------------------------------------------
15 // Tests
16 //------------------------------------------------------------------------------
17
18 const ruleTester = new RuleTester();
19
20 ruleTester.run("no-trailing-spaces", rule, {
21
22 valid: [
23 {
24 code: "var a = 5;",
25 options: [{}]
26 },
27 {
28 code: "var a = 5,\n b = 3;",
29 options: [{}]
30 },
31 "var a = 5;",
32 "var a = 5,\n b = 3;",
33 {
34 code: "var a = 5,\n b = 3;",
35 options: [{ skipBlankLines: true }]
36 },
37 {
38 code: " ",
39 options: [{ skipBlankLines: true }]
40 },
41 {
42 code: "\t",
43 options: [{ skipBlankLines: true }]
44 },
45 {
46 code: " \n var c = 1;",
47 options: [{ skipBlankLines: true }]
48 },
49 {
50 code: "\t\n\tvar c = 2;",
51 options: [{ skipBlankLines: true }]
52 },
53 {
54 code: "\n var c = 3;",
55 options: [{ skipBlankLines: true }]
56 },
57 {
58 code: "\n\tvar c = 4;",
59 options: [{ skipBlankLines: true }]
60 },
61 {
62 code: "let str = `${a}\n \n${b}`;",
63 parserOptions: { ecmaVersion: 6 }
64 },
65 {
66 code: "let str = `${a}\n \n${b}`;\n \n ",
67 options: [{ skipBlankLines: true }],
68 parserOptions: { ecmaVersion: 6 }
69 },
70 {
71 code: "// Trailing comment test. ",
72 options: [{ ignoreComments: true }]
73 },
74 {
75 code: "// Trailing comment test.",
76 options: [{ ignoreComments: false }]
77 },
78 {
79 code: "// Trailing comment test.",
80 options: []
81 },
82 {
83 code: "/* \nTrailing comments test. \n*/",
84 options: [{ ignoreComments: true }]
85 },
86 {
87 code: "#!/usr/bin/env node ",
88 options: [{ ignoreComments: true }]
89 },
90 {
91 code: "/* \n */ // ",
92 options: [{ ignoreComments: true }]
93 },
94 {
95 code: "/* \n */ /* \n */",
96 options: [{ ignoreComments: true }]
97 }
98 ],
99
100 invalid: [
101 {
102 code:
103 "var short2 = true;\r\n" +
104 "\r\n" +
105 "module.exports = {\r\n" +
106 " short: short, \r\n" +
107 " short2: short\r\n" +
108 "}",
109 output:
110 "var short2 = true;\r\n" +
111 "\r\n" +
112 "module.exports = {\r\n" +
113 " short: short,\r\n" +
114 " short2: short\r\n" +
115 "}",
116 errors: [{
117 messageId: "trailingSpace",
118 type: "Program"
119 }]
120 },
121 {
122 code:
123 "var short2 = true;\n" +
124 "\r\n" +
125 "module.exports = {\r\n" +
126 " short: short, \r\n" +
127 " short2: short\n" +
128 "}",
129 output:
130 "var short2 = true;\n" +
131 "\r\n" +
132 "module.exports = {\r\n" +
133 " short: short,\r\n" +
134 " short2: short\n" +
135 "}",
136 errors: [{
137 messageId: "trailingSpace",
138 type: "Program"
139 }]
140 },
141 {
142 code:
143 "var short2 = true;\n" +
144 "\n" +
145 "module.exports = {\n" +
146 " short: short, \n" +
147 " short2: short\n" +
148 "}\n",
149 output:
150 "var short2 = true;\n" +
151 "\n" +
152 "module.exports = {\n" +
153 " short: short,\n" +
154 " short2: short\n" +
155 "}\n",
156 errors: [{
157 messageId: "trailingSpace",
158 type: "Program"
159 }]
160 },
161 {
162 code:
163 "var short2 = true;\n" +
164 "\n" +
165 "module.exports = {\n" +
166 " short, \n" +
167 " short2\n" +
168 "}\n",
169 output:
170 "var short2 = true;\n" +
171 "\n" +
172 "module.exports = {\n" +
173 " short,\n" +
174 " short2\n" +
175 "}\n",
176 parserOptions: { ecmaVersion: 6 },
177 errors: [{
178 messageId: "trailingSpace",
179 type: "Program"
180 }]
181 },
182 {
183 code:
184 "\n" +
185 "measAr.push(\"<dl></dl>\", \n" +
186 " \" </dt><dd class ='pta-res'>\");",
187 output:
188 "\n" +
189 "measAr.push(\"<dl></dl>\",\n" +
190 " \" </dt><dd class ='pta-res'>\");",
191 errors: [{
192 messageId: "trailingSpace",
193 type: "Program"
194 }]
195 },
196 {
197 code:
198 "measAr.push(\"<dl></dl>\", \n" +
199 " \" </dt><dd class ='pta-res'>\");",
200 output:
201 "measAr.push(\"<dl></dl>\",\n" +
202 " \" </dt><dd class ='pta-res'>\");",
203 errors: [{
204 messageId: "trailingSpace",
205 type: "Program"
206 }]
207 },
208 {
209 code: "var a = 5; \n",
210 output: "var a = 5;\n",
211 errors: [{
212 messageId: "trailingSpace",
213 type: "Program"
214 }]
215 },
216 {
217 code: "var a = 5; \n b = 3; ",
218 output: "var a = 5;\n b = 3;",
219 errors: [{
220 messageId: "trailingSpace",
221 type: "Program"
222 }, {
223 messageId: "trailingSpace",
224 type: "Program"
225 }]
226 },
227 {
228 code: "var a = 5; \n\n b = 3; ",
229 output: "var a = 5;\n\n b = 3;",
230 errors: [{
231 messageId: "trailingSpace",
232 type: "Program"
233 }, {
234 messageId: "trailingSpace",
235 type: "Program"
236 }]
237 },
238 {
239 code: "var a = 5;\t\n b = 3;",
240 output: "var a = 5;\n b = 3;",
241 errors: [{
242 messageId: "trailingSpace",
243 type: "Program"
244 }]
245 },
246 {
247 code: " \n var c = 1;",
248 output: "\n var c = 1;",
249 errors: [{
250 messageId: "trailingSpace",
251 type: "Program"
252 }]
253 },
254 {
255 code: "\t\n\tvar c = 2;",
256 output: "\n\tvar c = 2;",
257 errors: [{
258 messageId: "trailingSpace",
259 type: "Program"
260 }]
261 },
262 {
263 code: "var a = 5; \n",
264 output: "var a = 5;\n",
265 options: [{}],
266 errors: [{
267 messageId: "trailingSpace",
268 type: "Program"
269 }]
270 },
271 {
272 code: "var a = 5; \n b = 3; ",
273 output: "var a = 5;\n b = 3;",
274 options: [{}],
275 errors: [{
276 messageId: "trailingSpace",
277 type: "Program",
278 line: 1,
279 column: 11,
280 endLine: 1,
281 endColumn: 12
282 }, {
283 messageId: "trailingSpace",
284 type: "Program",
285 line: 2,
286 column: 8,
287 endLine: 2,
288 endColumn: 9
289 }]
290 },
291 {
292 code: "var a = 5;\t\n b = 3;",
293 output: "var a = 5;\n b = 3;",
294 options: [{}],
295 errors: [{
296 messageId: "trailingSpace",
297 type: "Program",
298 line: 1,
299 column: 11,
300 endLine: 1,
301 endColumn: 12
302 }]
303 },
304 {
305 code: " \n var c = 1;",
306 output: "\n var c = 1;",
307 options: [{}],
308 errors: [{
309 messageId: "trailingSpace",
310 type: "Program",
311 line: 1,
312 column: 1,
313 endLine: 1,
314 endColumn: 6
315 }]
316 },
317 {
318 code: "\t\n\tvar c = 2;",
319 output: "\n\tvar c = 2;",
320 options: [{}],
321 errors: [{
322 messageId: "trailingSpace",
323 type: "Program"
324 }]
325 },
326 {
327 code: "var a = 'bar'; \n \n\t",
328 output: "var a = 'bar';\n \n\t",
329 options: [{
330 skipBlankLines: true
331 }],
332 errors: [{
333 messageId: "trailingSpace",
334 type: "Program",
335 line: 1,
336 column: 15, // there are invalid spaces in columns 15 and 16
337 endLine: 1,
338 endColumn: 17
339 }]
340 },
341 {
342 code: "var a = 'foo'; \nvar b = 'bar'; \n \n",
343 output: "var a = 'foo';\nvar b = 'bar';\n \n",
344 options: [{
345 skipBlankLines: true
346 }],
347 errors: [
348 {
349 messageId: "trailingSpace",
350 type: "Program",
351 line: 1,
352 column: 15,
353 endLine: 1,
354 endColumn: 18
355 },
356 {
357 messageId: "trailingSpace",
358 type: "Program",
359 line: 2,
360 column: 15,
361 endLine: 2,
362 endColumn: 17
363 }
364 ]
365 },
366 {
367 code: "let str = `${a}\n \n${b}`; \n",
368 output: "let str = `${a}\n \n${b}`;\n",
369 parserOptions: { ecmaVersion: 6 },
370 errors: [{
371 messageId: "trailingSpace",
372 type: "Program",
373 line: 3,
374 column: 7,
375 endLine: 3,
376 endColumn: 9
377 }]
378 },
379 {
380 code: "let str = `\n${a}\n \n${b}`; \n\t",
381 output: "let str = `\n${a}\n \n${b}`;\n",
382 parserOptions: { ecmaVersion: 6 },
383 errors: [
384 {
385 messageId: "trailingSpace",
386 type: "Program",
387 line: 4,
388 column: 7,
389 endLine: 4,
390 endColumn: 9
391 },
392 {
393 messageId: "trailingSpace",
394 type: "Program",
395 line: 5,
396 column: 1,
397 endLine: 5,
398 endColumn: 2
399 }
400 ]
401 },
402 {
403 code: "let str = ` \n ${a}\n \n${b}`; \n",
404 output: "let str = ` \n ${a}\n \n${b}`;\n",
405 parserOptions: { ecmaVersion: 6 },
406 errors: [
407 {
408 messageId: "trailingSpace",
409 type: "Program",
410 line: 4,
411 column: 7,
412 endLine: 4,
413 endColumn: 9
414 }
415 ]
416 },
417 {
418 code: "let str = `${a}\n \n${b}`; \n \n",
419 output: "let str = `${a}\n \n${b}`;\n \n",
420 options: [{
421 skipBlankLines: true
422 }],
423 parserOptions: { ecmaVersion: 6 },
424 errors: [
425 {
426 messageId: "trailingSpace",
427 type: "Program",
428 line: 3,
429 column: 7,
430 endLine: 3,
431 endColumn: 9
432 }
433 ]
434 },
435
436 // https://github.com/eslint/eslint/issues/6933
437 {
438 code: " \nabcdefg ",
439 output: " \nabcdefg",
440 options: [{ skipBlankLines: true }],
441 errors: [
442 {
443 messageId: "trailingSpace",
444 type: "Program",
445 line: 2,
446 column: 8,
447 endLine: 2,
448 endColumn: 9
449 }
450 ]
451 },
452 {
453 code: " \nabcdefg ",
454 output: "\nabcdefg",
455 errors: [
456 {
457 messageId: "trailingSpace",
458 type: "Program",
459 line: 1,
460 column: 1,
461 endLine: 1,
462 endColumn: 5
463 },
464 {
465 messageId: "trailingSpace",
466 type: "Program",
467 line: 2,
468 column: 8,
469 endLine: 2,
470 endColumn: 9
471 }
472 ]
473 },
474
475 // Tests for ignoreComments flag.
476 {
477 code: "var foo = 'bar'; ",
478 output: "var foo = 'bar';",
479 options: [{ ignoreComments: true }],
480 errors: [
481 {
482 messageId: "trailingSpace",
483 type: "Program",
484 line: 1,
485 column: 17,
486 endLine: 1,
487 endColumn: 18
488 }
489 ]
490 },
491 {
492 code: "/* */ ",
493 output: "/* */",
494 options: [{ ignoreComments: true }],
495 errors: [
496 {
497 messageId: "trailingSpace",
498 type: "Program",
499 line: 1,
500 column: 6
501 }
502 ]
503 },
504 {
505 code: "/* */foo ",
506 output: "/* */foo",
507 options: [{ ignoreComments: true }],
508 errors: [
509 {
510 messageId: "trailingSpace",
511 type: "Program",
512 line: 1,
513 column: 9
514 }
515 ]
516 },
517 {
518 code: "/* \n */ ",
519 output: "/* \n */",
520 options: [{ ignoreComments: true }],
521 errors: [
522 {
523 messageId: "trailingSpace",
524 type: "Program",
525 line: 2,
526 column: 4
527 }
528 ]
529 },
530 {
531 code: "/* \n */ foo ",
532 output: "/* \n */ foo",
533 options: [{ ignoreComments: true }],
534 errors: [
535 {
536 messageId: "trailingSpace",
537 type: "Program",
538 line: 2,
539 column: 8
540 }
541 ]
542 },
543 {
544 code: "// Trailing comment test. ",
545 output: "// Trailing comment test.",
546 options: [{ ignoreComments: false }],
547 errors: [
548 {
549 messageId: "trailingSpace",
550 type: "Program",
551 line: 1,
552 column: 26,
553 endLine: 1,
554 endColumn: 27
555 }
556 ]
557 },
558 {
559 code: "/* \nTrailing comments test. \n*/",
560 output: "/*\nTrailing comments test.\n*/",
561 options: [{ ignoreComments: false }],
562 errors: [
563 {
564 messageId: "trailingSpace",
565 type: "Program",
566 line: 1,
567 column: 3,
568 endLine: 1,
569 endColumn: 4
570 },
571 {
572 messageId: "trailingSpace",
573 type: "Program",
574 line: 2,
575 column: 24,
576 endLine: 2,
577 endColumn: 25
578 }
579 ]
580 },
581 {
582 code: "#!/usr/bin/env node ",
583 output: "#!/usr/bin/env node",
584 options: [{ ignoreComments: false }],
585 errors: [
586 {
587 messageId: "trailingSpace",
588 type: "Program",
589 line: 1,
590 column: 20,
591 endLine: 1,
592 endColumn: 21
593 }
594 ]
595 },
596 {
597 code: "// Trailing comment default test. ",
598 output: "// Trailing comment default test.",
599 options: [],
600 errors: [
601 {
602 messageId: "trailingSpace",
603 type: "Program",
604 line: 1,
605 column: 34,
606 endLine: 1,
607 endColumn: 35
608 }
609 ]
610 }
611 ]
612 });