Skip to content

Commit

Permalink
Fix: don't count line after EOF in max-lines (#13735)
Browse files Browse the repository at this point in the history
* Fix: don't count line after EOF in max-lines

* Update docs
  • Loading branch information
mdjermanovic committed Oct 9, 2020
1 parent d973675 commit fa9429a
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/rules/max-lines.md
Expand Up @@ -6,6 +6,7 @@ Some people consider large files a code smell. Large files tend to do a lot of t

This rule enforces a maximum number of lines per file, in order to aid in maintainability and reduce complexity.

Please note that most editors show an additional empty line at the end if the file ends with a line break. This rule does not count that extra line.

## Options

Expand Down
8 changes: 8 additions & 0 deletions lib/rules/max-lines.js
Expand Up @@ -131,6 +131,14 @@ module.exports = {
text
}));

/*
* If file ends with a linebreak, `sourceCode.lines` will have one extra empty line at the end.
* That isn't a real line, so we shouldn't count it.
*/
if (lines.length > 1 && lodash.last(lines).text === "") {
lines.pop();
}

if (skipBlankLines) {
lines = lines.filter(l => l.text.trim() !== "");
}
Expand Down
150 changes: 148 additions & 2 deletions tests/lib/rules/max-lines.js
Expand Up @@ -21,8 +21,15 @@ ruleTester.run("max-lines", rule, {
valid: [
"var x;",
"var xy;\nvar xy;",
{ code: "A", options: [1] },
{ code: "A\n", options: [1] },
{ code: "A\r", options: [1] },
{ code: "A\r\n", options: [1] },
{ code: "var xy;\nvar xy;", options: [2] },
{ code: "var xy;\nvar xy;\n", options: [2] },
{ code: "var xy;\nvar xy;", options: [{ max: 2 }] },
{ code: "// comment\n", options: [{ max: 0, skipComments: true }] },
{ code: "foo;\n /* comment */\n", options: [{ max: 1, skipComments: true }] },
{
code: [
"//a single line comment",
Expand Down Expand Up @@ -233,6 +240,50 @@ ruleTester.run("max-lines", rule, {
}
]
},
{

// Questionable. Makes sense to report this, and makes sense to not report this.
code: "",
options: [{ max: 0 }],
errors: [
{
messageId: "exceed",
data: { max: 0, actual: 1 },
line: 1,
column: 1,
endLine: 1,
endColumn: 1
}
]
},
{
code: " ",
options: [{ max: 0 }],
errors: [
{
messageId: "exceed",
data: { max: 0, actual: 1 },
line: 1,
column: 1,
endLine: 1,
endColumn: 2
}
]
},
{
code: "\n",
options: [{ max: 0 }],
errors: [
{
messageId: "exceed",
data: { max: 0, actual: 1 },
line: 1,
column: 1,
endLine: 2,
endColumn: 1
}
]
},
{
code: "A",
options: [{ max: 0 }],
Expand All @@ -247,6 +298,62 @@ ruleTester.run("max-lines", rule, {
}
]
},
{
code: "A\n",
options: [{ max: 0 }],
errors: [
{
messageId: "exceed",
data: { max: 0, actual: 1 },
line: 1,
column: 1,
endLine: 2,
endColumn: 1
}
]
},
{
code: "A\n ",
options: [{ max: 0 }],
errors: [
{
messageId: "exceed",
data: { max: 0, actual: 2 },
line: 1,
column: 1,
endLine: 2,
endColumn: 2
}
]
},
{
code: "A\n ",
options: [{ max: 1 }],
errors: [
{
messageId: "exceed",
data: { max: 1, actual: 2 },
line: 2,
column: 1,
endLine: 2,
endColumn: 2
}
]
},
{
code: "A\n\n",
options: [{ max: 1 }],
errors: [
{
messageId: "exceed",
data: { max: 1, actual: 2 },
line: 2,
column: 1,
endLine: 3,
endColumn: 1
}
]
},
{
code: ["var a = 'a'; ", "var x", "var c;", "console.log"].join(
"\n"
Expand All @@ -269,7 +376,7 @@ ruleTester.run("max-lines", rule, {
errors: [
{
messageId: "exceed",
data: { max: 2, actual: 4 },
data: { max: 2, actual: 3 },
line: 3,
column: 1,
endLine: 4,
Expand All @@ -283,7 +390,7 @@ ruleTester.run("max-lines", rule, {
errors: [
{
messageId: "exceed",
data: { max: 2, actual: 4 },
data: { max: 2, actual: 3 },
line: 3,
column: 1,
endLine: 4,
Expand Down Expand Up @@ -346,6 +453,26 @@ ruleTester.run("max-lines", rule, {
}
]
},
{
code: [
"var a = 'a'; ",
"var x",
"var c;",
"console.log",
"/* block comments */\n"
].join("\n"),
options: [{ max: 2, skipComments: true }],
errors: [
{
messageId: "exceed",
data: { max: 2, actual: 4 },
line: 3,
column: 1,
endLine: 6,
endColumn: 1
}
]
},
{
code: [
"var a = 'a'; ",
Expand All @@ -366,6 +493,25 @@ ruleTester.run("max-lines", rule, {
}
]
},
{
code: [
"var a = 'a'; ",
"",
"",
"// comment"
].join("\n"),
options: [{ max: 2, skipComments: true }],
errors: [
{
messageId: "exceed",
data: { max: 2, actual: 3 },
line: 3,
column: 1,
endLine: 4,
endColumn: 11
}
]
},
{
code: [
"var a = 'a'; ",
Expand Down

0 comments on commit fa9429a

Please sign in to comment.