question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prohibiting two consecutive newlines at EOF does not work as expected

See original GitHub issue

Tell us about your environment

  • ESLint Version: ^6.8.0
  • Node Version: 10.0.0
  • npm Version: 5.6.0

What parser (default, Babel-ESLint, etc.) are you using? default (?)

Please show your full configuration:

Configuration
{
  "env": {
    "es6": true,
    "node": true
  },
  "extends": [
    "airbnb-base"
  ],
  "globals": {
    "Atomics": "readonly",
    "SharedArrayBuffer": "readonly"
  },
  "parserOptions": {
    "ecmaVersion": 2018,
    "sourceType": "module"
  },
  "rules": {
    "no-multiple-empty-lines": ["error", { "max": 1 }]
  }
}

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

Source code:

// Source code of any .js file of the project

Command:

npx eslint .

What did you expect to happen? What actually happened?

I want to ensure there is exactly one newline at the end of each file. Since I am extending the Airbnb style guide, eol-last is already enabled, which afaik requires at least one newline at the end of the file. So all that’s left for me to specify is that more than one newlines at the end of the file are disallowed. I thought I might be able to accomplish this like so:

"no-multiple-empty-lines": ["error", { "max": 1 }]

Now, there should be both

  • the requirement of at least one newline at the end of the file, due to eol-last being enabled in the Airbnb style guide, as well as
  • the requirement of no more than one newline at a time (applying to the whole file, not just the end of the file), due to manually setting no-multiple-empty-lines to { "max": 1 }.

However, this still configuration seems to still allow two consecutive newlines at the end of the file.

In the Gitter chat room, it was recommended to set maxEOF to 1. This, however, does not change the fact that two newlines at the end of the file are being accepted. The fact that this does not fix the issue is to be expected, as max is already set to 1, so setting maxEOF to 1 also should not have an effect.

In the Gitter chat room it was concluded to file a bug report.

Are you willing to submit a pull request to fix this bug?

Maybe. It would be an honor but I’m not sure I can. I’ll take a look.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
mdjermanoviccommented, Jan 16, 2020

It seems we all agree that the rule is working as intended and that it would be great to improve the documentation, so I marked this as an accepted “documentation” issue.

1reaction
mdjermanoviccommented, Jan 5, 2020

maxEOF has to be set to 0 to enforce exactly one line at the EOF. Also, the documentation gives no clue about this.

I believe it’s technically zero empty lines at the EOF, but it looks like one empty line in most editors.

I agree this should be explained in the documentation. Also, I think there should be an example with maxEOF: 0 because it’s a common configuration for this rule. PR is welcome!

Just, I’m not sure how to avoid this confusion in maxEOF examples because the presentation depends on the editor/viewer.

For instance, if you open to view a source file on GitHub and see a code that looks exactly like the actual incorrect example in the docs (GitHub shows 8 lines, last 2 are empty), it’s indeed an incorrect code by this rule. If you open the same file in VSCode, there will be one extra line (VSCode shows 9 lines, last 3 are empty).

However, if you open another source file in VSCode, and see a code that looks exactly like the actual incorrect example in the docs (VSCode shows 8 lines, last 2 are empty), it’s actually a correct code by this rule.

image

It’s same in ESLint Online Demo

So, these examples can be indeed confusing.

Also, it looks that some other rules like max-lines inconsistently treat this issue, because it seems that SourceCode#lines does contain an extra line after the last linebreak.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Search for three consecutive newlines? - Unix Stack Exchange
One possible issue in either case is that a file with no existing x bytes will be considered as one very long line,...
Read more >
Remove newline from end of file - Stack Overflow
I would expect this not to work, at least not reliably, because eof only becomes true when you have tried to read past...
Read more >
no-multiple-empty-lines - ESLint - Pluggable JavaScript Linter
This rule has an object option: "max" (default: 2 ) enforces a maximum number of consecutive empty lines. "maxEOF" enforces a maximum number...
Read more >
command line - What is EOF and how to trigger it? - Ask Ubuntu
EOF does not 'consist in sending an "empty input"', and the SO answer you cite does not say otherwise. It is an out...
Read more >
sed, a stream editor - GNU.org
The following command modifies file.txt and does not produce any output: ... working as command separators and thus should be terminated with newlines...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found