Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: report only lines that exceed the limit in max-lines-per-function #15140

Merged
merged 13 commits into from Dec 2, 2021
Merged
22 changes: 20 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 limit of lines allowed by {{linesExceed}}. Maximum allowed number of lines per function is {{maxLines}}."
}
},

Expand Down Expand Up @@ -169,18 +169,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 @@ -190,11 +198,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,
The-x-Theorist marked this conversation as resolved.
Show resolved Hide resolved
loc,
messageId: "exceed",
data: { name, lineCount, maxLines }
data: { name, linesExceed, maxLines }
});
}
}
Expand Down