Knap: An Automated Tool for Finding Unused Files, Exports, and Dependencies

Comments · 41 Views

Most of the projects have at least a few unused files, exports, and dependencies lying around, often because it’s difficult knowing when one thing relies on another and scary removing something you’re not sure is in use. Lars Kipper shares a tool he’s been working on that offers a solution — meet Knap that, at its most basic, scours projects for these unused artifacts.

Lots of the boring and repetitive things, like formatting and lining, are mostly solved problems with tools like Prettier, Slant, and Typescript.

Yet there’s another area that often doesn’t receive much attention: handling unused files, exports, and dependencies. This is especially true as projects grow over time, negatively impacting maintainability as well as our enthusiasm for it. These unused artifacts often go unnoticed because they’re typically hard to find.

Where do you start looking for unused things? I bet you’ve done a global search for a dependency to find out whether it’s still used. Did you know you can right-click a file and “Find File References” in VS Code? These are the things you shouldn’t have to do. They’re tedious tasks that can — and should — be automated.

There’s got to be a Better Way #

When I was in an expanding codebase, the number of unused files and exports in it kept growing. Tracking them became more and more difficult, so I started looking for automated solutions to help.

I was happy I found some existing tools, such as test-prune. After some initial runs, I discovered that our codebase required a lot of configuration and still produced many false positives. I researched how other codebases try to stay tidy and realized there’s a huge opportunity in this space.

Along the road, I also found recheck, which deals with a lot of complexity and customizations in JavaScript projects. And then there’s unimproved, which also does a notable job of finding dangling files and unused dependencies.

But none of these tools handled my project very well. The fact that I would have to use a combination of them wouldn’t be a showstopper, but I couldn’t get the configurations right for handling the project’s customizations without reporting too many false positives or ignoring too much of the project and leaving a large blind spot.

 

In the end, tools in this area are only used when they are fully automated and able to cover the whole project. It also didn’t help that none of the existing tools support monographs, a structure for repositories that has recently gained widespread popularity. Last but not least, both test-prune and recheck are in maintenance mode, meaning they would likely never support monographs.

disclaimer
Comments