Skip to content

Commit

Permalink
feat: report only lines that exceed the limit in max-lines-per-functi…
Browse files Browse the repository at this point in the history
…on (#15140)

* Update: reports exceeded lines (fixes #15098)

* Update lib/rules/max-lines-per-function.js

Co-authored-by: Nitin Kumar <snitin315@gmail.com>

* Update: added loc property to context.report()

* Update lib/rules/max-lines-per-function.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update: loc property improved

* higlights only the exceeded lines

* highlight only exceeded line with skipSomeLines too

* skipComments working fine

* Update lib/rules/max-lines-per-function.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update lib/rules/max-lines-per-function.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update lib/rules/max-lines-per-function.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update lib/rules/max-lines-per-function.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update lib/rules/max-lines-per-function.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

Co-authored-by: Nitin Kumar <snitin315@gmail.com>
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
  • Loading branch information
3 people committed Dec 2, 2021
1 parent fa0423a commit 8f44cf5
Show file tree
Hide file tree
Showing 2 changed files with 227 additions and 27 deletions.
22 changes: 20 additions & 2 deletions lib/rules/max-lines-per-function.js
Expand Up @@ -80,7 +80,7 @@ module.exports = {
OPTIONS_OR_INTEGER_SCHEMA
],
messages: {
exceed: "{{name}} has too many lines ({{lineCount}}). Maximum allowed is {{maxLines}}."
exceed: "{{name}} has exceeded the limit of lines allowed by {{linesExceed}}. Maximum allowed number of lines per function is {{maxLines}}."
}
},

Expand Down Expand Up @@ -170,18 +170,26 @@ module.exports = {
return;
}
let lineCount = 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];

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;
}
}
Expand All @@ -191,11 +199,21 @@ module.exports = {

if (lineCount > maxLines) {
const name = upperCaseFirst(astUtils.getFunctionNameWithKind(funcNode));
const linesExceed = lineCount - maxLines;

const loc = {
start: {
line: node.loc.start.line + maxLines + (comments + blankLines),
column: 0
},
end: node.loc.end
};

context.report({
node,
loc,
messageId: "exceed",
data: { name, lineCount, maxLines }
data: { name, linesExceed, maxLines }
});
}
}
Expand Down

0 comments on commit 8f44cf5

Please sign in to comment.