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

Fix: don't count line after EOF in max-lines #13735

Merged
merged 2 commits into from Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did this behavior exist before your change? I'd be in favor of assuming max: 0 is a mistake and normalizing to max: 1. I can't think of a reason we'd ever want 0 max lines in a file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's behavior in the actual version as well, I thought it would be better not to change that behavior in this PR and just added this test to make sure that this change doesn't affect max: 0 on an empty file, since that isn't technically an extra empty line after a linebreak.

I'm not sure why is it allowed to set max: 0, but it seems to be working like that from the start, and I couldn't find any concerns regarding that in the initial PR. There is also an existing test with max: 0.

The only use case for max: 0 I could think of is using ESLint to disallow any .js files in some directories, and for that cause it might make sense to treat an empty file as a file with 1 line.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I agree with not changing what it’s already doing. 👍

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