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 1 commit
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
41 changes: 33 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,28 @@ 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)
);
}
const loc = {
start: {
line: max + 1,
column: 0
},
end: {
line: lines.length,
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
211 changes: 199 additions & 12 deletions tests/lib/rules/max-lines.js
Expand Up @@ -9,10 +9,8 @@
//------------------------------------------------------------------------------

const rule = require("../../../lib/rules/max-lines"),

{ RuleTester } = require("../../../lib/rule-tester");


//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -79,17 +77,38 @@ ruleTester.run("max-lines", rule, {
{
code: "var xyz;\nvar xyz;\nvar xyz;",
options: [2],
errors: [{ messageId: "exceed", data: { max: 2, actual: 3 } }]
errors: [
{
messageId: "exceed",
data: { max: 2, actual: 3 },
line: 3,
endLine: 3
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
}
]
},
{
code: "/* a multiline comment\n that goes to many lines*/\nvar xy;\nvar xy;",
options: [2],
errors: [{ messageId: "exceed", data: { max: 2, actual: 4 } }]
errors: [
{
messageId: "exceed",
data: { max: 2, actual: 4 },
line: 3,
endLine: 4
}
]
},
{
code: "//a single line comment\nvar xy;\nvar xy;",
options: [2],
errors: [{ messageId: "exceed", data: { max: 2, actual: 3 } }]
errors: [
{
messageId: "exceed",
data: { max: 2, actual: 3 },
line: 3,
endLine: 3
}
]
},
{
code: [
Expand All @@ -100,7 +119,14 @@ ruleTester.run("max-lines", rule, {
"var y;"
].join("\n"),
options: [{ max: 2 }],
errors: [{ messageId: "exceed", data: { max: 2, actual: 5 } }]
errors: [
{
messageId: "exceed",
data: { max: 2, actual: 5 },
line: 3,
endLine: 5
}
]
},
{
code: [
Expand All @@ -114,7 +140,14 @@ ruleTester.run("max-lines", rule, {
" long comment*/"
].join("\n"),
options: [{ max: 2, skipComments: true }],
errors: [{ messageId: "exceed", data: { max: 2, actual: 4 } }]
errors: [
{
messageId: "exceed",
data: { max: 2, actual: 4 },
line: 3,
endLine: 4
}
]
},
{
code: [
Expand All @@ -123,7 +156,14 @@ ruleTester.run("max-lines", rule, {
"var z;"
].join("\n"),
options: [{ max: 2, skipComments: true }],
errors: [{ messageId: "exceed", data: { max: 2, actual: 3 } }]
errors: [
{
messageId: "exceed",
data: { max: 2, actual: 3 },
line: 3,
endLine: 3
}
]
},
{
code: [
Expand All @@ -133,7 +173,14 @@ ruleTester.run("max-lines", rule, {
"var z;"
].join("\n"),
options: [{ max: 2, skipComments: true }],
errors: [{ messageId: "exceed", data: { max: 2, actual: 3 } }]
errors: [
{
messageId: "exceed",
data: { max: 2, actual: 3 },
line: 3,
endLine: 3
}
]
},
{
code: [
Expand All @@ -147,17 +194,157 @@ ruleTester.run("max-lines", rule, {
" long comment*/"
].join("\n"),
options: [{ max: 2, skipBlankLines: true }],
errors: [{ messageId: "exceed", data: { max: 2, actual: 6 } }]
errors: [
{
messageId: "exceed",
data: { max: 2, actual: 6 },
line: 3,
endLine: 6
}
]
},
{
code: "AAAAAAAA\n".repeat(301).trim(),
options: [{}],
errors: [{ messageId: "exceed", data: { max: 300, actual: 301 } }]
errors: [
{
messageId: "exceed",
data: { max: 300, actual: 301 },
line: 301,
endLine: 301
}
]
},
{
code: "A",
options: [{ max: 0 }],
errors: [{ messageId: "exceed", data: { max: 0, actual: 1 } }]
errors: [
{
messageId: "exceed",
data: { max: 0, actual: 1 },
line: 1,
endLine: 1
}
]
},
{
code: ["var a = 'a'; ", "var x", "var c;", "console.log"].join(
"\n"
),
options: [{ max: 2 }],
errors: [
{
messageId: "exceed",
data: { max: 2, actual: 4 },
line: 3,
column: 1,
endLine: 4,
endColumn: 1
}
]
},
{
code: "var a = 'a',\nc,\nx;\r",
options: [{ max: 2 }],
errors: [
{
messageId: "exceed",
data: { max: 2, actual: 4 },
line: 3,
column: 1,
endLine: 4,
endColumn: 1
}
]
},
{
code: "var a = 'a',\nc,\nx;\n",
options: [{ max: 2 }],
errors: [
{
messageId: "exceed",
data: { max: 2, actual: 4 },
line: 3,
column: 1,
endLine: 4,
endColumn: 1
}
]
},
{
code: "\n\nvar a = 'a',\nc,\nx;\n",
options: [{ max: 2, skipBlankLines: true }],
errors: [
{
messageId: "exceed",
data: { max: 2, actual: 3 },
line: 3,
column: 1,
endLine: 3,
endColumn: 1
}
]
},
{
code: [
"var a = 'a'; ",
"var x",
"var c;",
"console.log",
"// some block ",
"// comments"
].join("\n"),
options: [{ max: 2, skipComments: true }],
errors: [
{
messageId: "exceed",
data: { max: 2, actual: 4 },
line: 3,
column: 1,
endLine: 4,
endColumn: 1
}
]
},
{
code: [
"var a = 'a'; ",
"var x",
"var c;",
"console.log",
"/* block comments */"
].join("\n"),
options: [{ max: 2, skipComments: true }],
errors: [
{
messageId: "exceed",
data: { max: 2, actual: 4 },
line: 3,
column: 1,
endLine: 4,
endColumn: 1
}
]
},
{
code: [
"var a = 'a'; ",
"var x",
"var c;",
"console.log",
"/** block \n\n comments */"
].join("\n"),
options: [{ max: 2, skipComments: true }],
errors: [
{
messageId: "exceed",
data: { max: 2, actual: 4 },
line: 3,
column: 1,
endLine: 4,
endColumn: 1
}
]
}
]
});