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