From bb1f228d2ea4f23af64f9cc356a192f123a72f62 Mon Sep 17 00:00:00 2001 From: The-x-Theorist Date: Thu, 7 Oct 2021 22:26:30 +0530 Subject: [PATCH 01/13] Update: reports exceeded lines (fixes #15098) --- lib/rules/max-lines-per-function.js | 5 ++- tests/lib/rules/max-lines-per-function.js | 48 +++++++++++------------ 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/lib/rules/max-lines-per-function.js b/lib/rules/max-lines-per-function.js index b2130ca260b..f792dec94d1 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 ba01206f4a3..bee372d8dce 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 } } ] } ] From 0d3f95b6de7820d16fb6375884ddb9c7b3f76a7e Mon Sep 17 00:00:00 2001 From: Sneh Khatri <75059911+The-x-Theorist@users.noreply.github.com> Date: Fri, 22 Oct 2021 23:07:32 +0530 Subject: [PATCH 02/13] Update lib/rules/max-lines-per-function.js Co-authored-by: Nitin Kumar --- lib/rules/max-lines-per-function.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/max-lines-per-function.js b/lib/rules/max-lines-per-function.js index f792dec94d1..2a9f24e8962 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 exceeded the line limit by ({{linesExceed}}). Maximum allowed is {{maxLines}}." + exceed: "{{name}} has exceeded the limit of lines allowed by ({{linesExceed}}). Maximum allowed is {{maxLines}}." } }, From d195b10d1c8d6b93f5f5aab17d245d61c0a9ddf1 Mon Sep 17 00:00:00 2001 From: Sneh Khatri Date: Fri, 29 Oct 2021 14:18:06 +0530 Subject: [PATCH 03/13] Update: added loc property to context.report() --- lib/rules/max-lines-per-function.js | 21 ++- tests/lib/rules/max-lines-per-function.js | 216 +++++++++++++++++++--- 2 files changed, 210 insertions(+), 27 deletions(-) diff --git a/lib/rules/max-lines-per-function.js b/lib/rules/max-lines-per-function.js index 2a9f24e8962..6ca80a2708c 100644 --- a/lib/rules/max-lines-per-function.js +++ b/lib/rules/max-lines-per-function.js @@ -85,7 +85,10 @@ module.exports = { create(context) { const sourceCode = context.getSourceCode(); - const lines = sourceCode.lines; + const lines = sourceCode.lines.map((text, i) => ({ + lineNumber: i + 1, + text + })); const option = context.options[0]; let maxLines = 50; @@ -171,7 +174,7 @@ module.exports = { let lineCount = 0; for (let i = node.loc.start.line - 1; i < node.loc.end.line; ++i) { - const line = lines[i]; + const line = lines[i].text; if (skipComments) { if (commentLineNumbers.has(i + 1) && isFullLineComment(line, i + 1, commentLineNumbers.get(i + 1))) { @@ -191,9 +194,21 @@ module.exports = { if (lineCount > maxLines) { const name = upperCaseFirst(astUtils.getFunctionNameWithKind(funcNode)); const linesExceed = lineCount - maxLines; + const srcCodeLinesLength = [...sourceCode.lines].pop().length; + + const loc = { + start: { + line: lines[maxLines].lineNumber, + column: 0 + }, + end: { + line: sourceCode.lines.length, + column: srcCodeLinesLength + } + }; context.report({ - node, + loc, messageId: "exceed", 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 bee372d8dce..098305e3e08 100644 --- a/tests/lib/rules/max-lines-per-function.js +++ b/tests/lib/rules/max-lines-per-function.js @@ -199,7 +199,14 @@ if ( x === y ) { code: "function name() {\n}", options: [1], errors: [ - { messageId: "exceed", data: { name: "Function 'name'", linesExceed: 1, maxLines: 1 } } + { + messageId: "exceed", + data: { name: "Function 'name'", linesExceed: 1, maxLines: 1 }, + line: 2, + column: 1, + endLine: 2, + endColumn: 2 + } ] }, @@ -208,7 +215,14 @@ if ( x === y ) { code: "var func = function() {\n}", options: [1], errors: [ - { messageId: "exceed", data: { name: "Function", linesExceed: 1, maxLines: 1 } } + { + messageId: "exceed", + data: { name: "Function", linesExceed: 1, maxLines: 1 }, + line: 2, + column: 1, + endLine: 2, + endColumn: 2 + } ] }, @@ -217,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", linesExceed: 1, maxLines: 3 } } + { + messageId: "exceed", + data: { name: "Arrow function", linesExceed: 1, maxLines: 3 }, + line: 4, + column: 1, + endLine: 4, + endColumn: 2 + } ] }, @@ -226,7 +247,14 @@ if ( x === y ) { code: "const bar = () =>\n 2", options: [1], errors: [ - { messageId: "exceed", data: { name: "Arrow function", linesExceed: 1, maxLines: 1 } } + { + messageId: "exceed", + data: { name: "Arrow function", linesExceed: 1, maxLines: 1 }, + line: 2, + column: 1, + endLine: 2, + endColumn: 3 + } ] }, @@ -235,7 +263,14 @@ if ( x === y ) { code: `() => {${"foo\n".repeat(60)}}`, options: [{}], errors: [ - { messageId: "exceed", data: { name: "Arrow function", linesExceed: 11, maxLines: 50 } } + { + messageId: "exceed", + data: { name: "Arrow function", linesExceed: 11, maxLines: 50 }, + line: 51, + column: 1, + endLine: 61, + endColumn: 2 + } ] }, @@ -244,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'", linesExceed: 1, maxLines: 6 } } + { + messageId: "exceed", + data: { name: "Function 'name'", linesExceed: 1, maxLines: 6 }, + line: 7, + column: 1, + endLine: 7, + endColumn: 2 + } ] }, @@ -253,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'", linesExceed: 1, maxLines: 6 } } + { + messageId: "exceed", + data: { name: "Function 'name'", linesExceed: 1, maxLines: 6 }, + line: 7, + column: 1, + endLine: 7, + endColumn: 2 + } ] }, @@ -262,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'", linesExceed: 2, maxLines: 2 } } + { + messageId: "exceed", + data: { name: "Function 'name'", linesExceed: 2, maxLines: 2 }, + line: 3, + column: 1, + endLine: 7, + endColumn: 2 + } ] }, @@ -271,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'", linesExceed: 2, maxLines: 2 } } + { + messageId: "exceed", + data: { name: "Function 'name'", linesExceed: 2, maxLines: 2 }, + line: 3, + column: 1, + endLine: 7, + endColumn: 2 + } ] }, @@ -280,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'", linesExceed: 1, maxLines: 6 } } + { + messageId: "exceed", + data: { name: "Function 'name'", linesExceed: 1, maxLines: 6 }, + line: 7, + column: 1, + endLine: 8, + endColumn: 2 + } ] }, @@ -289,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'", linesExceed: 3, maxLines: 1 } } + { + messageId: "exceed", + data: { name: "Function 'name'", linesExceed: 3, maxLines: 1 }, + line: 2, + column: 1, + endLine: 8, + endColumn: 2 + } ] }, @@ -298,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'", linesExceed: 4, maxLines: 1 } } + { + messageId: "exceed", + data: { name: "Function 'name'", linesExceed: 4, maxLines: 1 }, + line: 2, + column: 1, + endLine: 8, + endColumn: 2 + } ] }, @@ -313,7 +397,14 @@ if ( x === y ) { }`, options: [{ max: 2, skipComments: true, skipBlankLines: false }], errors: [ - { messageId: "exceed", data: { name: "Function 'foo'", linesExceed: 5, maxLines: 2 } } + { + messageId: "exceed", + data: { name: "Function 'foo'", linesExceed: 5, maxLines: 2 }, + line: 3, + column: 1, + endLine: 7, + endColumn: 2 + } ] }, @@ -328,7 +419,14 @@ function ()`, options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: true }], errors: [ - { messageId: "exceed", data: { name: "Function", linesExceed: 2, maxLines: 2 } } + { + messageId: "exceed", + data: { name: "Function", linesExceed: 2, maxLines: 2 }, + line: 3, + column: 1, + endLine: 7, + endColumn: 3 + } ] }, @@ -346,7 +444,14 @@ if ( x === y ) { }`, options: [{ max: 9, skipComments: true, skipBlankLines: false }], errors: [ - { messageId: "exceed", data: { name: "Function 'parent'", linesExceed: 1, maxLines: 9 } } + { + messageId: "exceed", + data: { name: "Function 'parent'", linesExceed: 1, maxLines: 9 }, + line: 10, + column: 1, + endLine: 10, + endColumn: 2 + } ] }, @@ -364,8 +469,22 @@ if ( x === y ) { }`, options: [{ max: 2, skipComments: true, skipBlankLines: false }], errors: [ - { messageId: "exceed", data: { name: "Function 'parent'", linesExceed: 8, maxLines: 2 } }, - { messageId: "exceed", data: { name: "Function 'nested'", linesExceed: 2, 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: 3, + column: 1, + endLine: 10, + endColumn: 2 + } ] }, @@ -380,7 +499,14 @@ if ( x === y ) { }`, options: [{ max: 2, skipComments: true, skipBlankLines: false }], errors: [ - { messageId: "exceed", data: { name: "Method 'method'", linesExceed: 3, maxLines: 2 } } + { + messageId: "exceed", + data: { name: "Method 'method'", linesExceed: 3, maxLines: 2 }, + line: 3, + column: 1, + endLine: 7, + endColumn: 2 + } ] }, @@ -395,7 +521,14 @@ if ( x === y ) { }`, options: [{ max: 2, skipComments: true, skipBlankLines: false }], errors: [ - { messageId: "exceed", data: { name: "Static method 'foo'", linesExceed: 3, maxLines: 2 } } + { + messageId: "exceed", + data: { name: "Static method 'foo'", linesExceed: 3, maxLines: 2 }, + line: 3, + column: 1, + endLine: 7, + endColumn: 2 + } ] }, @@ -410,7 +543,14 @@ if ( x === y ) { }`, options: [{ max: 2, skipComments: true, skipBlankLines: false }], errors: [ - { messageId: "exceed", data: { name: "Getter 'foo'", linesExceed: 3, maxLines: 2 } } + { + messageId: "exceed", + data: { name: "Getter 'foo'", linesExceed: 3, maxLines: 2 }, + line: 3, + column: 1, + endLine: 7, + endColumn: 2 + } ] }, @@ -425,7 +565,14 @@ if ( x === y ) { }`, options: [{ max: 2, skipComments: true, skipBlankLines: false }], errors: [ - { messageId: "exceed", data: { name: "Setter 'foo'", linesExceed: 3, maxLines: 2 } } + { + messageId: "exceed", + data: { name: "Setter 'foo'", linesExceed: 3, maxLines: 2 }, + line: 3, + column: 1, + endLine: 7, + endColumn: 2 + } ] }, @@ -443,7 +590,14 @@ if ( x === y ) { }`, options: [{ max: 2, skipComments: true, skipBlankLines: false }], errors: [ - { messageId: "exceed", data: { name: "Static method", linesExceed: 6, maxLines: 2 } } + { + messageId: "exceed", + data: { name: "Static method", linesExceed: 6, maxLines: 2 }, + line: 3, + column: 1, + endLine: 10, + endColumn: 2 + } ] }, @@ -458,7 +612,14 @@ if ( x === y ) { }());`, options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: true }], errors: [ - { messageId: "exceed", data: { name: "Function", linesExceed: 5, maxLines: 2 } } + { + messageId: "exceed", + data: { name: "Function", linesExceed: 5, maxLines: 2 }, + line: 3, + column: 1, + endLine: 7, + endColumn: 6 + } ] }, @@ -473,7 +634,14 @@ if ( x === y ) { })();`, options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: true }], errors: [ - { messageId: "exceed", data: { name: "Arrow function", linesExceed: 5, maxLines: 2 } } + { + messageId: "exceed", + data: { name: "Arrow function", linesExceed: 5, maxLines: 2 }, + line: 3, + column: 1, + endLine: 7, + endColumn: 6 + } ] } ] From 06ec633f325693aa457a67b335e8ae5c64fea910 Mon Sep 17 00:00:00 2001 From: Sneh Khatri Date: Tue, 2 Nov 2021 12:20:49 +0530 Subject: [PATCH 04/13] Update lib/rules/max-lines-per-function.js Co-authored-by: Milos Djermanovic --- lib/rules/max-lines-per-function.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/rules/max-lines-per-function.js b/lib/rules/max-lines-per-function.js index 6ca80a2708c..585e1fd601d 100644 --- a/lib/rules/max-lines-per-function.js +++ b/lib/rules/max-lines-per-function.js @@ -201,10 +201,7 @@ module.exports = { line: lines[maxLines].lineNumber, column: 0 }, - end: { - line: sourceCode.lines.length, - column: srcCodeLinesLength - } + end: node.loc.end }; context.report({ From 7dd905d5abc5860019a9ac2b88f7bd98f5daa9db Mon Sep 17 00:00:00 2001 From: Sneh Khatri Date: Tue, 2 Nov 2021 12:48:30 +0530 Subject: [PATCH 05/13] Update: loc property improved --- lib/rules/max-lines-per-function.js | 4 +- tests/lib/rules/max-lines-per-function.js | 216 +++------------------- 2 files changed, 26 insertions(+), 194 deletions(-) diff --git a/lib/rules/max-lines-per-function.js b/lib/rules/max-lines-per-function.js index 585e1fd601d..7b07cdeaf76 100644 --- a/lib/rules/max-lines-per-function.js +++ b/lib/rules/max-lines-per-function.js @@ -194,17 +194,17 @@ module.exports = { if (lineCount > maxLines) { const name = upperCaseFirst(astUtils.getFunctionNameWithKind(funcNode)); const linesExceed = lineCount - maxLines; - const srcCodeLinesLength = [...sourceCode.lines].pop().length; const loc = { start: { - line: lines[maxLines].lineNumber, + line: maxLines + 1, column: 0 }, end: node.loc.end }; context.report({ + node, loc, messageId: "exceed", 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 098305e3e08..bee372d8dce 100644 --- a/tests/lib/rules/max-lines-per-function.js +++ b/tests/lib/rules/max-lines-per-function.js @@ -199,14 +199,7 @@ if ( x === y ) { code: "function name() {\n}", options: [1], errors: [ - { - messageId: "exceed", - data: { name: "Function 'name'", linesExceed: 1, maxLines: 1 }, - line: 2, - column: 1, - endLine: 2, - endColumn: 2 - } + { messageId: "exceed", data: { name: "Function 'name'", linesExceed: 1, maxLines: 1 } } ] }, @@ -215,14 +208,7 @@ if ( x === y ) { code: "var func = function() {\n}", options: [1], errors: [ - { - messageId: "exceed", - data: { name: "Function", linesExceed: 1, maxLines: 1 }, - line: 2, - column: 1, - endLine: 2, - endColumn: 2 - } + { messageId: "exceed", data: { name: "Function", linesExceed: 1, maxLines: 1 } } ] }, @@ -231,14 +217,7 @@ if ( x === y ) { code: "const bar = () => {\nconst x = 2 + 1;\nreturn x;\n}", options: [3], errors: [ - { - messageId: "exceed", - data: { name: "Arrow function", linesExceed: 1, maxLines: 3 }, - line: 4, - column: 1, - endLine: 4, - endColumn: 2 - } + { messageId: "exceed", data: { name: "Arrow function", linesExceed: 1, maxLines: 3 } } ] }, @@ -247,14 +226,7 @@ if ( x === y ) { code: "const bar = () =>\n 2", options: [1], errors: [ - { - messageId: "exceed", - data: { name: "Arrow function", linesExceed: 1, maxLines: 1 }, - line: 2, - column: 1, - endLine: 2, - endColumn: 3 - } + { messageId: "exceed", data: { name: "Arrow function", linesExceed: 1, maxLines: 1 } } ] }, @@ -263,14 +235,7 @@ if ( x === y ) { code: `() => {${"foo\n".repeat(60)}}`, options: [{}], errors: [ - { - messageId: "exceed", - data: { name: "Arrow function", linesExceed: 11, maxLines: 50 }, - line: 51, - column: 1, - endLine: 61, - endColumn: 2 - } + { messageId: "exceed", data: { name: "Arrow function", linesExceed: 11, maxLines: 50 } } ] }, @@ -279,14 +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'", linesExceed: 1, maxLines: 6 }, - line: 7, - column: 1, - endLine: 7, - endColumn: 2 - } + { messageId: "exceed", data: { name: "Function 'name'", linesExceed: 1, maxLines: 6 } } ] }, @@ -295,14 +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'", linesExceed: 1, maxLines: 6 }, - line: 7, - column: 1, - endLine: 7, - endColumn: 2 - } + { messageId: "exceed", data: { name: "Function 'name'", linesExceed: 1, maxLines: 6 } } ] }, @@ -311,14 +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'", linesExceed: 2, maxLines: 2 }, - line: 3, - column: 1, - endLine: 7, - endColumn: 2 - } + { messageId: "exceed", data: { name: "Function 'name'", linesExceed: 2, maxLines: 2 } } ] }, @@ -327,14 +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'", linesExceed: 2, maxLines: 2 }, - line: 3, - column: 1, - endLine: 7, - endColumn: 2 - } + { messageId: "exceed", data: { name: "Function 'name'", linesExceed: 2, maxLines: 2 } } ] }, @@ -343,14 +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'", linesExceed: 1, maxLines: 6 }, - line: 7, - column: 1, - endLine: 8, - endColumn: 2 - } + { messageId: "exceed", data: { name: "Function 'name'", linesExceed: 1, maxLines: 6 } } ] }, @@ -359,14 +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'", linesExceed: 3, maxLines: 1 }, - line: 2, - column: 1, - endLine: 8, - endColumn: 2 - } + { messageId: "exceed", data: { name: "Function 'name'", linesExceed: 3, maxLines: 1 } } ] }, @@ -375,14 +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'", linesExceed: 4, maxLines: 1 }, - line: 2, - column: 1, - endLine: 8, - endColumn: 2 - } + { messageId: "exceed", data: { name: "Function 'name'", linesExceed: 4, maxLines: 1 } } ] }, @@ -397,14 +313,7 @@ if ( x === y ) { }`, options: [{ max: 2, skipComments: true, skipBlankLines: false }], errors: [ - { - messageId: "exceed", - data: { name: "Function 'foo'", linesExceed: 5, maxLines: 2 }, - line: 3, - column: 1, - endLine: 7, - endColumn: 2 - } + { messageId: "exceed", data: { name: "Function 'foo'", linesExceed: 5, maxLines: 2 } } ] }, @@ -419,14 +328,7 @@ function ()`, options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: true }], errors: [ - { - messageId: "exceed", - data: { name: "Function", linesExceed: 2, maxLines: 2 }, - line: 3, - column: 1, - endLine: 7, - endColumn: 3 - } + { messageId: "exceed", data: { name: "Function", linesExceed: 2, maxLines: 2 } } ] }, @@ -444,14 +346,7 @@ if ( x === y ) { }`, options: [{ max: 9, skipComments: true, skipBlankLines: false }], errors: [ - { - messageId: "exceed", - data: { name: "Function 'parent'", linesExceed: 1, maxLines: 9 }, - line: 10, - column: 1, - endLine: 10, - endColumn: 2 - } + { messageId: "exceed", data: { name: "Function 'parent'", linesExceed: 1, maxLines: 9 } } ] }, @@ -469,22 +364,8 @@ if ( x === y ) { }`, options: [{ max: 2, skipComments: true, skipBlankLines: false }], errors: [ - { - 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: 3, - column: 1, - endLine: 10, - endColumn: 2 - } + { messageId: "exceed", data: { name: "Function 'parent'", linesExceed: 8, maxLines: 2 } }, + { messageId: "exceed", data: { name: "Function 'nested'", linesExceed: 2, maxLines: 2 } } ] }, @@ -499,14 +380,7 @@ if ( x === y ) { }`, options: [{ max: 2, skipComments: true, skipBlankLines: false }], errors: [ - { - messageId: "exceed", - data: { name: "Method 'method'", linesExceed: 3, maxLines: 2 }, - line: 3, - column: 1, - endLine: 7, - endColumn: 2 - } + { messageId: "exceed", data: { name: "Method 'method'", linesExceed: 3, maxLines: 2 } } ] }, @@ -521,14 +395,7 @@ if ( x === y ) { }`, options: [{ max: 2, skipComments: true, skipBlankLines: false }], errors: [ - { - messageId: "exceed", - data: { name: "Static method 'foo'", linesExceed: 3, maxLines: 2 }, - line: 3, - column: 1, - endLine: 7, - endColumn: 2 - } + { messageId: "exceed", data: { name: "Static method 'foo'", linesExceed: 3, maxLines: 2 } } ] }, @@ -543,14 +410,7 @@ if ( x === y ) { }`, options: [{ max: 2, skipComments: true, skipBlankLines: false }], errors: [ - { - messageId: "exceed", - data: { name: "Getter 'foo'", linesExceed: 3, maxLines: 2 }, - line: 3, - column: 1, - endLine: 7, - endColumn: 2 - } + { messageId: "exceed", data: { name: "Getter 'foo'", linesExceed: 3, maxLines: 2 } } ] }, @@ -565,14 +425,7 @@ if ( x === y ) { }`, options: [{ max: 2, skipComments: true, skipBlankLines: false }], errors: [ - { - messageId: "exceed", - data: { name: "Setter 'foo'", linesExceed: 3, maxLines: 2 }, - line: 3, - column: 1, - endLine: 7, - endColumn: 2 - } + { messageId: "exceed", data: { name: "Setter 'foo'", linesExceed: 3, maxLines: 2 } } ] }, @@ -590,14 +443,7 @@ if ( x === y ) { }`, options: [{ max: 2, skipComments: true, skipBlankLines: false }], errors: [ - { - messageId: "exceed", - data: { name: "Static method", linesExceed: 6, maxLines: 2 }, - line: 3, - column: 1, - endLine: 10, - endColumn: 2 - } + { messageId: "exceed", data: { name: "Static method", linesExceed: 6, maxLines: 2 } } ] }, @@ -612,14 +458,7 @@ if ( x === y ) { }());`, options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: true }], errors: [ - { - messageId: "exceed", - data: { name: "Function", linesExceed: 5, maxLines: 2 }, - line: 3, - column: 1, - endLine: 7, - endColumn: 6 - } + { messageId: "exceed", data: { name: "Function", linesExceed: 5, maxLines: 2 } } ] }, @@ -634,14 +473,7 @@ if ( x === y ) { })();`, 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: 6 - } + { messageId: "exceed", data: { name: "Arrow function", linesExceed: 5, maxLines: 2 } } ] } ] From 8858adc34a98d83c0af5cd8c01bb1ac93e3c7692 Mon Sep 17 00:00:00 2001 From: Sneh Khatri Date: Mon, 8 Nov 2021 12:27:10 +0530 Subject: [PATCH 06/13] higlights only the exceeded lines --- lib/rules/max-lines-per-function.js | 2 +- tests/lib/rules/max-lines-per-function.js | 246 +++++++++++++++++++--- 2 files changed, 222 insertions(+), 26 deletions(-) diff --git a/lib/rules/max-lines-per-function.js b/lib/rules/max-lines-per-function.js index 7b07cdeaf76..422e1fa4815 100644 --- a/lib/rules/max-lines-per-function.js +++ b/lib/rules/max-lines-per-function.js @@ -197,7 +197,7 @@ module.exports = { const loc = { start: { - line: maxLines + 1, + line: (lines[node.loc.start.line].lineNumber - 1) + maxLines, column: 0 }, end: node.loc.end diff --git a/tests/lib/rules/max-lines-per-function.js b/tests/lib/rules/max-lines-per-function.js index bee372d8dce..1e8ee605b8c 100644 --- a/tests/lib/rules/max-lines-per-function.js +++ b/tests/lib/rules/max-lines-per-function.js @@ -199,7 +199,14 @@ if ( x === y ) { code: "function name() {\n}", options: [1], errors: [ - { messageId: "exceed", data: { name: "Function 'name'", linesExceed: 1, maxLines: 1 } } + { + messageId: "exceed", + data: { name: "Function 'name'", linesExceed: 1, maxLines: 1 }, + line: 2, + column: 1, + endLine: 2, + endColumn: 2 + } ] }, @@ -208,7 +215,14 @@ if ( x === y ) { code: "var func = function() {\n}", options: [1], errors: [ - { messageId: "exceed", data: { name: "Function", linesExceed: 1, maxLines: 1 } } + { + messageId: "exceed", + data: { name: "Function", linesExceed: 1, maxLines: 1 }, + line: 2, + column: 1, + endLine: 2, + endColumn: 2 + } ] }, @@ -217,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", linesExceed: 1, maxLines: 3 } } + { + messageId: "exceed", + data: { name: "Arrow function", linesExceed: 1, maxLines: 3 }, + line: 4, + column: 1, + endLine: 4, + endColumn: 2 + } ] }, @@ -226,7 +247,14 @@ if ( x === y ) { code: "const bar = () =>\n 2", options: [1], errors: [ - { messageId: "exceed", data: { name: "Arrow function", linesExceed: 1, maxLines: 1 } } + { + messageId: "exceed", + data: { name: "Arrow function", linesExceed: 1, maxLines: 1 }, + line: 2, + column: 1, + endLine: 2, + endColumn: 3 + } ] }, @@ -235,7 +263,14 @@ if ( x === y ) { code: `() => {${"foo\n".repeat(60)}}`, options: [{}], errors: [ - { messageId: "exceed", data: { name: "Arrow function", linesExceed: 11, maxLines: 50 } } + { + messageId: "exceed", + data: { name: "Arrow function", linesExceed: 11, maxLines: 50 }, + line: 51, + column: 1, + endLine: 61, + endColumn: 2 + } ] }, @@ -244,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'", linesExceed: 1, maxLines: 6 } } + { + messageId: "exceed", + data: { name: "Function 'name'", linesExceed: 1, maxLines: 6 }, + line: 7, + column: 1, + endLine: 7, + endColumn: 2 + } ] }, @@ -253,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'", linesExceed: 1, maxLines: 6 } } + { + messageId: "exceed", + data: { name: "Function 'name'", linesExceed: 1, maxLines: 6 }, + line: 7, + column: 1, + endLine: 7, + endColumn: 2 + } ] }, @@ -262,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'", linesExceed: 2, maxLines: 2 } } + { + messageId: "exceed", + data: { name: "Function 'name'", linesExceed: 2, maxLines: 2 }, + line: 3, + column: 1, + endLine: 7, + endColumn: 2 + } ] }, @@ -271,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'", linesExceed: 2, maxLines: 2 } } + { + messageId: "exceed", + data: { name: "Function 'name'", linesExceed: 2, maxLines: 2 }, + line: 3, + column: 1, + endLine: 7, + endColumn: 2 + } ] }, @@ -280,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'", linesExceed: 1, maxLines: 6 } } + { + messageId: "exceed", + data: { name: "Function 'name'", linesExceed: 1, maxLines: 6 }, + line: 7, + column: 1, + endLine: 8, + endColumn: 2 + } ] }, @@ -289,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'", linesExceed: 3, maxLines: 1 } } + { + messageId: "exceed", + data: { name: "Function 'name'", linesExceed: 3, maxLines: 1 }, + line: 2, + column: 1, + endLine: 8, + endColumn: 2 + } ] }, @@ -298,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'", linesExceed: 4, maxLines: 1 } } + { + messageId: "exceed", + data: { name: "Function 'name'", linesExceed: 4, maxLines: 1 }, + line: 2, + column: 1, + endLine: 8, + endColumn: 2 + } ] }, @@ -313,7 +397,14 @@ if ( x === y ) { }`, options: [{ max: 2, skipComments: true, skipBlankLines: false }], errors: [ - { messageId: "exceed", data: { name: "Function 'foo'", linesExceed: 5, maxLines: 2 } } + { + messageId: "exceed", + data: { name: "Function 'foo'", linesExceed: 5, maxLines: 2 }, + line: 3, + column: 1, + endLine: 7, + endColumn: 2 + } ] }, @@ -328,7 +419,14 @@ function ()`, options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: true }], errors: [ - { messageId: "exceed", data: { name: "Function", linesExceed: 2, maxLines: 2 } } + { + messageId: "exceed", + data: { name: "Function", linesExceed: 2, maxLines: 2 }, + line: 4, + column: 1, + endLine: 5, + endColumn: 2 + } ] }, @@ -346,7 +444,14 @@ if ( x === y ) { }`, options: [{ max: 9, skipComments: true, skipBlankLines: false }], errors: [ - { messageId: "exceed", data: { name: "Function 'parent'", linesExceed: 1, maxLines: 9 } } + { + messageId: "exceed", + data: { name: "Function 'parent'", linesExceed: 1, maxLines: 9 }, + line: 10, + column: 1, + endLine: 10, + endColumn: 2 + } ] }, @@ -364,8 +469,22 @@ if ( x === y ) { }`, options: [{ max: 2, skipComments: true, skipBlankLines: false }], errors: [ - { messageId: "exceed", data: { name: "Function 'parent'", linesExceed: 8, maxLines: 2 } }, - { messageId: "exceed", data: { name: "Function 'nested'", linesExceed: 2, 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 + } ] }, @@ -380,7 +499,14 @@ if ( x === y ) { }`, options: [{ max: 2, skipComments: true, skipBlankLines: false }], errors: [ - { messageId: "exceed", data: { name: "Method 'method'", linesExceed: 3, maxLines: 2 } } + { + messageId: "exceed", + data: { name: "Method 'method'", linesExceed: 3, maxLines: 2 }, + line: 4, + column: 1, + endLine: 6, + endColumn: 6 + } ] }, @@ -395,7 +521,14 @@ if ( x === y ) { }`, options: [{ max: 2, skipComments: true, skipBlankLines: false }], errors: [ - { messageId: "exceed", data: { name: "Static method 'foo'", linesExceed: 3, maxLines: 2 } } + { + messageId: "exceed", + data: { name: "Static method 'foo'", linesExceed: 3, maxLines: 2 }, + line: 4, + column: 1, + endLine: 6, + endColumn: 6 + } ] }, @@ -408,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'", linesExceed: 3, maxLines: 2 } } + { + messageId: "exceed", + data: { name: "Getter 'foo'", linesExceed: 1, maxLines: 4 }, + line: 6, + column: 1, + endLine: 6, + endColumn: 6 + } ] }, @@ -425,7 +565,14 @@ if ( x === y ) { }`, options: [{ max: 2, skipComments: true, skipBlankLines: false }], errors: [ - { messageId: "exceed", data: { name: "Setter 'foo'", linesExceed: 3, maxLines: 2 } } + { + messageId: "exceed", + data: { name: "Setter 'foo'", linesExceed: 3, maxLines: 2 }, + line: 4, + column: 1, + endLine: 6, + endColumn: 6 + } ] }, @@ -443,7 +590,14 @@ if ( x === y ) { }`, options: [{ max: 2, skipComments: true, skipBlankLines: false }], errors: [ - { messageId: "exceed", data: { name: "Static method", linesExceed: 6, maxLines: 2 } } + { + messageId: "exceed", + data: { name: "Static method", linesExceed: 6, maxLines: 2 }, + line: 4, + column: 1, + endLine: 9, + endColumn: 6 + } ] }, @@ -458,7 +612,14 @@ if ( x === y ) { }());`, options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: true }], errors: [ - { messageId: "exceed", data: { name: "Function", linesExceed: 5, maxLines: 2 } } + { + messageId: "exceed", + data: { name: "Function", linesExceed: 5, maxLines: 2 }, + line: 3, + column: 1, + endLine: 7, + endColumn: 2 + } ] }, @@ -473,7 +634,42 @@ if ( x === y ) { })();`, options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: true }], errors: [ - { messageId: "exceed", data: { name: "Arrow function", linesExceed: 5, maxLines: 2 } } + { + messageId: "exceed", + data: { name: "Arrow function", linesExceed: 5, maxLines: 2 }, + line: 3, + column: 1, + endLine: 7, + endColumn: 2 + } + ] + }, + { + code: ` +foo(); +bar(); +baz(); +quz(); + +function f() { + 2 + 3 + 4 + 5 +} + +quux(); + `, + options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: true }], + errors: [ + { + messageId: "exceed", + data: { name: "Function 'f'", linesExceed: 4, maxLines: 2 }, + line: 9, + column: 1, + endLine: 12, + endColumn: 2 + } ] } ] From dfa0d3b6cd80235045437e75a8851e0059de46d5 Mon Sep 17 00:00:00 2001 From: Sneh Khatri Date: Tue, 9 Nov 2021 13:42:14 +0530 Subject: [PATCH 07/13] highlight only exceeded line with skipSomeLines too --- lib/rules/max-lines-per-function.js | 8 +++++- tests/lib/rules/max-lines-per-function.js | 34 +++++++---------------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/lib/rules/max-lines-per-function.js b/lib/rules/max-lines-per-function.js index 422e1fa4815..711589d7bfa 100644 --- a/lib/rules/max-lines-per-function.js +++ b/lib/rules/max-lines-per-function.js @@ -172,10 +172,13 @@ module.exports = { return; } let lineCount = 0; + let linesTotal = 0; for (let i = node.loc.start.line - 1; i < node.loc.end.line; ++i) { const line = lines[i].text; + linesTotal++; + if (skipComments) { if (commentLineNumbers.has(i + 1) && isFullLineComment(line, i + 1, commentLineNumbers.get(i + 1))) { continue; @@ -200,7 +203,10 @@ module.exports = { line: (lines[node.loc.start.line].lineNumber - 1) + maxLines, column: 0 }, - end: node.loc.end + end: { + line: node.loc.end.line - (linesTotal - lineCount), + column: node.loc.end.column + } }; context.report({ diff --git a/tests/lib/rules/max-lines-per-function.js b/tests/lib/rules/max-lines-per-function.js index 1e8ee605b8c..ddaf600cf00 100644 --- a/tests/lib/rules/max-lines-per-function.js +++ b/tests/lib/rules/max-lines-per-function.js @@ -316,7 +316,7 @@ if ( x === y ) { data: { name: "Function 'name'", linesExceed: 2, maxLines: 2 }, line: 3, column: 1, - endLine: 7, + endLine: 4, endColumn: 2 } ] @@ -332,7 +332,7 @@ if ( x === y ) { data: { name: "Function 'name'", linesExceed: 2, maxLines: 2 }, line: 3, column: 1, - endLine: 7, + endLine: 4, endColumn: 2 } ] @@ -348,7 +348,7 @@ if ( x === y ) { data: { name: "Function 'name'", linesExceed: 1, maxLines: 6 }, line: 7, column: 1, - endLine: 8, + endLine: 7, endColumn: 2 } ] @@ -364,7 +364,7 @@ if ( x === y ) { data: { name: "Function 'name'", linesExceed: 3, maxLines: 1 }, line: 2, column: 1, - endLine: 8, + endLine: 4, endColumn: 2 } ] @@ -380,7 +380,7 @@ if ( x === y ) { data: { name: "Function 'name'", linesExceed: 4, maxLines: 1 }, line: 2, column: 1, - endLine: 8, + endLine: 5, endColumn: 2 } ] @@ -645,29 +645,15 @@ if ( x === y ) { ] }, { - code: ` -foo(); -bar(); -baz(); -quz(); - -function f() { - 2 - 3 - 4 - 5 -} - -quux(); - `, - options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: true }], + code: "\nfoo();\nbar();\nbaz();\nfunction name() {\nvar x = 5;\n\t\n \n\nvar x = 2;\n}\nquz();", + options: [{ max: 2, skipComments: false, skipBlankLines: true }], errors: [ { messageId: "exceed", - data: { name: "Function 'f'", linesExceed: 4, maxLines: 2 }, - line: 9, + data: { name: "Function 'name'", linesExceed: 2, maxLines: 2 }, + line: 7, column: 1, - endLine: 12, + endLine: 8, endColumn: 2 } ] From dbf9de416a36b9d24207fa3240a5f7332da68c75 Mon Sep 17 00:00:00 2001 From: Sneh Khatri Date: Fri, 12 Nov 2021 16:16:49 +0530 Subject: [PATCH 08/13] skipComments working fine --- lib/rules/max-lines-per-function.js | 15 +++++++++----- tests/lib/rules/max-lines-per-function.js | 24 +++++++++++------------ 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/lib/rules/max-lines-per-function.js b/lib/rules/max-lines-per-function.js index 711589d7bfa..e60155865e7 100644 --- a/lib/rules/max-lines-per-function.js +++ b/lib/rules/max-lines-per-function.js @@ -172,21 +172,26 @@ module.exports = { return; } let lineCount = 0; - let linesTotal = 0; + let comments = 0; + let blankLines = 0; for (let i = node.loc.start.line - 1; i < node.loc.end.line; ++i) { const line = lines[i].text; - linesTotal++; - if (skipComments) { if (commentLineNumbers.has(i + 1) && isFullLineComment(line, i + 1, commentLineNumbers.get(i + 1))) { + if (lineCount <= maxLines) { + comments++; + } continue; } } if (skipBlankLines) { if (line.match(/^\s*$/u)) { + if (lineCount <= maxLines) { + blankLines++; + } continue; } } @@ -200,11 +205,11 @@ module.exports = { const loc = { start: { - line: (lines[node.loc.start.line].lineNumber - 1) + maxLines, + line: (lines[node.loc.start.line].lineNumber - 1) + maxLines + (comments + blankLines), column: 0 }, end: { - line: node.loc.end.line - (linesTotal - lineCount), + line: node.loc.end.line, column: node.loc.end.column } }; diff --git a/tests/lib/rules/max-lines-per-function.js b/tests/lib/rules/max-lines-per-function.js index ddaf600cf00..74acab2af2e 100644 --- a/tests/lib/rules/max-lines-per-function.js +++ b/tests/lib/rules/max-lines-per-function.js @@ -314,9 +314,9 @@ if ( x === y ) { { messageId: "exceed", data: { name: "Function 'name'", linesExceed: 2, maxLines: 2 }, - line: 3, + line: 6, column: 1, - endLine: 4, + endLine: 7, endColumn: 2 } ] @@ -330,9 +330,9 @@ if ( x === y ) { { messageId: "exceed", data: { name: "Function 'name'", linesExceed: 2, maxLines: 2 }, - line: 3, + line: 6, column: 1, - endLine: 4, + endLine: 7, endColumn: 2 } ] @@ -346,9 +346,9 @@ if ( x === y ) { { messageId: "exceed", data: { name: "Function 'name'", linesExceed: 1, maxLines: 6 }, - line: 7, + line: 8, column: 1, - endLine: 7, + endLine: 8, endColumn: 2 } ] @@ -364,7 +364,7 @@ if ( x === y ) { data: { name: "Function 'name'", linesExceed: 3, maxLines: 1 }, line: 2, column: 1, - endLine: 4, + endLine: 8, endColumn: 2 } ] @@ -380,7 +380,7 @@ if ( x === y ) { data: { name: "Function 'name'", linesExceed: 4, maxLines: 1 }, line: 2, column: 1, - endLine: 5, + endLine: 8, endColumn: 2 } ] @@ -645,15 +645,15 @@ if ( x === y ) { ] }, { - code: "\nfoo();\nbar();\nbaz();\nfunction name() {\nvar x = 5;\n\t\n \n\nvar x = 2;\n}\nquz();", - options: [{ max: 2, skipComments: false, skipBlankLines: true }], + 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: 7, + line: 12, column: 1, - endLine: 8, + endLine: 13, endColumn: 2 } ] From 2365c3071f2edeae86269b7cd277a17cf5fd80e2 Mon Sep 17 00:00:00 2001 From: Sneh Khatri Date: Fri, 26 Nov 2021 11:48:32 +0530 Subject: [PATCH 09/13] Update lib/rules/max-lines-per-function.js Co-authored-by: Milos Djermanovic --- lib/rules/max-lines-per-function.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/max-lines-per-function.js b/lib/rules/max-lines-per-function.js index e60155865e7..d1255edef07 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 exceeded the limit of lines allowed by ({{linesExceed}}). Maximum allowed is {{maxLines}}." + exceed: "{{name}} has exceeded the limit of lines allowed by {{linesExceed}}. Maximum allowed number of lines per function is {{maxLines}}." } }, From 67eb6ef3ed858cf3a4ab5aad443fe6a2c71bc457 Mon Sep 17 00:00:00 2001 From: Sneh Khatri Date: Fri, 26 Nov 2021 11:48:47 +0530 Subject: [PATCH 10/13] Update lib/rules/max-lines-per-function.js Co-authored-by: Milos Djermanovic --- lib/rules/max-lines-per-function.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/rules/max-lines-per-function.js b/lib/rules/max-lines-per-function.js index d1255edef07..8419c6dbfbc 100644 --- a/lib/rules/max-lines-per-function.js +++ b/lib/rules/max-lines-per-function.js @@ -85,10 +85,7 @@ module.exports = { create(context) { const sourceCode = context.getSourceCode(); - const lines = sourceCode.lines.map((text, i) => ({ - lineNumber: i + 1, - text - })); + const lines = sourceCode.lines; const option = context.options[0]; let maxLines = 50; From 81d27a4698b3a843a67bc6a47aa1ce832c45adcc Mon Sep 17 00:00:00 2001 From: Sneh Khatri Date: Fri, 26 Nov 2021 11:48:55 +0530 Subject: [PATCH 11/13] Update lib/rules/max-lines-per-function.js Co-authored-by: Milos Djermanovic --- lib/rules/max-lines-per-function.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/max-lines-per-function.js b/lib/rules/max-lines-per-function.js index 8419c6dbfbc..e962894afed 100644 --- a/lib/rules/max-lines-per-function.js +++ b/lib/rules/max-lines-per-function.js @@ -173,7 +173,7 @@ module.exports = { let blankLines = 0; for (let i = node.loc.start.line - 1; i < node.loc.end.line; ++i) { - const line = lines[i].text; + const line = lines[i]; if (skipComments) { if (commentLineNumbers.has(i + 1) && isFullLineComment(line, i + 1, commentLineNumbers.get(i + 1))) { From a05ab56f7e8033c83f39fdf1bb4ff1e86ce293c3 Mon Sep 17 00:00:00 2001 From: Sneh Khatri Date: Fri, 26 Nov 2021 11:49:02 +0530 Subject: [PATCH 12/13] Update lib/rules/max-lines-per-function.js Co-authored-by: Milos Djermanovic --- lib/rules/max-lines-per-function.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/max-lines-per-function.js b/lib/rules/max-lines-per-function.js index e962894afed..3e55864b5fa 100644 --- a/lib/rules/max-lines-per-function.js +++ b/lib/rules/max-lines-per-function.js @@ -202,7 +202,7 @@ module.exports = { const loc = { start: { - line: (lines[node.loc.start.line].lineNumber - 1) + maxLines + (comments + blankLines), + line: node.loc.start.line + maxLines + (comments + blankLines), column: 0 }, end: { From ce9cf36eb0031e1089f2111c48641c9ac9704bf2 Mon Sep 17 00:00:00 2001 From: Sneh Khatri Date: Fri, 26 Nov 2021 11:49:08 +0530 Subject: [PATCH 13/13] Update lib/rules/max-lines-per-function.js Co-authored-by: Milos Djermanovic --- lib/rules/max-lines-per-function.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/rules/max-lines-per-function.js b/lib/rules/max-lines-per-function.js index 3e55864b5fa..47423f889cc 100644 --- a/lib/rules/max-lines-per-function.js +++ b/lib/rules/max-lines-per-function.js @@ -205,10 +205,7 @@ module.exports = { line: node.loc.start.line + maxLines + (comments + blankLines), column: 0 }, - end: { - line: node.loc.end.line, - column: node.loc.end.column - } + end: node.loc.end }; context.report({