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

Conversation

mdjermanovic
Copy link
Member

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[ ] Documentation update
[x] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

Tell us about your environment

  • ESLint Version: v7.10.0
  • Node Version: v12.18.4
  • npm Version: v6.14.6

What parser (default, @babel/eslint-parser, @typescript-eslint/parser, etc.) are you using?

default

Please show your full configuration:

Configuration
module.exports = {
  parserOptions: {
    ecmaVersion: 2015
  }
};

What did you do? Please include the actual source code causing the issue.

Online Demo

/* eslint max-lines: ["error", { max: 3 }] */
second_line
third_line

There's a linebreak at the end of third_line, and nothing after that linebreak.

What did you expect to happen?

No errors. Editors may show 4 lines, but in this case there are only 3 real lines.

What actually happened? Please include the actual, raw output from ESLint.

1 error:

  4:1  error  File has too many lines (4). Maximum allowed is 3  max-lines

What changes did you make? (Give an overview)

Fixed the max-lines rule to not count line after EOF.

Is there anything you'd like reviewers to focus on?

@mdjermanovic mdjermanovic added bug ESLint is working incorrectly rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion labels Oct 3, 2020
@@ -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. 👍

Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

Code looks fine. Can you add some documentation about this?

@mdjermanovic
Copy link
Member Author

Code looks fine. Can you add some documentation about this?

Done!

Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

LGTM

@mdjermanovic mdjermanovic merged commit fa9429a into master Oct 9, 2020
@mdjermanovic mdjermanovic deleted the maxlines-eof branch October 9, 2020 13:15
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Apr 8, 2021
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Apr 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants