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