]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/max-lines-per-function.js
2 * @fileoverview Tests for max-lines-per-function rule.
3 * @author Pete Ward <peteward44@gmail.com>
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const rule
= require ( "../../../lib/rules/max-lines-per-function" );
12 const { RuleTester
} = require ( "../../../lib/rule-tester" );
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
18 const ruleTester
= new RuleTester ({ parserOptions
: { ecmaVersion
: 6 } });
20 ruleTester
. run ( "max-lines-per-function" , rule
, {
23 // Test code in global scope doesn't count
25 code
: "var x = 5; \n var x = 2; \n " ,
29 // Test single line standalone function
31 code
: "function name() {}" ,
35 // Test standalone function with lines of code
37 code
: "function name() { \n var x = 5; \n var x = 2; \n }" ,
41 // Test inline arrow function
43 code
: "const bar = () => 2" ,
47 // Test arrow function
49 code
: "const bar = () => { \n const x = 2 + 1; \n return x; \n }" ,
53 // skipBlankLines: false with simple standalone function
55 code
: "function name() { \n var x = 5; \n\t\n \n\n var x = 2; \n }" ,
56 options
: [{ max
: 7 , skipComments
: false , skipBlankLines
: false }]
59 // skipBlankLines: true with simple standalone function
61 code
: "function name() { \n var x = 5; \n\t\n \n\n var x = 2; \n }" ,
62 options
: [{ max
: 4 , skipComments
: false , skipBlankLines
: true }]
65 // skipComments: true with an individual single line comment
67 code
: "function name() { \n var x = 5; \n var x = 2; // end of line comment \n }" ,
68 options
: [{ max
: 4 , skipComments
: true , skipBlankLines
: false }]
71 // skipComments: true with an individual single line comment
73 code
: "function name() { \n var x = 5; \n // a comment on it's own line \n var x = 2; // end of line comment \n }" ,
74 options
: [{ max
: 4 , skipComments
: true , skipBlankLines
: false }]
77 // skipComments: true with single line comments
79 code
: "function name() { \n var x = 5; \n // a comment on it's own line \n // and another line comment \n var x = 2; // end of line comment \n }" ,
80 options
: [{ max
: 4 , skipComments
: true , skipBlankLines
: false }]
83 // skipComments: true test with multiple different comment types
85 code
: "function name() { \n var x = 5; \n /* a \n multi \n line \n comment \n */ \n\n var x = 2; // end of line comment \n }" ,
86 options
: [{ max
: 5 , skipComments
: true , skipBlankLines
: false }]
89 // skipComments: true with multiple different comment types, including trailing and leading whitespace
91 code
: "function name() { \n var 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\n var x = 2; // end of line comment \n }" ,
92 options
: [{ max
: 5 , skipComments
: true , skipBlankLines
: false }]
95 // Multiple params on separate lines test
102 return aaa + bbb + ccc
104 options
: [{ max
: 7 , skipComments
: true , skipBlankLines
: false }]
107 // IIFE validity test
116 options
: [{ max
: 4 , skipComments
: true , skipBlankLines
: false , IIFEs
: true }]
119 // Nested function validity test
121 code
: `function parent() {
131 options
: [{ max
: 10 , skipComments
: true , skipBlankLines
: false }]
134 // Class method validity test
143 options
: [{ max
: 5 , skipComments
: true , skipBlankLines
: false }]
146 // IIFEs should be recognised if IIFEs: true
155 options
: [{ max
: 7 , skipComments
: true , skipBlankLines
: false , IIFEs
: true }]
158 // IIFEs should not be recognised if IIFEs: false
167 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: false , IIFEs
: false }]
170 // Arrow IIFEs should be recognised if IIFEs: true
179 options
: [{ max
: 7 , skipComments
: true , skipBlankLines
: false , IIFEs
: true }]
182 // Arrow IIFEs should not be recognised if IIFEs: false
191 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: false , IIFEs
: false }]
197 // Test simple standalone function is recognised
199 code
: "function name() { \n }" ,
202 { messageId
: "exceed" , data
: { name
: "Function 'name'" , lineCount
: 2 , maxLines
: 1 } }
206 // Test anonymous function assigned to variable is recognised
208 code
: "var func = function() { \n }" ,
211 { messageId
: "exceed" , data
: { name
: "Function" , lineCount
: 2 , maxLines
: 1 } }
215 // Test arrow functions are recognised
217 code
: "const bar = () => { \n const x = 2 + 1; \n return x; \n }" ,
220 { messageId
: "exceed" , data
: { name
: "Arrow function" , lineCount
: 4 , maxLines
: 3 } }
224 // Test inline arrow functions are recognised
226 code
: "const bar = () => \n 2" ,
229 { messageId
: "exceed" , data
: { name
: "Arrow function" , lineCount
: 2 , maxLines
: 1 } }
233 // Test that option defaults work as expected
235 code
: `() => { ${"foo\n".repeat(60)} }` ,
238 { messageId
: "exceed" , data
: { name
: "Arrow function" , lineCount
: 61 , maxLines
: 50 } }
242 // Test skipBlankLines: false
244 code
: "function name() { \n var x = 5; \n\t\n \n\n var x = 2; \n }" ,
245 options
: [{ max
: 6 , skipComments
: false , skipBlankLines
: false }],
247 { messageId
: "exceed" , data
: { name
: "Function 'name'" , lineCount
: 7 , maxLines
: 6 } }
251 // Test skipBlankLines: false with CRLF line endings
253 code
: "function name() { \r\n var x = 5; \r\n\t\r\n \r\n\r\n var x = 2; \r\n }" ,
254 options
: [{ max
: 6 , skipComments
: true , skipBlankLines
: false }],
256 { messageId
: "exceed" , data
: { name
: "Function 'name'" , lineCount
: 7 , maxLines
: 6 } }
260 // Test skipBlankLines: true
262 code
: "function name() { \n var x = 5; \n\t\n \n\n var x = 2; \n }" ,
263 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: true }],
265 { messageId
: "exceed" , data
: { name
: "Function 'name'" , lineCount
: 4 , maxLines
: 2 } }
269 // Test skipBlankLines: true with CRLF line endings
271 code
: "function name() { \r\n var x = 5; \r\n\t\r\n \r\n\r\n var x = 2; \r\n }" ,
272 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: true }],
274 { messageId
: "exceed" , data
: { name
: "Function 'name'" , lineCount
: 4 , maxLines
: 2 } }
278 // Test skipComments: true and skipBlankLines: false for multiple types of comment
280 code
: "function name() { // end of line comment \n var x = 5; /* mid line comment */ \n\t // single line comment taking up whole line \n\t\n \n\n var x = 2; \n }" ,
281 options
: [{ max
: 6 , skipComments
: true , skipBlankLines
: false }],
283 { messageId
: "exceed" , data
: { name
: "Function 'name'" , lineCount
: 7 , maxLines
: 6 } }
287 // Test skipComments: true and skipBlankLines: true for multiple types of comment
289 code
: "function name() { // end of line comment \n var x = 5; /* mid line comment */ \n\t // single line comment taking up whole line \n\t\n \n\n var x = 2; \n }" ,
290 options
: [{ max
: 1 , skipComments
: true , skipBlankLines
: true }],
292 { messageId
: "exceed" , data
: { name
: "Function 'name'" , lineCount
: 4 , maxLines
: 1 } }
296 // Test skipComments: false and skipBlankLines: true for multiple types of comment
298 code
: "function name() { // end of line comment \n var x = 5; /* mid line comment */ \n\t // single line comment taking up whole line \n\t\n \n\n var x = 2; \n }" ,
299 options
: [{ max
: 1 , skipComments
: false , skipBlankLines
: true }],
301 { messageId
: "exceed" , data
: { name
: "Function 'name'" , lineCount
: 5 , maxLines
: 1 } }
305 // Test simple standalone function with params on separate lines
312 return aaa + bbb + ccc
314 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: false }],
316 { messageId
: "exceed" , data
: { name
: "Function 'foo'" , lineCount
: 7 , maxLines
: 2 } }
320 // Test IIFE "function" keyword is included in the count
329 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: false , IIFEs
: true }],
331 { messageId
: "exceed" , data
: { name
: "Function" , lineCount
: 4 , maxLines
: 2 } }
335 // Test nested functions are included in it's parent's function count.
337 code
: `function parent() {
347 options
: [{ max
: 9 , skipComments
: true , skipBlankLines
: false }],
349 { messageId
: "exceed" , data
: { name
: "Function 'parent'" , lineCount
: 10 , maxLines
: 9 } }
353 // Test nested functions are included in it's parent's function count.
355 code
: `function parent() {
365 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: false }],
367 { messageId
: "exceed" , data
: { name
: "Function 'parent'" , lineCount
: 10 , maxLines
: 2 } },
368 { messageId
: "exceed" , data
: { name
: "Function 'nested'" , lineCount
: 4 , maxLines
: 2 } }
372 // Test regular methods are recognised
381 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: false }],
383 { messageId
: "exceed" , data
: { name
: "Method 'method'" , lineCount
: 5 , maxLines
: 2 } }
387 // Test static methods are recognised
396 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: false }],
398 { messageId
: "exceed" , data
: { name
: "Static method 'foo'" , lineCount
: 5 , maxLines
: 2 } }
402 // Test getters are recognised as properties
411 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: false }],
413 { messageId
: "exceed" , data
: { name
: "Getter 'foo'" , lineCount
: 5 , maxLines
: 2 } }
417 // Test setters are recognised as properties
426 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: false }],
428 { messageId
: "exceed" , data
: { name
: "Setter 'foo'" , lineCount
: 5 , maxLines
: 2 } }
432 // Test computed property names
444 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: false }],
446 { messageId
: "exceed" , data
: { name
: "Static method" , lineCount
: 8 , maxLines
: 2 } }
450 // Test the IIFEs option includes IIFEs
459 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: false , IIFEs
: true }],
461 { messageId
: "exceed" , data
: { name
: "Function" , lineCount
: 7 , maxLines
: 2 } }
465 // Test the IIFEs option includes arrow IIFEs
474 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: false , IIFEs
: true }],
476 { messageId
: "exceed" , data
: { name
: "Arrow function" , lineCount
: 7 , maxLines
: 2 } }