🎉 Announcing lint-staged with support for partially staged files

Written by okonetchnikov | Published 2018/10/29
Tech Story Tags: javascript | developer-experience | productivity | staged-files | lint-staged

TLDRvia the TL;DR App

Three years ago I’ve introduced lint-staged to the world. As of today, after 81 releases it’s in the dependency graph of more than 55.000 GitHub repositories and has surpassed 500.000 weekly downloads on NPM!

I would never expect lint-staged to become such a popular tool when I was releasing it back in 2015.

One issue that took me two years to fix

Shortly after lint-staged was introduced to react-boilerplate — a popular open source project by Max Stoiber, I’ve got an issue report entitled “Add better support for partially staged files”

This issue is number #62 (yes, I can remember its number without looking it up). This issue has lots of comments and lots of different opinions varying from “huh, it’s as simple as doing …” to “that’s impossible to achieve”. It’s so long that it would not fit into one single PNG file because it would exceed 30.000px — maximum allowed image height (sic!).

Screenshot of the issue #62 from GitHub. https://github.com/okonet/lint-staged/issues/62

I tried fixing the issue several times without luck. Last closest attempt was about a year ago on the train to AgentConf 2018 where I held a presentation about lint-staged.

Unfortunately, even though I spent almost a few days hacking on it I could not solve it. This work still had some positive outcomes:

  1. I meet a few people during the conference who encouraged and helped me to keep trying (thanks to Juho Vepsäläinen, Michel Weststrate, and Nejc Zdovc).
  2. I wrote lots of tests so it became easier to do TDD and try different implementation ideas (which I did a lot).

🎉 Introducing lint-staged v8

Today, after more than two years and 138 commits I’m very excited to finally release version 8 of lint-staged that supports partially staged files!

It uses the following algorithm:

  1. Before running linters, check if there are partially stated files (i.e. both with changes in commit index and the working copy)
  2. Stash and remove from the working copy all files that aren’t in the commit index
  3. Execute commands (linters and formatters) on these files
  4. Add formatters changes to index (separate task in the config)
  5. Restore the stashed working copy state if all linters exit with zero code (i.e. success). If there are errors running linters, restore stashed working copy.
  6. Compare new commit index and working copy and apply as many formatting changes to working copy as possible. If there are conflicting hunks, drop formatter’s changes. User modifications that were stashed should always take precedence over formatters.

And this is how it looks in action:

🚫💩 lint-staged with support for partially staged files_This is the demo of how partially staged files support works in 🚫💩 lint-staged. For more information see…_asciinema.org

Most importantly, y_ou don’t have to change your config to get support for partially staged files to work!_ The only “breaking change” in this version is the requirement of Node 8.6.

npm install --save-dev lint-staged@latest

I’ve been using it for more than a month on all my projects and consider it stable. If you happen to encounter issues with it, please report them on GitHub and help me fix them.

Takeaways

Don’t give up on your ideas!

People kept saying that the problem I’m trying to tackle is impossible to solve. Some part of me kept denying it… One thing I’ve learned is that it’s worth trying something if you believe into it.

Open Source is ❤️

The final solution is based on comments and ideas shared in the comments to the original issue and wouldn’t be possible without huge support and encouragement coming from people around the globe. I’d also like to thank contributors who help me maintain the project: Suhas Karanth and Lufty Wiranda.

Open Source is 💩

The fact it took me more than 2 years to find the solution is just ridiculous but it goes back to justifying this kind of work. I spent countless amount of hours looking for the final solution. It took me a few more days to finalize and clean up the code, merge changes from upstream, and write this blog post. Just in last 2 months I spent more than 50 hours on it. Dedicating this amount of time becomes harder and harder for me to justify, keeping in mind I’m a father of two.

If you think lint-staged is useful please consider funding the project at Open Collective.


Written by okonetchnikov | Improving designers & developers communication
Published by HackerNoon on 2018/10/29