Filtering lint errors

— 2016-06-10 —

We all learn new stuff every day, that goes for how we write code as well. Over time our perception of best practices change, the codestyle we prefer change and what we would like to enforce with linters change.

Changing a linter configuration can be hard. The changes necessary to use the new configuration could be huge. You learned a new thing or a new lint rule finally got accepted and you want to start using the rule. When you add it to your project you are met with a result like the one below. This means that a significant amount of time must be allocated to refactoring the code to follow the rule upfront.

 ✖ 980 problems (976 errors, 4 warnings)

In a few of the projects I participate in we have started to use information from git to filter out the lint errors that does not apply to the current changeset. Resulting in the linter output above changing into the one listed below.

0 of 976 errors and 0 of 4 warnings

This has allowed us to change the our code guidelines without having to fix it right away. The impact has been great, as we have had the chance to alter our guidelines and make sure we do not write more code that does not follow the guidelines.

We made a tool, lint-filter, that will filter a checkstyle based lint report with the information in the git diff between the current state and the master branch. The way it works is that it will check that the lines referenced from the errors and warnings are in the git diff. Below is an example of how to use it with eslint.

npm install -g lint-filter
eslint . -f checkstyle | lint-filter

This will work for most errors. However, there is a few errors that would be triggered outside the changeset. One example is unused variables. In most cases it will not be cached by if you remove the last usage of a variable. To avoid these kinds of false negatives I recommend running another lint config as well. One were the extra rules are disabled. lint-filter will help you generate the configuration by running the following command.

lint-filter generate-config --linter eslint

Hope this helps you. Happy linting ☕