Skip to content

Commit

Permalink
Update: max-lines reporting loc improvement (refs #12334) (#13318)
Browse files Browse the repository at this point in the history
* Update: max-lines reporting loc refactore (refs #12334)

* Update: changed loc position

* Update: updated startline logic and end loc

* Chore: refactor

* Chore: tests error property ordering

* Chore: tests re-ordering
  • Loading branch information
anikethsaha committed Jun 19, 2020
1 parent ee2fc2e commit 09cc0a2
Show file tree
Hide file tree
Showing 2 changed files with 342 additions and 20 deletions.
42 changes: 34 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,10 @@ 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
}));

if (skipBlankLines) {
lines = lines.filter(l => l.text.trim() !== "");
Expand All @@ -127,14 +138,29 @@ 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)
);
}

if (lines.length > max) {
const loc = {
start: {
line: lines[max].lineNumber,
column: 0
},
end: {
line: sourceCode.lines.length,
column: lodash.last(sourceCode.lines).length
}
};

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

0 comments on commit 09cc0a2

Please sign in to comment.