]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/max-lines-per-function.js
import 8.3.0 source
[pve-eslint.git] / eslint / tests / lib / rules / max-lines-per-function.js
1 /**
2 * @fileoverview Tests for max-lines-per-function rule.
3 * @author Pete Ward <peteward44@gmail.com>
4 */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Requirements
9 //------------------------------------------------------------------------------
10
11 const rule = require("../../../lib/rules/max-lines-per-function");
12 const { RuleTester } = require("../../../lib/rule-tester");
13
14 //------------------------------------------------------------------------------
15 // Tests
16 //------------------------------------------------------------------------------
17
18 const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
19
20 ruleTester.run("max-lines-per-function", rule, {
21 valid: [
22
23 // Test code in global scope doesn't count
24 {
25 code: "var x = 5;\nvar x = 2;\n",
26 options: [1]
27 },
28
29 // Test single line standalone function
30 {
31 code: "function name() {}",
32 options: [1]
33 },
34
35 // Test standalone function with lines of code
36 {
37 code: "function name() {\nvar x = 5;\nvar x = 2;\n}",
38 options: [4]
39 },
40
41 // Test inline arrow function
42 {
43 code: "const bar = () => 2",
44 options: [1]
45 },
46
47 // Test arrow function
48 {
49 code: "const bar = () => {\nconst x = 2 + 1;\nreturn x;\n}",
50 options: [4]
51 },
52
53 // skipBlankLines: false with simple standalone function
54 {
55 code: "function name() {\nvar x = 5;\n\t\n \n\nvar x = 2;\n}",
56 options: [{ max: 7, skipComments: false, skipBlankLines: false }]
57 },
58
59 // skipBlankLines: true with simple standalone function
60 {
61 code: "function name() {\nvar x = 5;\n\t\n \n\nvar x = 2;\n}",
62 options: [{ max: 4, skipComments: false, skipBlankLines: true }]
63 },
64
65 // skipComments: true with an individual single line comment
66 {
67 code: "function name() {\nvar x = 5;\nvar x = 2; // end of line comment\n}",
68 options: [{ max: 4, skipComments: true, skipBlankLines: false }]
69 },
70
71 // skipComments: true with an individual single line comment
72 {
73 code: "function name() {\nvar x = 5;\n// a comment on it's own line\nvar x = 2; // end of line comment\n}",
74 options: [{ max: 4, skipComments: true, skipBlankLines: false }]
75 },
76
77 // skipComments: true with single line comments
78 {
79 code: "function name() {\nvar x = 5;\n// a comment on it's own line\n// and another line comment\nvar x = 2; // end of line comment\n}",
80 options: [{ max: 4, skipComments: true, skipBlankLines: false }]
81 },
82
83 // skipComments: true test with multiple different comment types
84 {
85 code: "function name() {\nvar x = 5;\n/* a \n multi \n line \n comment \n*/\n\nvar x = 2; // end of line comment\n}",
86 options: [{ max: 5, skipComments: true, skipBlankLines: false }]
87 },
88
89 // skipComments: true with multiple different comment types, including trailing and leading whitespace
90 {
91 code: "function name() {\nvar x = 5;\n\t/* a comment with leading whitespace */\n/* a comment with trailing whitespace */\t\t\n\t/* a comment with trailing and leading whitespace */\t\t\n/* a \n multi \n line \n comment \n*/\t\t\n\nvar x = 2; // end of line comment\n}",
92 options: [{ max: 5, skipComments: true, skipBlankLines: false }]
93 },
94
95 // Multiple params on separate lines test
96 {
97 code: `function foo(
98 aaa = 1,
99 bbb = 2,
100 ccc = 3
101 ) {
102 return aaa + bbb + ccc
103 }`,
104 options: [{ max: 7, skipComments: true, skipBlankLines: false }]
105 },
106
107 // IIFE validity test
108 {
109 code: `(
110 function
111 ()
112 {
113 }
114 )
115 ()`,
116 options: [{ max: 4, skipComments: true, skipBlankLines: false, IIFEs: true }]
117 },
118
119 // Nested function validity test
120 {
121 code: `function parent() {
122 var x = 0;
123 function nested() {
124 var y = 0;
125 x = 2;
126 }
127 if ( x === y ) {
128 x++;
129 }
130 }`,
131 options: [{ max: 10, skipComments: true, skipBlankLines: false }]
132 },
133
134 // Class method validity test
135 {
136 code: `class foo {
137 method() {
138 let y = 10;
139 let x = 20;
140 return y + x;
141 }
142 }`,
143 options: [{ max: 5, skipComments: true, skipBlankLines: false }]
144 },
145
146 // IIFEs should be recognised if IIFEs: true
147 {
148 code: `(function(){
149 let x = 0;
150 let y = 0;
151 let z = x + y;
152 let foo = {};
153 return bar;
154 }());`,
155 options: [{ max: 7, skipComments: true, skipBlankLines: false, IIFEs: true }]
156 },
157
158 // IIFEs should not be recognised if IIFEs: false
159 {
160 code: `(function(){
161 let x = 0;
162 let y = 0;
163 let z = x + y;
164 let foo = {};
165 return bar;
166 }());`,
167 options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: false }]
168 },
169
170 // Arrow IIFEs should be recognised if IIFEs: true
171 {
172 code: `(() => {
173 let x = 0;
174 let y = 0;
175 let z = x + y;
176 let foo = {};
177 return bar;
178 })();`,
179 options: [{ max: 7, skipComments: true, skipBlankLines: false, IIFEs: true }]
180 },
181
182 // Arrow IIFEs should not be recognised if IIFEs: false
183 {
184 code: `(() => {
185 let x = 0;
186 let y = 0;
187 let z = x + y;
188 let foo = {};
189 return bar;
190 })();`,
191 options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: false }]
192 }
193 ],
194
195 invalid: [
196
197 // Test simple standalone function is recognised
198 {
199 code: "function name() {\n}",
200 options: [1],
201 errors: [
202 { messageId: "exceed", data: { name: "Function 'name'", lineCount: 2, maxLines: 1 } }
203 ]
204 },
205
206 // Test anonymous function assigned to variable is recognised
207 {
208 code: "var func = function() {\n}",
209 options: [1],
210 errors: [
211 { messageId: "exceed", data: { name: "Function", lineCount: 2, maxLines: 1 } }
212 ]
213 },
214
215 // Test arrow functions are recognised
216 {
217 code: "const bar = () => {\nconst x = 2 + 1;\nreturn x;\n}",
218 options: [3],
219 errors: [
220 { messageId: "exceed", data: { name: "Arrow function", lineCount: 4, maxLines: 3 } }
221 ]
222 },
223
224 // Test inline arrow functions are recognised
225 {
226 code: "const bar = () =>\n 2",
227 options: [1],
228 errors: [
229 { messageId: "exceed", data: { name: "Arrow function", lineCount: 2, maxLines: 1 } }
230 ]
231 },
232
233 // Test that option defaults work as expected
234 {
235 code: `() => {${"foo\n".repeat(60)}}`,
236 options: [{}],
237 errors: [
238 { messageId: "exceed", data: { name: "Arrow function", lineCount: 61, maxLines: 50 } }
239 ]
240 },
241
242 // Test skipBlankLines: false
243 {
244 code: "function name() {\nvar x = 5;\n\t\n \n\nvar x = 2;\n}",
245 options: [{ max: 6, skipComments: false, skipBlankLines: false }],
246 errors: [
247 { messageId: "exceed", data: { name: "Function 'name'", lineCount: 7, maxLines: 6 } }
248 ]
249 },
250
251 // Test skipBlankLines: false with CRLF line endings
252 {
253 code: "function name() {\r\nvar x = 5;\r\n\t\r\n \r\n\r\nvar x = 2;\r\n}",
254 options: [{ max: 6, skipComments: true, skipBlankLines: false }],
255 errors: [
256 { messageId: "exceed", data: { name: "Function 'name'", lineCount: 7, maxLines: 6 } }
257 ]
258 },
259
260 // Test skipBlankLines: true
261 {
262 code: "function name() {\nvar x = 5;\n\t\n \n\nvar x = 2;\n}",
263 options: [{ max: 2, skipComments: true, skipBlankLines: true }],
264 errors: [
265 { messageId: "exceed", data: { name: "Function 'name'", lineCount: 4, maxLines: 2 } }
266 ]
267 },
268
269 // Test skipBlankLines: true with CRLF line endings
270 {
271 code: "function name() {\r\nvar x = 5;\r\n\t\r\n \r\n\r\nvar x = 2;\r\n}",
272 options: [{ max: 2, skipComments: true, skipBlankLines: true }],
273 errors: [
274 { messageId: "exceed", data: { name: "Function 'name'", lineCount: 4, maxLines: 2 } }
275 ]
276 },
277
278 // Test skipComments: true and skipBlankLines: false for multiple types of comment
279 {
280 code: "function name() { // end of line comment\nvar x = 5; /* mid line comment */\n\t// single line comment taking up whole line\n\t\n \n\nvar x = 2;\n}",
281 options: [{ max: 6, skipComments: true, skipBlankLines: false }],
282 errors: [
283 { messageId: "exceed", data: { name: "Function 'name'", lineCount: 7, maxLines: 6 } }
284 ]
285 },
286
287 // Test skipComments: true and skipBlankLines: true for multiple types of comment
288 {
289 code: "function name() { // end of line comment\nvar x = 5; /* mid line comment */\n\t// single line comment taking up whole line\n\t\n \n\nvar x = 2;\n}",
290 options: [{ max: 1, skipComments: true, skipBlankLines: true }],
291 errors: [
292 { messageId: "exceed", data: { name: "Function 'name'", lineCount: 4, maxLines: 1 } }
293 ]
294 },
295
296 // Test skipComments: false and skipBlankLines: true for multiple types of comment
297 {
298 code: "function name() { // end of line comment\nvar x = 5; /* mid line comment */\n\t// single line comment taking up whole line\n\t\n \n\nvar x = 2;\n}",
299 options: [{ max: 1, skipComments: false, skipBlankLines: true }],
300 errors: [
301 { messageId: "exceed", data: { name: "Function 'name'", lineCount: 5, maxLines: 1 } }
302 ]
303 },
304
305 // Test simple standalone function with params on separate lines
306 {
307 code: `function foo(
308 aaa = 1,
309 bbb = 2,
310 ccc = 3
311 ) {
312 return aaa + bbb + ccc
313 }`,
314 options: [{ max: 2, skipComments: true, skipBlankLines: false }],
315 errors: [
316 { messageId: "exceed", data: { name: "Function 'foo'", lineCount: 7, maxLines: 2 } }
317 ]
318 },
319
320 // Test IIFE "function" keyword is included in the count
321 {
322 code: `(
323 function
324 ()
325 {
326 }
327 )
328 ()`,
329 options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: true }],
330 errors: [
331 { messageId: "exceed", data: { name: "Function", lineCount: 4, maxLines: 2 } }
332 ]
333 },
334
335 // Test nested functions are included in it's parent's function count.
336 {
337 code: `function parent() {
338 var x = 0;
339 function nested() {
340 var y = 0;
341 x = 2;
342 }
343 if ( x === y ) {
344 x++;
345 }
346 }`,
347 options: [{ max: 9, skipComments: true, skipBlankLines: false }],
348 errors: [
349 { messageId: "exceed", data: { name: "Function 'parent'", lineCount: 10, maxLines: 9 } }
350 ]
351 },
352
353 // Test nested functions are included in it's parent's function count.
354 {
355 code: `function parent() {
356 var x = 0;
357 function nested() {
358 var y = 0;
359 x = 2;
360 }
361 if ( x === y ) {
362 x++;
363 }
364 }`,
365 options: [{ max: 2, skipComments: true, skipBlankLines: false }],
366 errors: [
367 { messageId: "exceed", data: { name: "Function 'parent'", lineCount: 10, maxLines: 2 } },
368 { messageId: "exceed", data: { name: "Function 'nested'", lineCount: 4, maxLines: 2 } }
369 ]
370 },
371
372 // Test regular methods are recognised
373 {
374 code: `class foo {
375 method() {
376 let y = 10;
377 let x = 20;
378 return y + x;
379 }
380 }`,
381 options: [{ max: 2, skipComments: true, skipBlankLines: false }],
382 errors: [
383 { messageId: "exceed", data: { name: "Method 'method'", lineCount: 5, maxLines: 2 } }
384 ]
385 },
386
387 // Test static methods are recognised
388 {
389 code: `class A {
390 static
391 foo
392 (a) {
393 return a
394 }
395 }`,
396 options: [{ max: 2, skipComments: true, skipBlankLines: false }],
397 errors: [
398 { messageId: "exceed", data: { name: "Static method 'foo'", lineCount: 5, maxLines: 2 } }
399 ]
400 },
401
402 // Test getters are recognised as properties
403 {
404 code: `var obj = {
405 get
406 foo
407 () {
408 return 1
409 }
410 }`,
411 options: [{ max: 2, skipComments: true, skipBlankLines: false }],
412 errors: [
413 { messageId: "exceed", data: { name: "Getter 'foo'", lineCount: 5, maxLines: 2 } }
414 ]
415 },
416
417 // Test setters are recognised as properties
418 {
419 code: `var obj = {
420 set
421 foo
422 ( val ) {
423 this._foo = val;
424 }
425 }`,
426 options: [{ max: 2, skipComments: true, skipBlankLines: false }],
427 errors: [
428 { messageId: "exceed", data: { name: "Setter 'foo'", lineCount: 5, maxLines: 2 } }
429 ]
430 },
431
432 // Test computed property names
433 {
434 code: `class A {
435 static
436 [
437 foo +
438 bar
439 ]
440 (a) {
441 return a
442 }
443 }`,
444 options: [{ max: 2, skipComments: true, skipBlankLines: false }],
445 errors: [
446 { messageId: "exceed", data: { name: "Static method", lineCount: 8, maxLines: 2 } }
447 ]
448 },
449
450 // Test the IIFEs option includes IIFEs
451 {
452 code: `(function(){
453 let x = 0;
454 let y = 0;
455 let z = x + y;
456 let foo = {};
457 return bar;
458 }());`,
459 options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: true }],
460 errors: [
461 { messageId: "exceed", data: { name: "Function", lineCount: 7, maxLines: 2 } }
462 ]
463 },
464
465 // Test the IIFEs option includes arrow IIFEs
466 {
467 code: `(() => {
468 let x = 0;
469 let y = 0;
470 let z = x + y;
471 let foo = {};
472 return bar;
473 })();`,
474 options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: true }],
475 errors: [
476 { messageId: "exceed", data: { name: "Arrow function", lineCount: 7, maxLines: 2 } }
477 ]
478 }
479 ]
480 });