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