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

Update: max-lines reporting loc improvement (refs #12334) #13318

Merged
merged 6 commits into from Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 42 additions & 8 deletions lib/rules/max-lines.js
Expand Up @@ -53,15 +53,19 @@ module.exports = {
}
],
messages: {
exceed: "File has too many lines ({{actual}}). Maximum allowed is {{max}}."
exceed:
"File has too many lines ({{actual}}). Maximum allowed is {{max}}."
}
},

create(context) {
const option = context.options[0];
let max = 300;

if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "max")) {
if (
typeof option === "object" &&
Object.prototype.hasOwnProperty.call(option, "max")
) {
max = option.max;
} else if (typeof option === "number") {
max = option;
Expand Down Expand Up @@ -94,7 +98,9 @@ module.exports = {

token = comment;
do {
token = sourceCode.getTokenBefore(token, { includeComments: true });
token = sourceCode.getTokenBefore(token, {
includeComments: true
});
} while (isCommentNodeType(token));

if (token && astUtils.isTokenOnSameLine(token, comment)) {
Expand All @@ -103,7 +109,9 @@ module.exports = {

token = comment;
do {
token = sourceCode.getTokenAfter(token, { includeComments: true });
token = sourceCode.getTokenAfter(token, {
includeComments: true
});
} while (isCommentNodeType(token));

if (token && astUtils.isTokenOnSameLine(comment, token)) {
Expand All @@ -118,7 +126,12 @@ module.exports = {

return {
"Program:exit"() {
let lines = sourceCode.lines.map((text, i) => ({ lineNumber: i + 1, text }));
let lines = sourceCode.lines.map((text, i) => ({
lineNumber: i + 1,
text
}));

let startLine = 0;

if (skipBlankLines) {
lines = lines.filter(l => l.text.trim() !== "");
Expand All @@ -127,14 +140,35 @@ module.exports = {
if (skipComments) {
const comments = sourceCode.getAllComments();

const commentLines = lodash.flatten(comments.map(comment => getLinesWithoutCode(comment)));
const commentLines = lodash.flatten(
comments.map(comment => getLinesWithoutCode(comment))
);

lines = lines.filter(l => !lodash.includes(commentLines, l.lineNumber));
lines = lines.filter(
l => !lodash.includes(commentLines, l.lineNumber)
);
}

lines.forEach((line, i) => {
if (max + 1 === i + 1) {
startLine = line.lineNumber;
}
});
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved

const loc = {
start: {
line: startLine,
column: 0
},
end: {
line: sourceCode.lines.length + 1,
column: 0
}
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
};

if (lines.length > max) {
context.report({
loc: { line: 1, column: 0 },
loc,
messageId: "exceed",
data: {
max,
Expand Down