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