]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/max-lines-per-function.js
9107cd69f0f34a583da8b8b271b5da677ab22592
2 * @fileoverview Tests for max-lines-per-function rule.
3 * @author Pete Ward <peteward44@gmail.com>
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
10 const rule
= require ( "../../../lib/rules/max-lines-per-function" );
11 const { RuleTester
} = require ( "../../../lib/rule-tester" );
13 //------------------------------------------------------------------------------
15 //------------------------------------------------------------------------------
17 const ruleTester
= new RuleTester ({ parserOptions
: { ecmaVersion
: 6 } });
19 ruleTester
. run ( "max-lines-per-function" , rule
, {
22 // Test code in global scope doesn't count
24 code
: "var x = 5; \n var x = 2; \n " ,
28 // Test single line standalone function
30 code
: "function name() {}" ,
34 // Test standalone function with lines of code
36 code
: "function name() { \n var x = 5; \n var x = 2; \n }" ,
40 // Test inline arrow function
42 code
: "const bar = () => 2" ,
46 // Test arrow function
48 code
: "const bar = () => { \n const x = 2 + 1; \n return x; \n }" ,
52 // skipBlankLines: false with simple standalone function
54 code
: "function name() { \n var x = 5; \n\t\n \n\n var x = 2; \n }" ,
55 options
: [{ max
: 7 , skipComments
: false , skipBlankLines
: false }]
58 // skipBlankLines: true with simple standalone function
60 code
: "function name() { \n var x = 5; \n\t\n \n\n var x = 2; \n }" ,
61 options
: [{ max
: 4 , skipComments
: false , skipBlankLines
: true }]
64 // skipComments: true with an individual single line comment
66 code
: "function name() { \n var x = 5; \n var x = 2; // end of line comment \n }" ,
67 options
: [{ max
: 4 , skipComments
: true , skipBlankLines
: false }]
70 // skipComments: true with an individual single line comment
72 code
: "function name() { \n var x = 5; \n // a comment on it's own line \n var x = 2; // end of line comment \n }" ,
73 options
: [{ max
: 4 , skipComments
: true , skipBlankLines
: false }]
76 // skipComments: true with single line comments
78 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 }" ,
79 options
: [{ max
: 4 , skipComments
: true , skipBlankLines
: false }]
82 // skipComments: true test with multiple different comment types
84 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 }" ,
85 options
: [{ max
: 5 , skipComments
: true , skipBlankLines
: false }]
88 // skipComments: true with multiple different comment types, including trailing and leading whitespace
90 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 }" ,
91 options
: [{ max
: 5 , skipComments
: true , skipBlankLines
: false }]
94 // Multiple params on separate lines test
101 return aaa + bbb + ccc
103 options
: [{ max
: 7 , skipComments
: true , skipBlankLines
: false }]
106 // IIFE validity test
115 options
: [{ max
: 4 , skipComments
: true , skipBlankLines
: false , IIFEs
: true }]
118 // Nested function validity test
120 code
: `function parent() {
130 options
: [{ max
: 10 , skipComments
: true , skipBlankLines
: false }]
133 // Class method validity test
142 options
: [{ max
: 5 , skipComments
: true , skipBlankLines
: false }]
145 // IIFEs should be recognised if IIFEs: true
154 options
: [{ max
: 7 , skipComments
: true , skipBlankLines
: false , IIFEs
: true }]
157 // IIFEs should not be recognised if IIFEs: false
166 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: false , IIFEs
: false }]
172 // Test simple standalone function is recognised
174 code
: "function name() { \n }" ,
177 { messageId
: "exceed" , data
: { name
: "Function 'name'" , lineCount
: 2 , maxLines
: 1 } }
181 // Test anonymous function assigned to variable is recognised
183 code
: "var func = function() { \n }" ,
186 { messageId
: "exceed" , data
: { name
: "Function" , lineCount
: 2 , maxLines
: 1 } }
190 // Test arrow functions are recognised
192 code
: "const bar = () => { \n const x = 2 + 1; \n return x; \n }" ,
195 { messageId
: "exceed" , data
: { name
: "Arrow function" , lineCount
: 4 , maxLines
: 3 } }
199 // Test inline arrow functions are recognised
201 code
: "const bar = () => \n 2" ,
204 { messageId
: "exceed" , data
: { name
: "Arrow function" , lineCount
: 2 , maxLines
: 1 } }
208 // Test that option defaults work as expected
210 code
: `() => { ${"foo\n".repeat(60)} }` ,
213 { messageId
: "exceed" , data
: { name
: "Arrow function" , lineCount
: 61 , maxLines
: 50 } }
217 // Test skipBlankLines: false
219 code
: "function name() { \n var x = 5; \n\t\n \n\n var x = 2; \n }" ,
220 options
: [{ max
: 6 , skipComments
: false , skipBlankLines
: false }],
222 { messageId
: "exceed" , data
: { name
: "Function 'name'" , lineCount
: 7 , maxLines
: 6 } }
226 // Test skipBlankLines: false with CRLF line endings
228 code
: "function name() { \r\n var x = 5; \r\n\t\r\n \r\n\r\n var x = 2; \r\n }" ,
229 options
: [{ max
: 6 , skipComments
: true , skipBlankLines
: false }],
231 { messageId
: "exceed" , data
: { name
: "Function 'name'" , lineCount
: 7 , maxLines
: 6 } }
235 // Test skipBlankLines: true
237 code
: "function name() { \n var x = 5; \n\t\n \n\n var x = 2; \n }" ,
238 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: true }],
240 { messageId
: "exceed" , data
: { name
: "Function 'name'" , lineCount
: 4 , maxLines
: 2 } }
244 // Test skipBlankLines: true with CRLF line endings
246 code
: "function name() { \r\n var x = 5; \r\n\t\r\n \r\n\r\n var x = 2; \r\n }" ,
247 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: true }],
249 { messageId
: "exceed" , data
: { name
: "Function 'name'" , lineCount
: 4 , maxLines
: 2 } }
253 // Test skipComments: true and skipBlankLines: false for multiple types of comment
255 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 }" ,
256 options
: [{ max
: 6 , skipComments
: true , skipBlankLines
: false }],
258 { messageId
: "exceed" , data
: { name
: "Function 'name'" , lineCount
: 7 , maxLines
: 6 } }
262 // Test skipComments: true and skipBlankLines: true for multiple types of comment
264 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 }" ,
265 options
: [{ max
: 1 , skipComments
: true , skipBlankLines
: true }],
267 { messageId
: "exceed" , data
: { name
: "Function 'name'" , lineCount
: 4 , maxLines
: 1 } }
271 // Test skipComments: false and skipBlankLines: true for multiple types of comment
273 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 }" ,
274 options
: [{ max
: 1 , skipComments
: false , skipBlankLines
: true }],
276 { messageId
: "exceed" , data
: { name
: "Function 'name'" , lineCount
: 5 , maxLines
: 1 } }
280 // Test simple standalone function with params on separate lines
287 return aaa + bbb + ccc
289 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: false }],
291 { messageId
: "exceed" , data
: { name
: "Function 'foo'" , lineCount
: 7 , maxLines
: 2 } }
295 // Test IIFE "function" keyword is included in the count
304 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: false , IIFEs
: true }],
306 { messageId
: "exceed" , data
: { name
: "Function" , lineCount
: 4 , maxLines
: 2 } }
310 // Test nested functions are included in it's parent's function count.
312 code
: `function parent() {
322 options
: [{ max
: 9 , skipComments
: true , skipBlankLines
: false }],
324 { messageId
: "exceed" , data
: { name
: "Function 'parent'" , lineCount
: 10 , maxLines
: 9 } }
328 // Test nested functions are included in it's parent's function count.
330 code
: `function parent() {
340 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: false }],
342 { messageId
: "exceed" , data
: { name
: "Function 'parent'" , lineCount
: 10 , maxLines
: 2 } },
343 { messageId
: "exceed" , data
: { name
: "Function 'nested'" , lineCount
: 4 , maxLines
: 2 } }
347 // Test regular methods are recognised
356 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: false }],
358 { messageId
: "exceed" , data
: { name
: "Method 'method'" , lineCount
: 5 , maxLines
: 2 } }
362 // Test static methods are recognised
371 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: false }],
373 { messageId
: "exceed" , data
: { name
: "Static method 'foo'" , lineCount
: 5 , maxLines
: 2 } }
377 // Test getters are recognised as properties
386 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: false }],
388 { messageId
: "exceed" , data
: { name
: "Getter 'foo'" , lineCount
: 5 , maxLines
: 2 } }
392 // Test setters are recognised as properties
401 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: false }],
403 { messageId
: "exceed" , data
: { name
: "Setter 'foo'" , lineCount
: 5 , maxLines
: 2 } }
407 // Test computed property names
419 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: false }],
421 { messageId
: "exceed" , data
: { name
: "Static method" , lineCount
: 8 , maxLines
: 2 } }
425 // Test the IIFEs option includes IIFEs
434 options
: [{ max
: 2 , skipComments
: true , skipBlankLines
: false , IIFEs
: true }],
436 { messageId
: "exceed" , data
: { name
: "Function" , lineCount
: 7 , maxLines
: 2 } }