]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Tests for concise-object rule | |
3 | * @author Jamund Ferguson <http://www.jamund.com> | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
12 | const rule = require("../../../lib/rules/object-shorthand"), | |
13 | { RuleTester } = require("../../../lib/rule-tester"); | |
56c4a2cb | 14 | const { unIndent } = require("../../_utils"); |
eb39fafa DC |
15 | |
16 | //------------------------------------------------------------------------------ | |
17 | // Tests | |
18 | //------------------------------------------------------------------------------ | |
19 | ||
20 | const PROPERTY_ERROR = { messageId: "expectedPropertyShorthand", type: "Property" }; | |
21 | const METHOD_ERROR = { messageId: "expectedMethodShorthand", type: "Property" }; | |
22 | const LONGFORM_PROPERTY_ERROR = { messageId: "expectedPropertyLongform", type: "Property" }; | |
23 | const LONGFORM_METHOD_ERROR = { messageId: "expectedMethodLongform", type: "Property" }; | |
24 | const LONGFORM_METHOD_STRING_LITERAL_ERROR = { messageId: "expectedLiteralMethodLongform", type: "Property" }; | |
25 | const ALL_SHORTHAND_ERROR = { messageId: "expectedAllPropertiesShorthanded", type: "ObjectExpression" }; | |
26 | const MIXED_SHORTHAND_ERROR = { messageId: "unexpectedMix", type: "ObjectExpression" }; | |
27 | ||
28 | const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018 } }); | |
29 | ||
30 | ruleTester.run("object-shorthand", rule, { | |
31 | valid: [ | |
32 | "var x = {y() {}}", | |
33 | "var x = {y}", | |
34 | "var x = {a: b}", | |
35 | "var x = {a: 'a'}", | |
36 | "var x = {'a': 'a'}", | |
37 | "var x = {'a': b}", | |
38 | "var x = {y(x) {}}", | |
39 | "var {x,y,z} = x", | |
40 | "var {x: {y}} = z", | |
41 | "var x = {*x() {}}", | |
42 | "var x = {x: y}", | |
43 | "var x = {x: y, y: z}", | |
44 | "var x = {x: y, y: z, z: 'z'}", | |
45 | "var x = {x() {}, y: z, l(){}}", | |
46 | "var x = {x: y, y: z, a: b}", | |
47 | "var x = {x: y, y: z, 'a': b}", | |
48 | "var x = {x: y, y() {}, z: a}", | |
49 | "var x = {[y]: y}", | |
50 | "doSomething({x: y})", | |
51 | "doSomething({'x': y})", | |
52 | "doSomething({x: 'x'})", | |
53 | "doSomething({'x': 'x'})", | |
54 | "doSomething({y() {}})", | |
55 | "doSomething({x: y, y() {}})", | |
56 | "doSomething({y() {}, z: a})", | |
57 | "!{ a: function a(){} };", | |
58 | ||
59 | // arrow functions are still alright by default | |
60 | "var x = {y: (x)=>x}", | |
61 | "doSomething({y: (x)=>x})", | |
62 | "var x = {y: (x)=>x, y: a}", | |
63 | "doSomething({x, y: (x)=>x})", | |
64 | "({ foo: x => { return; }})", | |
65 | "({ foo: (x) => { return; }})", | |
66 | "({ foo: () => { return; }})", | |
67 | ||
68 | // getters and setters are ok | |
69 | "var x = {get y() {}}", | |
70 | "var x = {set y(z) {}}", | |
71 | "var x = {get y() {}, set y(z) {}}", | |
72 | "doSomething({get y() {}})", | |
73 | "doSomething({set y(z) {}})", | |
74 | "doSomething({get y() {}, set y(z) {}})", | |
75 | ||
76 | // object literal computed properties | |
77 | { | |
78 | code: "var x = {[y]: y}", | |
79 | options: ["properties"] | |
80 | }, | |
81 | { | |
82 | code: "var x = {['y']: 'y'}", | |
83 | options: ["properties"] | |
84 | }, | |
85 | { | |
86 | code: "var x = {['y']: y}", | |
87 | options: ["properties"] | |
88 | }, | |
89 | ||
90 | // object literal computed methods | |
91 | { | |
92 | code: "var x = {[y]() {}}", | |
93 | options: ["methods"] | |
94 | }, | |
95 | { | |
96 | code: "var x = {[y]: function x() {}}", | |
97 | options: ["methods"] | |
98 | }, | |
99 | { | |
100 | code: "var x = {[y]: y}", | |
101 | options: ["methods"] | |
102 | }, | |
103 | ||
104 | // options | |
105 | { | |
106 | code: "var x = {y() {}}", | |
107 | options: ["methods"] | |
108 | }, | |
109 | { | |
110 | code: "var x = {x, y() {}, a:b}", | |
111 | options: ["methods"] | |
112 | }, | |
113 | { | |
114 | code: "var x = {y}", | |
115 | options: ["properties"] | |
116 | }, | |
117 | { | |
118 | code: "var x = {y: {b}}", | |
119 | options: ["properties"] | |
120 | }, | |
121 | { | |
122 | code: "var x = {a: n, c: d, f: g}", | |
123 | options: ["never"] | |
124 | }, | |
125 | { | |
126 | code: "var x = {a: function(){}, b: {c: d}}", | |
127 | options: ["never"] | |
128 | }, | |
129 | ||
130 | // ignoreConstructors | |
131 | { | |
132 | code: "var x = {ConstructorFunction: function(){}, a: b}", | |
133 | options: ["always", { ignoreConstructors: true }] | |
134 | }, | |
135 | { | |
136 | code: "var x = {_ConstructorFunction: function(){}, a: b}", | |
137 | options: ["always", { ignoreConstructors: true }] | |
138 | }, | |
139 | { | |
140 | code: "var x = {$ConstructorFunction: function(){}, a: b}", | |
141 | options: ["always", { ignoreConstructors: true }] | |
142 | }, | |
143 | { | |
144 | code: "var x = {__ConstructorFunction: function(){}, a: b}", | |
145 | options: ["always", { ignoreConstructors: true }] | |
146 | }, | |
147 | { | |
148 | code: "var x = {_0ConstructorFunction: function(){}, a: b}", | |
149 | options: ["always", { ignoreConstructors: true }] | |
150 | }, | |
151 | { | |
152 | code: "var x = {notConstructorFunction(){}, b: c}", | |
153 | options: ["always", { ignoreConstructors: true }] | |
154 | }, | |
155 | { | |
156 | code: "var x = {ConstructorFunction: function(){}, a: b}", | |
157 | options: ["methods", { ignoreConstructors: true }] | |
158 | }, | |
159 | { | |
160 | code: "var x = {_ConstructorFunction: function(){}, a: b}", | |
161 | options: ["methods", { ignoreConstructors: true }] | |
162 | }, | |
163 | { | |
164 | code: "var x = {$ConstructorFunction: function(){}, a: b}", | |
165 | options: ["methods", { ignoreConstructors: true }] | |
166 | }, | |
167 | { | |
168 | code: "var x = {__ConstructorFunction: function(){}, a: b}", | |
169 | options: ["methods", { ignoreConstructors: true }] | |
170 | }, | |
171 | { | |
172 | code: "var x = {_0ConstructorFunction: function(){}, a: b}", | |
173 | options: ["methods", { ignoreConstructors: true }] | |
174 | }, | |
175 | { | |
176 | code: "var x = {notConstructorFunction(){}, b: c}", | |
177 | options: ["methods", { ignoreConstructors: true }] | |
178 | }, | |
179 | { | |
180 | code: "var x = {ConstructorFunction: function(){}, a: b}", | |
181 | options: ["never"] | |
182 | }, | |
183 | { | |
184 | code: "var x = {notConstructorFunction: function(){}, b: c}", | |
185 | options: ["never"] | |
186 | }, | |
187 | ||
188 | // avoidQuotes | |
189 | { | |
190 | code: "var x = {'a': function(){}}", | |
191 | options: ["always", { avoidQuotes: true }] | |
192 | }, | |
193 | { | |
194 | code: "var x = {['a']: function(){}}", | |
195 | options: ["methods", { avoidQuotes: true }] | |
196 | }, | |
197 | { | |
198 | code: "var x = {'y': y}", | |
199 | options: ["properties", { avoidQuotes: true }] | |
200 | }, | |
201 | ||
202 | // ignore object shorthand | |
203 | { | |
204 | code: "let {a, b} = o;", | |
205 | options: ["never"] | |
206 | }, | |
207 | { | |
208 | code: "var x = {foo: foo, bar: bar, ...baz}", | |
209 | options: ["never"], | |
210 | parserOptions: { ecmaVersion: 2018 } | |
211 | }, | |
212 | ||
213 | // consistent | |
214 | { | |
215 | code: "var x = {a: a, b: b}", | |
216 | options: ["consistent"] | |
217 | }, | |
218 | { | |
219 | code: "var x = {a: b, c: d, f: g}", | |
220 | options: ["consistent"] | |
221 | }, | |
222 | { | |
223 | code: "var x = {a, b}", | |
224 | options: ["consistent"] | |
225 | }, | |
226 | { | |
227 | code: "var x = {a, b, get test() { return 1; }}", | |
228 | options: ["consistent"] | |
229 | }, | |
230 | { | |
231 | code: "var x = {...bar}", | |
232 | options: ["consistent-as-needed"], | |
233 | parserOptions: { ecmaVersion: 2018 } | |
234 | }, | |
235 | { | |
236 | code: "var x = {foo, bar, ...baz}", | |
237 | options: ["consistent"], | |
238 | parserOptions: { ecmaVersion: 2018 } | |
239 | }, | |
240 | { | |
241 | code: "var x = {bar: baz, ...qux}", | |
242 | options: ["consistent"], | |
243 | parserOptions: { ecmaVersion: 2018 } | |
244 | }, | |
245 | { | |
246 | code: "var x = {...foo, bar: bar, baz: baz}", | |
247 | options: ["consistent"], | |
248 | parserOptions: { ecmaVersion: 2018 } | |
249 | }, | |
250 | ||
251 | // consistent-as-needed | |
252 | { | |
253 | code: "var x = {a, b}", | |
254 | options: ["consistent-as-needed"] | |
255 | }, | |
256 | { | |
257 | code: "var x = {a, b, get test(){return 1;}}", | |
258 | options: ["consistent-as-needed"] | |
259 | }, | |
260 | { | |
261 | code: "var x = {0: 'foo'}", | |
262 | options: ["consistent-as-needed"] | |
263 | }, | |
264 | { | |
265 | code: "var x = {'key': 'baz'}", | |
266 | options: ["consistent-as-needed"] | |
267 | }, | |
268 | { | |
269 | code: "var x = {foo: 'foo'}", | |
270 | options: ["consistent-as-needed"] | |
271 | }, | |
272 | { | |
273 | code: "var x = {[foo]: foo}", | |
274 | options: ["consistent-as-needed"] | |
275 | }, | |
276 | { | |
277 | code: "var x = {foo: function foo() {}}", | |
278 | options: ["consistent-as-needed"] | |
279 | }, | |
280 | { | |
281 | code: "var x = {[foo]: 'foo'}", | |
282 | options: ["consistent-as-needed"] | |
283 | }, | |
284 | { | |
285 | code: "var x = {bar, ...baz}", | |
286 | options: ["consistent-as-needed"], | |
287 | parserOptions: { ecmaVersion: 2018 } | |
288 | }, | |
289 | { | |
290 | code: "var x = {bar: baz, ...qux}", | |
291 | options: ["consistent-as-needed"], | |
292 | parserOptions: { ecmaVersion: 2018 } | |
293 | }, | |
294 | { | |
295 | code: "var x = {...foo, bar, baz}", | |
296 | options: ["consistent-as-needed"], | |
297 | parserOptions: { ecmaVersion: 2018 } | |
298 | }, | |
299 | ||
300 | // avoidExplicitReturnArrows | |
301 | { | |
302 | code: "({ x: () => foo })", | |
303 | options: ["always", { avoidExplicitReturnArrows: false }] | |
304 | }, | |
305 | { | |
306 | code: "({ x: () => { return; } })", | |
307 | options: ["always", { avoidExplicitReturnArrows: false }] | |
308 | }, | |
309 | { | |
310 | code: "({ x: () => foo })", | |
311 | options: ["always", { avoidExplicitReturnArrows: true }] | |
312 | }, | |
313 | { | |
314 | code: "({ x() { return; } })", | |
315 | options: ["always", { avoidExplicitReturnArrows: true }] | |
316 | }, | |
317 | { | |
318 | code: "({ x() { return; }, y() { return; } })", | |
319 | options: ["always", { avoidExplicitReturnArrows: true }] | |
320 | }, | |
321 | { | |
322 | code: "({ x() { return; }, y: () => foo })", | |
323 | options: ["always", { avoidExplicitReturnArrows: true }] | |
324 | }, | |
325 | { | |
326 | code: "({ x: () => foo, y() { return; } })", | |
327 | options: ["always", { avoidExplicitReturnArrows: true }] | |
328 | }, | |
329 | { | |
330 | code: "({ x: () => { this; } })", | |
331 | options: ["always", { avoidExplicitReturnArrows: true }] | |
332 | }, | |
333 | { | |
334 | code: "function foo() { ({ x: () => { arguments; } }) }", | |
335 | options: ["always", { avoidExplicitReturnArrows: true }] | |
336 | }, | |
337 | { | |
338 | code: ` | |
339 | class Foo extends Bar { | |
340 | constructor() { | |
341 | var foo = { x: () => { super(); } }; | |
342 | } | |
343 | } | |
344 | `, | |
345 | options: ["always", { avoidExplicitReturnArrows: true }] | |
346 | }, | |
347 | { | |
348 | code: ` | |
349 | class Foo extends Bar { | |
350 | baz() { | |
351 | var foo = { x: () => { super.baz(); } }; | |
352 | } | |
353 | } | |
354 | `, | |
355 | options: ["always", { avoidExplicitReturnArrows: true }] | |
356 | }, | |
357 | { | |
358 | code: ` | |
359 | function foo() { | |
360 | var x = { x: () => { new.target; } }; | |
361 | } | |
362 | `, | |
363 | options: ["always", { avoidExplicitReturnArrows: true }] | |
364 | }, | |
365 | { | |
366 | code: ` | |
367 | function foo() { | |
368 | var x = { | |
369 | x: () => { | |
370 | var y = () => { this; }; | |
371 | } | |
372 | }; | |
373 | } | |
374 | `, | |
375 | options: ["always", { avoidExplicitReturnArrows: true }] | |
376 | }, | |
377 | { | |
378 | code: ` | |
379 | function foo() { | |
380 | var x = { | |
381 | x: () => { | |
382 | var y = () => { this; }; | |
383 | function foo() { this; } | |
384 | } | |
385 | }; | |
386 | } | |
387 | `, | |
388 | options: ["always", { avoidExplicitReturnArrows: true }] | |
389 | }, | |
390 | { | |
391 | code: ` | |
392 | function foo() { | |
393 | var x = { | |
394 | x: () => { | |
395 | return { y: () => { this; } }; | |
396 | } | |
397 | }; | |
398 | } | |
399 | `, | |
400 | options: ["always", { avoidExplicitReturnArrows: true }] | |
401 | }, | |
402 | { | |
403 | code: "({ [foo.bar]: () => {} })", | |
404 | options: ["always", { ignoreConstructors: true }] | |
405 | } | |
406 | ], | |
407 | invalid: [ | |
408 | { | |
409 | code: "var x = {x: x}", | |
410 | output: "var x = {x}", | |
411 | errors: [PROPERTY_ERROR] | |
412 | }, | |
413 | { | |
414 | code: "var x = {'x': x}", | |
415 | output: "var x = {x}", | |
416 | errors: [PROPERTY_ERROR] | |
417 | }, | |
418 | { | |
419 | code: "var x = {y: y, x: x}", | |
420 | output: "var x = {y, x}", | |
421 | errors: [PROPERTY_ERROR, PROPERTY_ERROR] | |
422 | }, | |
423 | { | |
424 | code: "var x = {y: z, x: x, a: b}", | |
425 | output: "var x = {y: z, x, a: b}", | |
426 | errors: [PROPERTY_ERROR] | |
427 | }, | |
428 | { | |
429 | code: "var x = {y: z,\n x: x,\n a: b\n // comment \n}", | |
430 | output: "var x = {y: z,\n x,\n a: b\n // comment \n}", | |
431 | errors: [PROPERTY_ERROR] | |
432 | }, | |
433 | { | |
434 | code: "var x = {y: z,\n a: b,\n // comment \nf: function() {}}", | |
435 | output: "var x = {y: z,\n a: b,\n // comment \nf() {}}", | |
436 | errors: [METHOD_ERROR] | |
437 | }, | |
438 | { | |
439 | code: "var x = {a: b,\n/* comment */\ny: y\n }", | |
440 | output: "var x = {a: b,\n/* comment */\ny\n }", | |
441 | errors: [PROPERTY_ERROR] | |
442 | }, | |
443 | { | |
444 | code: "var x = {\n a: b,\n /* comment */\n y: y\n}", | |
445 | output: "var x = {\n a: b,\n /* comment */\n y\n}", | |
446 | errors: [PROPERTY_ERROR] | |
447 | }, | |
448 | { | |
449 | code: "var x = {\n f: function() {\n /* comment */\n a(b);\n }\n }", | |
450 | output: "var x = {\n f() {\n /* comment */\n a(b);\n }\n }", | |
451 | errors: [METHOD_ERROR] | |
452 | }, | |
453 | { | |
454 | code: "var x = {\n [f]: function() {\n /* comment */\n a(b);\n }\n }", | |
455 | output: "var x = {\n [f]() {\n /* comment */\n a(b);\n }\n }", | |
456 | errors: [METHOD_ERROR] | |
457 | }, | |
458 | { | |
459 | code: "var x = {\n f: function*() {\n /* comment */\n a(b);\n }\n }", | |
460 | output: "var x = {\n *f() {\n /* comment */\n a(b);\n }\n }", | |
461 | errors: [METHOD_ERROR] | |
462 | }, | |
463 | { | |
464 | code: "var x = {\n f: /* comment */ function() {\n }\n }", | |
465 | output: null, | |
466 | errors: [METHOD_ERROR] | |
467 | }, | |
468 | { | |
469 | code: "var x = {\n f /* comment */: function() {\n }\n }", | |
470 | output: null, | |
471 | errors: [METHOD_ERROR] | |
472 | }, | |
473 | { | |
474 | code: "var x = {y: function() {}}", | |
475 | output: "var x = {y() {}}", | |
476 | errors: [METHOD_ERROR] | |
477 | }, | |
478 | { | |
479 | code: "var x = {y: function*() {}}", | |
480 | output: "var x = {*y() {}}", | |
481 | errors: [METHOD_ERROR] | |
482 | }, | |
483 | { | |
484 | code: "var x = {x: y, y: z, a: a}", | |
485 | output: "var x = {x: y, y: z, a}", | |
486 | errors: [PROPERTY_ERROR] | |
487 | }, | |
488 | { | |
489 | code: "var x = {ConstructorFunction: function(){}, a: b}", | |
490 | output: "var x = {ConstructorFunction(){}, a: b}", | |
491 | errors: [METHOD_ERROR] | |
492 | }, | |
493 | { | |
494 | code: "var x = {x: y, y: z, a: function(){}, b() {}}", | |
495 | output: "var x = {x: y, y: z, a(){}, b() {}}", | |
496 | errors: [METHOD_ERROR] | |
497 | }, | |
498 | { | |
499 | code: "var x = {x: x, y: function() {}}", | |
500 | output: "var x = {x, y() {}}", | |
501 | errors: [PROPERTY_ERROR, METHOD_ERROR] | |
502 | }, | |
503 | { | |
504 | code: "doSomething({x: x})", | |
505 | output: "doSomething({x})", | |
506 | errors: [PROPERTY_ERROR] | |
507 | }, | |
508 | { | |
509 | code: "doSomething({'x': x})", | |
510 | output: "doSomething({x})", | |
511 | errors: [PROPERTY_ERROR] | |
512 | }, | |
513 | { | |
514 | code: "doSomething({a: 'a', 'x': x})", | |
515 | output: "doSomething({a: 'a', x})", | |
516 | errors: [PROPERTY_ERROR] | |
517 | }, | |
518 | { | |
519 | code: "doSomething({y: function() {}})", | |
520 | output: "doSomething({y() {}})", | |
521 | errors: [METHOD_ERROR] | |
522 | }, | |
523 | { | |
524 | code: "doSomething({[y]: function() {}})", | |
525 | output: "doSomething({[y]() {}})", | |
526 | errors: [METHOD_ERROR] | |
527 | }, | |
528 | { | |
529 | code: "doSomething({['y']: function() {}})", | |
530 | output: "doSomething({['y']() {}})", | |
531 | errors: [METHOD_ERROR] | |
532 | }, | |
533 | { | |
534 | code: "({ foo: async function () {} })", | |
535 | output: "({ async foo () {} })", | |
536 | parserOptions: { ecmaVersion: 8 }, | |
537 | errors: [METHOD_ERROR] | |
538 | }, | |
539 | { | |
540 | code: "({ 'foo': async function() {} })", | |
541 | output: "({ async 'foo'() {} })", | |
542 | parserOptions: { ecmaVersion: 8 }, | |
543 | errors: [METHOD_ERROR] | |
544 | }, | |
545 | { | |
546 | code: "({ [foo]: async function() {} })", | |
547 | output: "({ async [foo]() {} })", | |
548 | parserOptions: { ecmaVersion: 8 }, | |
549 | errors: [METHOD_ERROR] | |
550 | }, | |
551 | { | |
552 | code: "({ [foo.bar]: function*() {} })", | |
553 | output: "({ *[foo.bar]() {} })", | |
554 | errors: [METHOD_ERROR] | |
555 | }, | |
556 | { | |
557 | code: "({ [foo ]: function() {} })", | |
558 | output: "({ [foo ]() {} })", | |
559 | errors: [METHOD_ERROR] | |
560 | }, | |
561 | { | |
562 | code: "({ [ foo ]: async function() {} })", | |
563 | output: "({ async [ foo ]() {} })", | |
564 | parserOptions: { ecmaVersion: 8 }, | |
565 | errors: [METHOD_ERROR] | |
566 | }, | |
567 | { | |
568 | code: "({ foo: function *() {} })", | |
569 | output: "({ *foo() {} })", | |
570 | errors: [METHOD_ERROR] | |
571 | }, | |
572 | { | |
573 | code: "({ [ foo ]: function() {} })", | |
574 | output: "({ [ foo ]() {} })", | |
575 | errors: [METHOD_ERROR] | |
576 | }, | |
577 | { | |
578 | code: "({ [ foo]: function() {} })", | |
579 | output: "({ [ foo]() {} })", | |
580 | errors: [METHOD_ERROR] | |
581 | }, | |
582 | ||
583 | // options | |
584 | { | |
585 | code: "var x = {y: function() {}}", | |
586 | output: "var x = {y() {}}", | |
587 | options: ["methods"], | |
588 | errors: [METHOD_ERROR] | |
589 | }, | |
590 | { | |
591 | code: "var x = {x, y() {}, z: function() {}}", | |
592 | output: "var x = {x, y() {}, z() {}}", | |
593 | options: ["methods"], | |
594 | errors: [METHOD_ERROR] | |
595 | }, | |
596 | { | |
597 | code: "var x = {ConstructorFunction: function(){}, a: b}", | |
598 | output: "var x = {ConstructorFunction(){}, a: b}", | |
599 | options: ["methods"], | |
600 | errors: [METHOD_ERROR] | |
601 | }, | |
602 | { | |
603 | code: "var x = {[y]: function() {}}", | |
604 | output: "var x = {[y]() {}}", | |
605 | options: ["methods"], | |
606 | errors: [METHOD_ERROR] | |
607 | }, | |
608 | { | |
609 | code: "({ [(foo)]: function() { return; } })", | |
610 | output: "({ [(foo)]() { return; } })", | |
611 | errors: [METHOD_ERROR] | |
612 | }, | |
613 | { | |
614 | code: "({ [(foo)]: async function() { return; } })", | |
615 | output: "({ async [(foo)]() { return; } })", | |
616 | parserOptions: { ecmaVersion: 8 }, | |
617 | errors: [METHOD_ERROR] | |
618 | }, | |
619 | { | |
620 | code: "({ [(((((((foo)))))))]: function() { return; } })", | |
621 | output: "({ [(((((((foo)))))))]() { return; } })", | |
622 | errors: [METHOD_ERROR] | |
623 | }, | |
624 | { | |
625 | code: "({ [(foo)]() { return; } })", | |
626 | output: "({ [(foo)]: function() { return; } })", | |
627 | options: ["never"], | |
628 | errors: [LONGFORM_METHOD_ERROR] | |
629 | }, | |
630 | { | |
631 | code: "({ async [(foo)]() { return; } })", | |
632 | output: "({ [(foo)]: async function() { return; } })", | |
633 | options: ["never"], | |
634 | parserOptions: { ecmaVersion: 8 }, | |
635 | errors: [LONGFORM_METHOD_ERROR] | |
636 | }, | |
637 | { | |
638 | code: "({ *[((foo))]() { return; } })", | |
639 | output: "({ [((foo))]: function*() { return; } })", | |
640 | options: ["never"], | |
641 | errors: [LONGFORM_METHOD_ERROR] | |
642 | }, | |
643 | { | |
644 | code: "({ [(((((((foo)))))))]() { return; } })", | |
645 | output: "({ [(((((((foo)))))))]: function() { return; } })", | |
646 | options: ["never"], | |
647 | errors: [LONGFORM_METHOD_ERROR] | |
648 | }, | |
649 | { | |
650 | code: "({ 'foo bar'() { return; } })", | |
651 | output: "({ 'foo bar': function() { return; } })", | |
652 | options: ["never"], | |
653 | errors: [LONGFORM_METHOD_ERROR] | |
654 | }, | |
655 | { | |
656 | code: "({ *foo() { return; } })", | |
657 | output: "({ foo: function*() { return; } })", | |
658 | options: ["never"], | |
659 | errors: [LONGFORM_METHOD_ERROR] | |
660 | }, | |
661 | { | |
662 | code: "({ async foo() { return; } })", | |
663 | output: "({ foo: async function() { return; } })", | |
664 | options: ["never"], | |
665 | parserOptions: { ecmaVersion: 8 }, | |
666 | errors: [LONGFORM_METHOD_ERROR] | |
667 | }, | |
668 | { | |
669 | code: "({ *['foo bar']() { return; } })", | |
670 | output: "({ ['foo bar']: function*() { return; } })", | |
671 | options: ["never"], | |
672 | parserOptions: { ecmaVersion: 8 }, | |
673 | errors: [LONGFORM_METHOD_ERROR] | |
674 | }, | |
675 | { | |
676 | code: "var x = {x: x}", | |
677 | output: "var x = {x}", | |
678 | options: ["properties"], | |
679 | errors: [PROPERTY_ERROR] | |
680 | }, | |
681 | { | |
682 | code: "var x = {a, b, c(){}, x: x}", | |
683 | output: "var x = {a, b, c(){}, x}", | |
684 | options: ["properties"], | |
685 | errors: [PROPERTY_ERROR] | |
686 | }, | |
687 | { | |
688 | code: "var x = {y() {}}", | |
689 | output: "var x = {y: function() {}}", | |
690 | options: ["never"], | |
691 | errors: [LONGFORM_METHOD_ERROR] | |
692 | }, | |
693 | { | |
694 | code: "var x = {*y() {}}", | |
695 | output: "var x = {y: function*() {}}", | |
696 | options: ["never"], | |
697 | errors: [LONGFORM_METHOD_ERROR] | |
698 | }, | |
699 | { | |
700 | code: "var x = {y}", | |
701 | output: "var x = {y: y}", | |
702 | options: ["never"], | |
703 | errors: [LONGFORM_PROPERTY_ERROR] | |
704 | }, | |
705 | { | |
706 | code: "var x = {y, a: b, *x(){}}", | |
707 | output: "var x = {y: y, a: b, x: function*(){}}", | |
708 | options: ["never"], | |
709 | errors: [LONGFORM_PROPERTY_ERROR, LONGFORM_METHOD_ERROR] | |
710 | }, | |
711 | { | |
712 | code: "var x = {y: {x}}", | |
713 | output: "var x = {y: {x: x}}", | |
714 | options: ["never"], | |
715 | errors: [LONGFORM_PROPERTY_ERROR] | |
716 | }, | |
717 | { | |
718 | code: "var x = {ConstructorFunction(){}, a: b}", | |
719 | output: "var x = {ConstructorFunction: function(){}, a: b}", | |
720 | options: ["never"], | |
721 | errors: [LONGFORM_METHOD_ERROR] | |
722 | }, | |
723 | { | |
724 | code: "var x = {notConstructorFunction(){}, b: c}", | |
725 | output: "var x = {notConstructorFunction: function(){}, b: c}", | |
726 | options: ["never"], | |
727 | errors: [LONGFORM_METHOD_ERROR] | |
728 | }, | |
729 | { | |
730 | code: "var x = {foo: foo, bar: baz, ...qux}", | |
731 | output: "var x = {foo, bar: baz, ...qux}", | |
732 | options: ["always"], | |
733 | parserOptions: { ecmaVersion: 2018 }, | |
734 | errors: [PROPERTY_ERROR] | |
735 | }, | |
736 | { | |
737 | code: "var x = {foo, bar: baz, ...qux}", | |
738 | output: "var x = {foo: foo, bar: baz, ...qux}", | |
739 | options: ["never"], | |
740 | parserOptions: { ecmaVersion: 2018 }, | |
741 | errors: [LONGFORM_PROPERTY_ERROR] | |
742 | }, | |
743 | ||
744 | // ignoreConstructors | |
745 | { | |
746 | code: "var x = {y: function() {}}", | |
747 | output: "var x = {y() {}}", | |
748 | options: ["methods", { ignoreConstructors: true }], | |
749 | errors: [METHOD_ERROR] | |
750 | }, | |
751 | { | |
752 | ||
753 | // https://github.com/eslint/eslint/issues/11595 | |
754 | code: "var x = {_y: function() {}}", | |
755 | output: "var x = {_y() {}}", | |
756 | options: ["methods", { ignoreConstructors: true }], | |
757 | errors: [METHOD_ERROR] | |
758 | }, | |
759 | { | |
760 | ||
761 | // https://github.com/eslint/eslint/issues/11595 | |
762 | code: "var x = {$y: function() {}}", | |
763 | output: "var x = {$y() {}}", | |
764 | options: ["methods", { ignoreConstructors: true }], | |
765 | errors: [METHOD_ERROR] | |
766 | }, | |
767 | { | |
768 | ||
769 | // https://github.com/eslint/eslint/issues/11595 | |
770 | code: "var x = {__y: function() {}}", | |
771 | output: "var x = {__y() {}}", | |
772 | options: ["methods", { ignoreConstructors: true }], | |
773 | errors: [METHOD_ERROR] | |
774 | }, | |
775 | { | |
776 | ||
777 | // https://github.com/eslint/eslint/issues/11595 | |
778 | code: "var x = {_0y: function() {}}", | |
779 | output: "var x = {_0y() {}}", | |
780 | options: ["methods", { ignoreConstructors: true }], | |
781 | errors: [METHOD_ERROR] | |
782 | }, | |
783 | ||
784 | // avoidQuotes | |
785 | { | |
786 | code: "var x = {a: a}", | |
787 | output: "var x = {a}", | |
788 | options: ["always", { avoidQuotes: true }], | |
789 | errors: [PROPERTY_ERROR] | |
790 | }, | |
791 | { | |
792 | code: "var x = {a: function(){}}", | |
793 | output: "var x = {a(){}}", | |
794 | options: ["methods", { avoidQuotes: true }], | |
795 | errors: [METHOD_ERROR] | |
796 | }, | |
797 | { | |
798 | code: "var x = {[a]: function(){}}", | |
799 | output: "var x = {[a](){}}", | |
800 | options: ["methods", { avoidQuotes: true }], | |
801 | errors: [METHOD_ERROR] | |
802 | }, | |
803 | { | |
804 | code: "var x = {'a'(){}}", | |
805 | output: "var x = {'a': function(){}}", | |
806 | options: ["always", { avoidQuotes: true }], | |
807 | errors: [LONGFORM_METHOD_STRING_LITERAL_ERROR] | |
808 | }, | |
809 | { | |
810 | code: "var x = {['a'](){}}", | |
811 | output: "var x = {['a']: function(){}}", | |
812 | options: ["methods", { avoidQuotes: true }], | |
813 | errors: [LONGFORM_METHOD_STRING_LITERAL_ERROR] | |
814 | }, | |
815 | ||
816 | // consistent | |
817 | { | |
818 | code: "var x = {a: a, b}", | |
819 | output: null, | |
820 | options: ["consistent"], | |
821 | errors: [MIXED_SHORTHAND_ERROR] | |
822 | }, | |
823 | { | |
824 | code: "var x = {b, c: d, f: g}", | |
825 | output: null, | |
826 | options: ["consistent"], | |
827 | errors: [MIXED_SHORTHAND_ERROR] | |
828 | }, | |
829 | { | |
830 | code: "var x = {foo, bar: baz, ...qux}", | |
831 | output: null, | |
832 | options: ["consistent"], | |
833 | parserOptions: { ecmaVersion: 2018 }, | |
834 | errors: [MIXED_SHORTHAND_ERROR] | |
835 | }, | |
836 | ||
837 | // consistent-as-needed | |
838 | { | |
839 | code: "var x = {a: a, b: b}", | |
840 | output: null, | |
841 | options: ["consistent-as-needed"], | |
842 | errors: [ALL_SHORTHAND_ERROR] | |
843 | }, | |
844 | { | |
845 | code: "var x = {a, z: function z(){}}", | |
846 | output: null, | |
847 | options: ["consistent-as-needed"], | |
848 | errors: [MIXED_SHORTHAND_ERROR] | |
849 | ||
850 | }, | |
851 | { | |
852 | code: "var x = {foo: function() {}}", | |
853 | output: null, | |
854 | options: ["consistent-as-needed"], | |
855 | errors: [ALL_SHORTHAND_ERROR] | |
856 | }, | |
857 | { | |
858 | code: "var x = {a: a, b: b, ...baz}", | |
859 | output: null, | |
860 | options: ["consistent-as-needed"], | |
861 | parserOptions: { ecmaVersion: 2018 }, | |
862 | errors: [ALL_SHORTHAND_ERROR] | |
863 | }, | |
864 | { | |
865 | code: "var x = {foo, bar: bar, ...qux}", | |
866 | output: null, | |
867 | options: ["consistent-as-needed"], | |
868 | parserOptions: { ecmaVersion: 2018 }, | |
869 | errors: [MIXED_SHORTHAND_ERROR] | |
870 | }, | |
871 | ||
872 | // avoidExplicitReturnArrows | |
873 | { | |
874 | code: "({ x: () => { return; } })", | |
875 | output: "({ x() { return; } })", | |
876 | options: ["always", { avoidExplicitReturnArrows: true }], | |
877 | errors: [METHOD_ERROR] | |
878 | }, | |
879 | { | |
880 | code: "({ x() { return; }, y: () => { return; } })", | |
881 | output: "({ x() { return; }, y() { return; } })", | |
882 | options: ["always", { avoidExplicitReturnArrows: true }], | |
883 | errors: [METHOD_ERROR] | |
884 | }, | |
885 | { | |
886 | code: "({ x: () => { return; }, y: () => foo })", | |
887 | output: "({ x() { return; }, y: () => foo })", | |
888 | options: ["always", { avoidExplicitReturnArrows: true }], | |
889 | errors: [METHOD_ERROR] | |
890 | }, | |
891 | { | |
892 | code: "({ x: () => { return; }, y: () => { return; } })", | |
893 | output: "({ x() { return; }, y() { return; } })", | |
894 | options: ["always", { avoidExplicitReturnArrows: true }], | |
895 | errors: [METHOD_ERROR, METHOD_ERROR] | |
896 | }, | |
897 | { | |
898 | code: "({ x: foo => { return; } })", | |
899 | output: "({ x(foo) { return; } })", | |
900 | options: ["always", { avoidExplicitReturnArrows: true }], | |
901 | errors: [METHOD_ERROR] | |
902 | }, | |
903 | { | |
904 | code: "({ x: (foo = 1) => { return; } })", | |
905 | output: "({ x(foo = 1) { return; } })", | |
906 | options: ["always", { avoidExplicitReturnArrows: true }], | |
907 | errors: [METHOD_ERROR] | |
908 | }, | |
909 | { | |
910 | code: "({ x: ({ foo: bar = 1 } = {}) => { return; } })", | |
911 | output: "({ x({ foo: bar = 1 } = {}) { return; } })", | |
912 | options: ["always", { avoidExplicitReturnArrows: true }], | |
913 | errors: [METHOD_ERROR] | |
914 | }, | |
915 | { | |
916 | code: "({ x: () => { function foo() { this; } } })", | |
917 | output: "({ x() { function foo() { this; } } })", | |
918 | options: ["always", { avoidExplicitReturnArrows: true }], | |
919 | errors: [METHOD_ERROR] | |
920 | }, | |
921 | { | |
922 | code: "({ x: () => { var foo = function() { arguments; } } })", | |
923 | output: "({ x() { var foo = function() { arguments; } } })", | |
924 | options: ["always", { avoidExplicitReturnArrows: true }], | |
925 | errors: [METHOD_ERROR] | |
926 | }, | |
927 | { | |
928 | code: "({ x: () => { function foo() { arguments; } } })", | |
929 | output: "({ x() { function foo() { arguments; } } })", | |
930 | options: ["always", { avoidExplicitReturnArrows: true }], | |
931 | errors: [METHOD_ERROR] | |
932 | }, | |
933 | { | |
934 | code: ` | |
935 | ({ | |
936 | x: () => { | |
937 | class Foo extends Bar { | |
938 | constructor() { | |
939 | super(); | |
940 | } | |
941 | } | |
942 | } | |
943 | }) | |
944 | `, | |
945 | output: ` | |
946 | ({ | |
947 | x() { | |
948 | class Foo extends Bar { | |
949 | constructor() { | |
950 | super(); | |
951 | } | |
952 | } | |
953 | } | |
954 | }) | |
955 | `, | |
956 | options: ["always", { avoidExplicitReturnArrows: true }], | |
957 | errors: [METHOD_ERROR] | |
958 | }, | |
959 | { | |
960 | code: ` | |
961 | ({ | |
962 | x: () => { | |
963 | function foo() { | |
964 | new.target; | |
965 | } | |
966 | } | |
967 | }) | |
968 | `, | |
969 | output: ` | |
970 | ({ | |
971 | x() { | |
972 | function foo() { | |
973 | new.target; | |
974 | } | |
975 | } | |
976 | }) | |
977 | `, | |
978 | options: ["always", { avoidExplicitReturnArrows: true }], | |
979 | errors: [METHOD_ERROR] | |
980 | }, | |
981 | { | |
982 | code: "({ 'foo bar': () => { return; } })", | |
983 | output: "({ 'foo bar'() { return; } })", | |
984 | options: ["always", { avoidExplicitReturnArrows: true }], | |
985 | errors: [METHOD_ERROR] | |
986 | }, | |
987 | { | |
988 | code: "({ [foo]: () => { return; } })", | |
989 | output: "({ [foo]() { return; } })", | |
990 | options: ["always", { avoidExplicitReturnArrows: true }], | |
991 | errors: [METHOD_ERROR] | |
992 | }, | |
993 | { | |
994 | code: "({ a: 1, foo: async (bar = 1) => { return; } })", | |
995 | output: "({ a: 1, async foo(bar = 1) { return; } })", | |
996 | options: ["always", { avoidExplicitReturnArrows: true }], | |
997 | parserOptions: { ecmaVersion: 8 }, | |
998 | errors: [METHOD_ERROR] | |
999 | }, | |
1000 | { | |
1001 | code: "({ [ foo ]: async bar => { return; } })", | |
1002 | output: "({ async [ foo ](bar) { return; } })", | |
1003 | options: ["always", { avoidExplicitReturnArrows: true }], | |
1004 | parserOptions: { ecmaVersion: 8 }, | |
1005 | errors: [METHOD_ERROR] | |
1006 | }, | |
1007 | { | |
1008 | ||
1009 | // https://github.com/eslint/eslint/issues/11305 | |
1010 | code: "({ key: (arg = () => {}) => {} })", | |
1011 | output: "({ key(arg = () => {}) {} })", | |
1012 | options: ["always", { avoidExplicitReturnArrows: true }], | |
1013 | errors: [METHOD_ERROR] | |
1014 | }, | |
1015 | { | |
1016 | code: ` | |
1017 | function foo() { | |
1018 | var x = { | |
1019 | x: () => { | |
1020 | this; | |
1021 | return { y: () => { foo; } }; | |
1022 | } | |
1023 | }; | |
1024 | } | |
1025 | `, | |
1026 | output: ` | |
1027 | function foo() { | |
1028 | var x = { | |
1029 | x: () => { | |
1030 | this; | |
1031 | return { y() { foo; } }; | |
1032 | } | |
1033 | }; | |
1034 | } | |
1035 | `, | |
1036 | options: ["always", { avoidExplicitReturnArrows: true }], | |
1037 | errors: [METHOD_ERROR] | |
1038 | }, | |
1039 | { | |
1040 | code: ` | |
1041 | function foo() { | |
1042 | var x = { | |
1043 | x: () => { | |
1044 | ({ y: () => { foo; } }); | |
1045 | this; | |
1046 | } | |
1047 | }; | |
1048 | } | |
1049 | `, | |
1050 | output: ` | |
1051 | function foo() { | |
1052 | var x = { | |
1053 | x: () => { | |
1054 | ({ y() { foo; } }); | |
1055 | this; | |
1056 | } | |
1057 | }; | |
1058 | } | |
1059 | `, | |
1060 | options: ["always", { avoidExplicitReturnArrows: true }], | |
1061 | errors: [METHOD_ERROR] | |
1062 | }, | |
1063 | { | |
1064 | code: "({ a: (function(){ return foo; }) })", | |
1065 | output: "({ a(){ return foo; } })", | |
1066 | errors: [METHOD_ERROR] | |
1067 | }, | |
1068 | { | |
1069 | code: "({ a: (() => { return foo; }) })", | |
1070 | output: "({ a() { return foo; } })", | |
1071 | options: ["always", { avoidExplicitReturnArrows: true }], | |
1072 | errors: [METHOD_ERROR] | |
1073 | }, | |
1074 | { | |
1075 | code: "({ a: ((arg) => { return foo; }) })", | |
1076 | output: "({ a(arg) { return foo; } })", | |
1077 | options: ["always", { avoidExplicitReturnArrows: true }], | |
1078 | errors: [METHOD_ERROR] | |
1079 | }, | |
1080 | { | |
1081 | code: "({ a: ((arg, arg2) => { return foo; }) })", | |
1082 | output: "({ a(arg, arg2) { return foo; } })", | |
1083 | options: ["always", { avoidExplicitReturnArrows: true }], | |
1084 | errors: [METHOD_ERROR] | |
1085 | }, | |
1086 | { | |
1087 | code: "({ a: (async () => { return foo; }) })", | |
1088 | output: "({ async a() { return foo; } })", | |
1089 | options: ["always", { avoidExplicitReturnArrows: true }], | |
1090 | errors: [METHOD_ERROR] | |
1091 | }, | |
1092 | { | |
1093 | code: "({ a: (async (arg) => { return foo; }) })", | |
1094 | output: "({ async a(arg) { return foo; } })", | |
1095 | options: ["always", { avoidExplicitReturnArrows: true }], | |
1096 | errors: [METHOD_ERROR] | |
1097 | }, | |
1098 | { | |
1099 | code: "({ a: (async (arg, arg2) => { return foo; }) })", | |
1100 | output: "({ async a(arg, arg2) { return foo; } })", | |
1101 | options: ["always", { avoidExplicitReturnArrows: true }], | |
1102 | errors: [METHOD_ERROR] | |
1103 | }, | |
1104 | ||
1105 | // async generators | |
1106 | { | |
1107 | code: "({ a: async function*() {} })", | |
1108 | output: "({ async *a() {} })", | |
1109 | options: ["always"], | |
1110 | errors: [METHOD_ERROR] | |
1111 | }, | |
1112 | { | |
1113 | code: "({ async* a() {} })", | |
1114 | output: "({ a: async function*() {} })", | |
1115 | options: ["never"], | |
1116 | errors: [LONGFORM_METHOD_ERROR] | |
1117 | }, | |
1118 | ||
1119 | // typescript: arrow function should preserve the return value | |
1120 | { | |
1121 | code: unIndent` | |
1122 | const test = { | |
1123 | key: (): void => {x()}, | |
1124 | key: ( (): void => {x()} ), | |
1125 | key: ( (): (void) => {x()} ), | |
1126 | ||
1127 | key: (arg: t): void => {x()}, | |
1128 | key: ( (arg: t): void => {x()} ), | |
1129 | key: ( (arg: t): (void) => {x()} ), | |
1130 | ||
1131 | key: (arg: t, arg2: t): void => {x()}, | |
1132 | key: ( (arg: t, arg2: t): void => {x()} ), | |
1133 | key: ( (arg: t, arg2: t): (void) => {x()} ), | |
1134 | ||
1135 | key: async (): void => {x()}, | |
1136 | key: ( async (): void => {x()} ), | |
1137 | key: ( async (): (void) => {x()} ), | |
1138 | ||
1139 | key: async (arg: t): void => {x()}, | |
1140 | key: ( async (arg: t): void => {x()} ), | |
1141 | key: ( async (arg: t): (void) => {x()} ), | |
1142 | ||
1143 | key: async (arg: t, arg2: t): void => {x()}, | |
1144 | key: ( async (arg: t, arg2: t): void => {x()} ), | |
1145 | key: ( async (arg: t, arg2: t): (void) => {x()} ), | |
1146 | } | |
1147 | `, | |
1148 | output: unIndent` | |
1149 | const test = { | |
1150 | key(): void {x()}, | |
1151 | key(): void {x()}, | |
1152 | key(): (void) {x()}, | |
1153 | ||
1154 | key(arg: t): void {x()}, | |
1155 | key(arg: t): void {x()}, | |
1156 | key(arg: t): (void) {x()}, | |
1157 | ||
1158 | key(arg: t, arg2: t): void {x()}, | |
1159 | key(arg: t, arg2: t): void {x()}, | |
1160 | key(arg: t, arg2: t): (void) {x()}, | |
1161 | ||
1162 | async key(): void {x()}, | |
1163 | async key(): void {x()}, | |
1164 | async key(): (void) {x()}, | |
1165 | ||
1166 | async key(arg: t): void {x()}, | |
1167 | async key(arg: t): void {x()}, | |
1168 | async key(arg: t): (void) {x()}, | |
1169 | ||
1170 | async key(arg: t, arg2: t): void {x()}, | |
1171 | async key(arg: t, arg2: t): void {x()}, | |
1172 | async key(arg: t, arg2: t): (void) {x()}, | |
1173 | } | |
1174 | `, | |
1175 | options: ["always", { avoidExplicitReturnArrows: true }], | |
1176 | parser: require.resolve("../../fixtures/parsers/typescript-parsers/object-with-arrow-fn-props"), | |
1177 | errors: Array(18).fill(METHOD_ERROR) | |
1178 | } | |
1179 | ] | |
1180 | }); |