diff --git a/lib/rules/max-lines-per-function.js b/lib/rules/max-lines-per-function.js index b2130ca260b4..f792dec94d1b 100644 --- a/lib/rules/max-lines-per-function.js +++ b/lib/rules/max-lines-per-function.js @@ -79,7 +79,7 @@ module.exports = { OPTIONS_OR_INTEGER_SCHEMA ], messages: { - exceed: "{{name}} has too many lines ({{lineCount}}). Maximum allowed is {{maxLines}}." + exceed: "{{name}} has exceeded the line limit by ({{linesExceed}}). Maximum allowed is {{maxLines}}." } }, @@ -190,11 +190,12 @@ module.exports = { if (lineCount > maxLines) { const name = upperCaseFirst(astUtils.getFunctionNameWithKind(funcNode)); + const linesExceed = lineCount - maxLines; context.report({ node, messageId: "exceed", - data: { name, lineCount, maxLines } + data: { name, linesExceed, maxLines } }); } } diff --git a/tests/lib/rules/max-lines-per-function.js b/tests/lib/rules/max-lines-per-function.js index ba01206f4a35..bee372d8dce1 100644 --- a/tests/lib/rules/max-lines-per-function.js +++ b/tests/lib/rules/max-lines-per-function.js @@ -199,7 +199,7 @@ 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 } } ] }, @@ -208,7 +208,7 @@ 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 } } ] }, @@ -217,7 +217,7 @@ 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 } } ] }, @@ -226,7 +226,7 @@ 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 } } ] }, @@ -235,7 +235,7 @@ 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 } } ] }, @@ -244,7 +244,7 @@ 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 } } ] }, @@ -253,7 +253,7 @@ 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 } } ] }, @@ -262,7 +262,7 @@ 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 } } ] }, @@ -271,7 +271,7 @@ 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 } } ] }, @@ -280,7 +280,7 @@ 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 } } ] }, @@ -289,7 +289,7 @@ 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 } } ] }, @@ -298,7 +298,7 @@ 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 } } ] }, @@ -313,7 +313,7 @@ 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 } } ] }, @@ -328,7 +328,7 @@ 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 } } ] }, @@ -346,7 +346,7 @@ 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 } } ] }, @@ -364,8 +364,8 @@ 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 } }, + { messageId: "exceed", data: { name: "Function 'nested'", linesExceed: 2, maxLines: 2 } } ] }, @@ -380,7 +380,7 @@ 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 } } ] }, @@ -395,7 +395,7 @@ 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 } } ] }, @@ -410,7 +410,7 @@ if ( x === y ) { }`, options: [{ max: 2, skipComments: true, skipBlankLines: false }], errors: [ - { messageId: "exceed", data: { name: "Getter 'foo'", lineCount: 5, maxLines: 2 } } + { messageId: "exceed", data: { name: "Getter 'foo'", linesExceed: 3, maxLines: 2 } } ] }, @@ -425,7 +425,7 @@ 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 } } ] }, @@ -443,7 +443,7 @@ 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 } } ] }, @@ -458,7 +458,7 @@ 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 } } ] }, @@ -473,7 +473,7 @@ if ( x === y ) { })();`, options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: true }], errors: [ - { messageId: "exceed", data: { name: "Arrow function", lineCount: 7, maxLines: 2 } } + { messageId: "exceed", data: { name: "Arrow function", linesExceed: 5, maxLines: 2 } } ] } ]