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
32 changes: 28 additions & 4 deletions lib/rules/max-lines-per-function.js
Expand Up @@ -79,13 +79,16 @@ 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 is {{maxLines}}."
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
The-x-Theorist marked this conversation as resolved.
Show resolved Hide resolved
}
},

create(context) {
const sourceCode = context.getSourceCode();
const lines = sourceCode.lines;
const lines = sourceCode.lines.map((text, i) => ({
lineNumber: i + 1,
text
}));
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
The-x-Theorist marked this conversation as resolved.
Show resolved Hide resolved

const option = context.options[0];
let maxLines = 50;
Expand Down Expand Up @@ -169,18 +172,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];
const line = lines[i].text;
The-x-Theorist marked this conversation as resolved.
Show resolved Hide resolved

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 +201,24 @@ module.exports = {

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

const loc = {
start: {
line: (lines[node.loc.start.line].lineNumber - 1) + maxLines + (comments + blankLines),
The-x-Theorist marked this conversation as resolved.
Show resolved Hide resolved
column: 0
},
end: {
line: node.loc.end.line,
column: node.loc.end.column
}
The-x-Theorist marked this conversation as resolved.
Show resolved Hide resolved
};

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