DEV Community

Cover image for Clean up your code with ease
Viktor Pasynok
Viktor Pasynok

Posted on

Clean up your code with ease

Hi, I am the creator of the Pure Index tool. Its essential purpose is to clean your packages of unused exports with ease. In this article, I share a brief story of its inception.

I work in a monorepo that contains dozens of packages from which we build a website as a team. One of the problems we have encountered is dead code. For example, a TextCountDown component was written a couple of years ago, and the code where it was used was removed at some point. Ideally, the component should have been removed as well, but it wasn't. Yes, it was a mistake, but it happened, and we needed to find a way to prevent such situations in the future.

What tools would have helped at that point? ESLint? Perhaps, but ESLint would not have identified TextCountDown as unused because it was exported from a file. And that's where ts-prune came on the scene. Its first run was both a disappointment and a joy for us. The significant number of unused exports displayed in the console meant that removing them would reduce the code base's size, build time, etc. We removed hundreds of such exports and all the dead code in a few iterations. That's how it seemed to us...

Packages have index files (index.ts, for example) that describe what the package exports. ts-prune showed all exports from these files as unused, which was a false positive. Some exports were dead, perhaps whole packages, but not all. The reason was in the import paths. There was no direct connection between import {smth} from 'my-package' and export {smth} from '~/src/index.ts'. Maybe it was possible to somehow configure ts-prune to handle this, but we found a case that wouldn't help.
There is a ui-kit package whose code is stored in another repository, and the package itself is used in other products and internal systems.That's when I decided to create Pure Index. In combination with ts-prune, it allowed the automate the process of searching for dead code as follows:

  1. search for all unused exports from the index file
  2. delete them
  3. run ts-prune inside the package to remove the dead code

Finding unnecessary exports involves:

  • collect all package exports into exports Set
  • traverse all files where package import may occur
  • if the import is found, remove it from exports Set
  • if the size of the exports Set became equal to 0, then exit with success
  • if the exports Set size is not equal to 0, then exit with an error

Pure Index handles this task in ~400 ms even for large repositories, for example, when analyzing ui-kit in two repositories that contain more than 15'000+ .ts and tsx files. Of course, Pure Index's algorithm is a bit more complex. You can read about it in the "How Does It Work" section.

As an added bonus, Pure Index allows you to collect all X package imports within a repository. For example, you want to find out how many functions from lodash you use. Just run pure-index -u lodash, and you'll get a complete list.

Pure Index is available on GitHub. Just follow the link and start using it.

P.S. I will be happy if you give a star to the repository and share your opinion about the tool 🌿

Top comments (0)