pre-commit / pre-commit-hooks

Some out-of-the-box hooks for pre-commit
MIT License
5.2k stars 694 forks source link

[bug] end-of-file-fixer doesn't handle last line with spaces #997

Closed r-downing closed 9 months ago

r-downing commented 9 months ago

Not sure if this is intentional, but the end-of-file fixer adds an extra line if the final line has spaces. Technically, combined with the trailing-whitespace check and running two passes, it would eventually fix it, but since it's called end-of-file-fixer perhaps it should handle this case on its own? I could put up a PR if you think it's worth fixing

File with spaces on last line: image

After running end-of-file-fixer (added a line): image

With pre-commit hooks in this order:

    hooks:
      - id: end-of-file-fixer
      - id: trailing-whitespace

You would actually have to run and stage twice. It would first add the extra line, then the trailing space would be removed from the second-to-last line, so you'd end up with 2 newlines at the end

After running with first pass: image

asottile commented 9 months ago

end-of-file-fixer intentionally only handles newlines

garbage in, garbage out -- you should also use trailing-whitespace

r-downing commented 9 months ago

Perhaps a warning in the readme that trailing-whitespace has to be run first?