Skip to content

Commit

Permalink
Update: report only the exceeded lines in max-lines-per-function (fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
The-x-Theorist committed Oct 7, 2021
1 parent d867f81 commit 10e715f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
5 changes: 3 additions & 2 deletions lib/rules/max-lines-per-function.js
Expand Up @@ -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}}."
}
},

Expand Down Expand Up @@ -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 }
});
}
}
Expand Down
48 changes: 24 additions & 24 deletions tests/lib/rules/max-lines-per-function.js
Expand Up @@ -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 } }
]
},

Expand All @@ -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 } }
]
},

Expand All @@ -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 } }
]
},

Expand All @@ -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 } }
]
},

Expand All @@ -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 } }
]
},

Expand All @@ -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 } }
]
},

Expand All @@ -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 } }
]
},

Expand All @@ -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 } }
]
},

Expand All @@ -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 } }
]
},

Expand All @@ -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 } }
]
},

Expand All @@ -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 } }
]
},

Expand All @@ -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 } }
]
},

Expand All @@ -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 } }
]
},

Expand All @@ -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 } }
]
},

Expand All @@ -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 } }
]
},

Expand All @@ -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 } }
]
},

Expand All @@ -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 } }
]
},

Expand All @@ -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 } }
]
},

Expand All @@ -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 } }
]
},

Expand All @@ -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 } }
]
},

Expand All @@ -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 } }
]
},

Expand All @@ -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 } }
]
},

Expand All @@ -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 } }
]
}
]
Expand Down

0 comments on commit 10e715f

Please sign in to comment.