A codemod helps you make sweeping changes to your code in a really straightforward way. React and Ember both use codemods a ton to help make the upgrade path easy.
It’s kinda like changing your code using a regular expression, except way better. Instead of manipulating strings, it’s based on manipulating the Abstract Syntax Tree. To see what an Abstract Syntax Tree (ast) is like, try putting some code into ASTExplorer and see what it spits out!
Some technologies involved:
codemod- language-agnostic tool to help run ast manipulations on a codebase / files
recast- a library that helps read/write AST (using
To write a codemod I use
jscodeshift directly, and it incorporates the rest. I’ve had to read documentation from them all, but I don’t have to import anything specifically from the others.
I made my first codemod this week! It helps ember users migrate away from importing
faker that’s bundled in
ember-cli-mirage to instead import it from
faker directly. I’m so glad we don’t have to edit these 200 files manually. This also helps out the whole Ember community, for when
ember-cli-mirage eventually moves away from having it bundled at all.
Here are some helpful references & resources I used to learn how codemods work, and create the one I did.
- Intro Articles
- Development Tips
- More Resources
Casey Watts! studied neurobiology at Yale University, and he is a co-author on several neurobiology papers. He has also worked in software development for 10 years, including at Heroku. Casey is an independent author based in Washington, DC.
Casey is the author of Debugging Your Brain. This book brings together two parts of Casey's background: psychology and software development.
Debugging Your Brain (DYB) is a clear applied psychology book and a concise self-help book. Whether or not you have a technical background, you will find the software development analogies approachable and insightful. You will likely reference and re-read DYB many times, each time discovering new insights.