Every now and then I'd like to apply a so called codemod to my a codebase. A codemod is a piece of code which modifies other code. Very often this is done by running some transformation on the abstract syntax tree (AST).
Whenever I need to do this I look for "What is the best way to apply a codemod on my TS code base right now?", because when I only do this every couple of month I either have forgotten about how to do it or tools have changed and my old way stopped working... or both. Surprisingly every time I search for that I found the existing tools bloated or quirky or not matching my workflow.
As I already use Babel to compile our source code I would like to create a Babel plugin which transforms my code. Sadly Babel alone is not good in preserving whitespace and formatting. Thankfully I use Prettier for that and because Prettier uses Babel internally I can create a Babel visitor to transform my source code.
The actually easiest and most resilient way for me to do that was not by using a dedicated codemod tool, but just Prettier itself with a custom wrapper (see prettiermod.js
). The wrapper allows you to specifiy a file pattern (aka. which files should be codemodded) and shows a preview of the change by default (pass write: true
to actually persist the changes).
In the usage.js
file you can see an example how my prettiermod.js
can be used. It shows how you can convert type Hello = { world: string; }
to interface Hello { world: string; }
(- I wanted to see, if this has any performance improvements on a large code base as interfaces are handled a bit differently in the TypeScript compiler).
Bonus tip: Use astexplorer.net with @babel/parser
and babelv7
as the "transform" setting for quick prototyping.