Eslint trick: lint only changed files to upgrade project codestyle

The f*cking problem

What if you are working on a large project, and after some meetings with your lovely team mates you decided it’s time to sharpen the good ‘ol eslint config.

Eslint errors

Everything fails, you got 2042234897234 errors help, 911 please help. WTF.

So you try eslint –fix, but still 999239 errors.

Calm down, I got the perfect solution for you.

What if you could only run Eslint on the commited changed files 😉 ? And by doing so, you can slowly migrate everything to adopt to the new code style. Plus when you have some time left over, you could refactor that auto-fetch-nudes component to the new code style. Fucking awesome.

The importance of a consistent code style

Ok lets get serious:

I believe it’s very important that a development team maintains a consistent code style.

Now it doesn’t really matter too much what kind of rules you apply, but it is important to be consistent

We probably all know that Eslint helps us with checking if we are all following these rules. We can use stuff like Husky and lint-staged to automatically trigger linting on precommit or prepush hooks.

The solution, lint-staged

First we are going to install Husky and lint-staged and if you don’t already use it:

npm install husky lint-staged --save-dev

The next step is to setup your precommit hook in your package.json like so:

  "scripts": {
    "precommit": "lint-staged"
  "lint-staged": {
     "*.js": ["eslint --fix", "git add"]

Where the magic happens

So now each time you commit, husky triggers the precommit hook, which triggers lint-staged and triggers eslint fix on your staged files and runs git add. A cool thing to add as well is to add a prepush hook and run your unit tests for an even more solid codebase.

Final thoughts

Feel free to leave comments and don’t forget to bookmark Codersmind 😉



2 responses to “Eslint trick: lint only changed files to upgrade project codestyle”

  1. Awesome. Thank you.

    Only one problem, you will fix files using –fex key, but commit unfixed files in the case autofix made his job and there is no more lint errors.

    • Mike Vercoelen says:

      Hey man, I’ve updated the article to use lint-staged. A “new” library which simplified everything even more. Cheers

Leave a Reply

Your email address will not be published. Required fields are marked *