]> git.proxmox.com Git - pve-eslint.git/blobdiff - eslint/tests/lib/rules/max-lines-per-function.js
import 8.4.0 source
[pve-eslint.git] / eslint / tests / lib / rules / max-lines-per-function.js
index 9107cd69f0f34a583da8b8b271b5da677ab22592..74acab2af2e78e647d8fbcfa3d5e4797363942f8 100644 (file)
@@ -7,6 +7,7 @@
 //------------------------------------------------------------------------------
 // Requirements
 //------------------------------------------------------------------------------
+
 const rule = require("../../../lib/rules/max-lines-per-function");
 const { RuleTester } = require("../../../lib/rule-tester");
 
@@ -164,6 +165,30 @@ if ( x === y ) {
     return bar;
 }());`,
             options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: false }]
+        },
+
+        // Arrow IIFEs should be recognised if IIFEs: true
+        {
+            code: `(() => {
+    let x = 0;
+    let y = 0;
+    let z = x + y;
+    let foo = {};
+    return bar;
+})();`,
+            options: [{ max: 7, skipComments: true, skipBlankLines: false, IIFEs: true }]
+        },
+
+        // Arrow IIFEs should not be recognised if IIFEs: false
+        {
+            code: `(() => {
+    let x = 0;
+    let y = 0;
+    let z = x + y;
+    let foo = {};
+    return bar;
+})();`,
+            options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: false }]
         }
     ],
 
@@ -174,7 +199,14 @@ if ( x === y ) {
             code: "function name() {\n}",
             options: [1],
             errors: [
-                { messageId: "exceed", data: { name: "Function 'name'", lineCount: 2, maxLines: 1 } }
+                {
+                    messageId: "exceed",
+                    data: { name: "Function 'name'", linesExceed: 1, maxLines: 1 },
+                    line: 2,
+                    column: 1,
+                    endLine: 2,
+                    endColumn: 2
+                }
             ]
         },
 
@@ -183,7 +215,14 @@ if ( x === y ) {
             code: "var func = function() {\n}",
             options: [1],
             errors: [
-                { messageId: "exceed", data: { name: "Function", lineCount: 2, maxLines: 1 } }
+                {
+                    messageId: "exceed",
+                    data: { name: "Function", linesExceed: 1, maxLines: 1 },
+                    line: 2,
+                    column: 1,
+                    endLine: 2,
+                    endColumn: 2
+                }
             ]
         },
 
@@ -192,7 +231,14 @@ if ( x === y ) {
             code: "const bar = () => {\nconst x = 2 + 1;\nreturn x;\n}",
             options: [3],
             errors: [
-                { messageId: "exceed", data: { name: "Arrow function", lineCount: 4, maxLines: 3 } }
+                {
+                    messageId: "exceed",
+                    data: { name: "Arrow function", linesExceed: 1, maxLines: 3 },
+                    line: 4,
+                    column: 1,
+                    endLine: 4,
+                    endColumn: 2
+                }
             ]
         },
 
@@ -201,7 +247,14 @@ if ( x === y ) {
             code: "const bar = () =>\n 2",
             options: [1],
             errors: [
-                { messageId: "exceed", data: { name: "Arrow function", lineCount: 2, maxLines: 1 } }
+                {
+                    messageId: "exceed",
+                    data: { name: "Arrow function", linesExceed: 1, maxLines: 1 },
+                    line: 2,
+                    column: 1,
+                    endLine: 2,
+                    endColumn: 3
+                }
             ]
         },
 
@@ -210,7 +263,14 @@ if ( x === y ) {
             code: `() => {${"foo\n".repeat(60)}}`,
             options: [{}],
             errors: [
-                { messageId: "exceed", data: { name: "Arrow function", lineCount: 61, maxLines: 50 } }
+                {
+                    messageId: "exceed",
+                    data: { name: "Arrow function", linesExceed: 11, maxLines: 50 },
+                    line: 51,
+                    column: 1,
+                    endLine: 61,
+                    endColumn: 2
+                }
             ]
         },
 
@@ -219,7 +279,14 @@ if ( x === y ) {
             code: "function name() {\nvar x = 5;\n\t\n \n\nvar x = 2;\n}",
             options: [{ max: 6, skipComments: false, skipBlankLines: false }],
             errors: [
-                { messageId: "exceed", data: { name: "Function 'name'", lineCount: 7, maxLines: 6 } }
+                {
+                    messageId: "exceed",
+                    data: { name: "Function 'name'", linesExceed: 1, maxLines: 6 },
+                    line: 7,
+                    column: 1,
+                    endLine: 7,
+                    endColumn: 2
+                }
             ]
         },
 
@@ -228,7 +295,14 @@ if ( x === y ) {
             code: "function name() {\r\nvar x = 5;\r\n\t\r\n \r\n\r\nvar x = 2;\r\n}",
             options: [{ max: 6, skipComments: true, skipBlankLines: false }],
             errors: [
-                { messageId: "exceed", data: { name: "Function 'name'", lineCount: 7, maxLines: 6 } }
+                {
+                    messageId: "exceed",
+                    data: { name: "Function 'name'", linesExceed: 1, maxLines: 6 },
+                    line: 7,
+                    column: 1,
+                    endLine: 7,
+                    endColumn: 2
+                }
             ]
         },
 
@@ -237,7 +311,14 @@ if ( x === y ) {
             code: "function name() {\nvar x = 5;\n\t\n \n\nvar x = 2;\n}",
             options: [{ max: 2, skipComments: true, skipBlankLines: true }],
             errors: [
-                { messageId: "exceed", data: { name: "Function 'name'", lineCount: 4, maxLines: 2 } }
+                {
+                    messageId: "exceed",
+                    data: { name: "Function 'name'", linesExceed: 2, maxLines: 2 },
+                    line: 6,
+                    column: 1,
+                    endLine: 7,
+                    endColumn: 2
+                }
             ]
         },
 
@@ -246,7 +327,14 @@ if ( x === y ) {
             code: "function name() {\r\nvar x = 5;\r\n\t\r\n \r\n\r\nvar x = 2;\r\n}",
             options: [{ max: 2, skipComments: true, skipBlankLines: true }],
             errors: [
-                { messageId: "exceed", data: { name: "Function 'name'", lineCount: 4, maxLines: 2 } }
+                {
+                    messageId: "exceed",
+                    data: { name: "Function 'name'", linesExceed: 2, maxLines: 2 },
+                    line: 6,
+                    column: 1,
+                    endLine: 7,
+                    endColumn: 2
+                }
             ]
         },
 
@@ -255,7 +343,14 @@ if ( x === y ) {
             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}",
             options: [{ max: 6, skipComments: true, skipBlankLines: false }],
             errors: [
-                { messageId: "exceed", data: { name: "Function 'name'", lineCount: 7, maxLines: 6 } }
+                {
+                    messageId: "exceed",
+                    data: { name: "Function 'name'", linesExceed: 1, maxLines: 6 },
+                    line: 8,
+                    column: 1,
+                    endLine: 8,
+                    endColumn: 2
+                }
             ]
         },
 
@@ -264,7 +359,14 @@ if ( x === y ) {
             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}",
             options: [{ max: 1, skipComments: true, skipBlankLines: true }],
             errors: [
-                { messageId: "exceed", data: { name: "Function 'name'", lineCount: 4, maxLines: 1 } }
+                {
+                    messageId: "exceed",
+                    data: { name: "Function 'name'", linesExceed: 3, maxLines: 1 },
+                    line: 2,
+                    column: 1,
+                    endLine: 8,
+                    endColumn: 2
+                }
             ]
         },
 
@@ -273,7 +375,14 @@ if ( x === y ) {
             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}",
             options: [{ max: 1, skipComments: false, skipBlankLines: true }],
             errors: [
-                { messageId: "exceed", data: { name: "Function 'name'", lineCount: 5, maxLines: 1 } }
+                {
+                    messageId: "exceed",
+                    data: { name: "Function 'name'", linesExceed: 4, maxLines: 1 },
+                    line: 2,
+                    column: 1,
+                    endLine: 8,
+                    endColumn: 2
+                }
             ]
         },
 
@@ -288,7 +397,14 @@ if ( x === y ) {
 }`,
             options: [{ max: 2, skipComments: true, skipBlankLines: false }],
             errors: [
-                { messageId: "exceed", data: { name: "Function 'foo'", lineCount: 7, maxLines: 2 } }
+                {
+                    messageId: "exceed",
+                    data: { name: "Function 'foo'", linesExceed: 5, maxLines: 2 },
+                    line: 3,
+                    column: 1,
+                    endLine: 7,
+                    endColumn: 2
+                }
             ]
         },
 
@@ -303,7 +419,14 @@ function
 ()`,
             options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: true }],
             errors: [
-                { messageId: "exceed", data: { name: "Function", lineCount: 4, maxLines: 2 } }
+                {
+                    messageId: "exceed",
+                    data: { name: "Function", linesExceed: 2, maxLines: 2 },
+                    line: 4,
+                    column: 1,
+                    endLine: 5,
+                    endColumn: 2
+                }
             ]
         },
 
@@ -321,7 +444,14 @@ if ( x === y ) {
 }`,
             options: [{ max: 9, skipComments: true, skipBlankLines: false }],
             errors: [
-                { messageId: "exceed", data: { name: "Function 'parent'", lineCount: 10, maxLines: 9 } }
+                {
+                    messageId: "exceed",
+                    data: { name: "Function 'parent'", linesExceed: 1, maxLines: 9 },
+                    line: 10,
+                    column: 1,
+                    endLine: 10,
+                    endColumn: 2
+                }
             ]
         },
 
@@ -339,8 +469,22 @@ if ( x === y ) {
 }`,
             options: [{ max: 2, skipComments: true, skipBlankLines: false }],
             errors: [
-                { messageId: "exceed", data: { name: "Function 'parent'", lineCount: 10, maxLines: 2 } },
-                { messageId: "exceed", data: { name: "Function 'nested'", lineCount: 4, maxLines: 2 } }
+                {
+                    messageId: "exceed",
+                    data: { name: "Function 'parent'", linesExceed: 8, maxLines: 2 },
+                    line: 3,
+                    column: 1,
+                    endLine: 10,
+                    endColumn: 2
+                },
+                {
+                    messageId: "exceed",
+                    data: { name: "Function 'nested'", linesExceed: 2, maxLines: 2 },
+                    line: 5,
+                    column: 1,
+                    endLine: 6,
+                    endColumn: 2
+                }
             ]
         },
 
@@ -355,7 +499,14 @@ if ( x === y ) {
 }`,
             options: [{ max: 2, skipComments: true, skipBlankLines: false }],
             errors: [
-                { messageId: "exceed", data: { name: "Method 'method'", lineCount: 5, maxLines: 2 } }
+                {
+                    messageId: "exceed",
+                    data: { name: "Method 'method'", linesExceed: 3, maxLines: 2 },
+                    line: 4,
+                    column: 1,
+                    endLine: 6,
+                    endColumn: 6
+                }
             ]
         },
 
@@ -370,7 +521,14 @@ if ( x === y ) {
 }`,
             options: [{ max: 2, skipComments: true, skipBlankLines: false }],
             errors: [
-                { messageId: "exceed", data: { name: "Static method 'foo'", lineCount: 5, maxLines: 2 } }
+                {
+                    messageId: "exceed",
+                    data: { name: "Static method 'foo'", linesExceed: 3, maxLines: 2 },
+                    line: 4,
+                    column: 1,
+                    endLine: 6,
+                    endColumn: 6
+                }
             ]
         },
 
@@ -383,9 +541,16 @@ if ( x === y ) {
         return 1
     }
 }`,
-            options: [{ max: 2, skipComments: true, skipBlankLines: false }],
+            options: [{ max: 4, skipComments: true, skipBlankLines: false }],
             errors: [
-                { messageId: "exceed", data: { name: "Getter 'foo'", lineCount: 5, maxLines: 2 } }
+                {
+                    messageId: "exceed",
+                    data: { name: "Getter 'foo'", linesExceed: 1, maxLines: 4 },
+                    line: 6,
+                    column: 1,
+                    endLine: 6,
+                    endColumn: 6
+                }
             ]
         },
 
@@ -400,7 +565,14 @@ if ( x === y ) {
 }`,
             options: [{ max: 2, skipComments: true, skipBlankLines: false }],
             errors: [
-                { messageId: "exceed", data: { name: "Setter 'foo'", lineCount: 5, maxLines: 2 } }
+                {
+                    messageId: "exceed",
+                    data: { name: "Setter 'foo'", linesExceed: 3, maxLines: 2 },
+                    line: 4,
+                    column: 1,
+                    endLine: 6,
+                    endColumn: 6
+                }
             ]
         },
 
@@ -418,7 +590,14 @@ if ( x === y ) {
 }`,
             options: [{ max: 2, skipComments: true, skipBlankLines: false }],
             errors: [
-                { messageId: "exceed", data: { name: "Static method", lineCount: 8, maxLines: 2 } }
+                {
+                    messageId: "exceed",
+                    data: { name: "Static method", linesExceed: 6, maxLines: 2 },
+                    line: 4,
+                    column: 1,
+                    endLine: 9,
+                    endColumn: 6
+                }
             ]
         },
 
@@ -433,7 +612,50 @@ if ( x === y ) {
 }());`,
             options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: true }],
             errors: [
-                { messageId: "exceed", data: { name: "Function", lineCount: 7, maxLines: 2 } }
+                {
+                    messageId: "exceed",
+                    data: { name: "Function", linesExceed: 5, maxLines: 2 },
+                    line: 3,
+                    column: 1,
+                    endLine: 7,
+                    endColumn: 2
+                }
+            ]
+        },
+
+        // Test the IIFEs option includes arrow IIFEs
+        {
+            code: `(() => {
+    let x = 0;
+    let y = 0;
+    let z = x + y;
+    let foo = {};
+    return bar;
+})();`,
+            options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: true }],
+            errors: [
+                {
+                    messageId: "exceed",
+                    data: { name: "Arrow function", linesExceed: 5, maxLines: 2 },
+                    line: 3,
+                    column: 1,
+                    endLine: 7,
+                    endColumn: 2
+                }
+            ]
+        },
+        {
+            code: "\nfoo();\nbar();\nbaz();\nfunction name() {\nvar x = 5;\n/* comment 1 */\n/* comment 2 */\n\t\n \n\nvar x = 2;\n}\nquz();",
+            options: [{ max: 2, skipComments: true, skipBlankLines: true }],
+            errors: [
+                {
+                    messageId: "exceed",
+                    data: { name: "Function 'name'", linesExceed: 2, maxLines: 2 },
+                    line: 12,
+                    column: 1,
+                    endLine: 13,
+                    endColumn: 2
+                }
             ]
         }
     ]