Hot to linter basic things like trailing whitespaces and newlines
For some time I was wondering why there is no linter for such simple things like trailing whitespaces or newlines at the end of file. And even wrote some prototypes on Python for these purposes.
Fortunately, solution already exists, though not really obvious.
Lets start with project called EditorConfig. It allows you easily define and maintain consistent coding styles between different editors and IDEs. You just need to install extension for your editor, for example EditorConfig for VS Code, and keep in repository file with the name
.editorconfig which looks like this:
trim_trailing_whitespace = true
insert_final_newline = true
It will ensure that all trailing whitespaces will be removed from file on saving. As well as final newline will be added.
But how to add linter for these things?
Just add to your CI/pre-commit-webhook/etc this utility — editorconfig-checker. It will read rules from
.editorconfig and fail if files in repo don’t pass them.
And if you’re using GitHub actions — it would be even easier, cause is already included in Super-Linter and Mega-Linter 🎉🎉🎉