Codemod – A library to assist with large-scale codebase refactoring
github.com
github.com
Suppose you have a function Foo::bar() and Bar::bar(). You decide to replace Bar::bar() by Bar::foo(). Good look with doing that via regex. Foo::bar() calls will most likely be affected as well. Matching directly on the AST solves this problem by explicitely matching all Bar::bar() calls.
What I really want: a language agnostic abstract syntax tree parser, matcher and transformer.
AST matchers and Clang refactoring tools: http://eli.thegreenplace.net/2014/07/29/ast-matchers-and-cla...
Refactoring C++ with Clang with cool examples: https://www.youtube.com/watch?v=yuIOGfcOH0k
Clang rename refactoring tool: https://clang.llvm.org/extra/clang-rename.html
I've started writing small scripts using the TypeScript parser as a library to make meaningful improvements in our codebase. Since JavaScript is valid TypeScript syntax the scripts work on both.
For example, I wrote a small script to change var to let or const when possible. It uses the AST to check scoping rules and to check if a variable is ever reassigned.
Writing scripts like this is way more effective than doing it by hand.
As a personal anecdote, I've written many ad hoc code mods while working on Luna2 (Asana's next framework) and had no idea one of our founders wrote this. He apparently assumed IDEs would just handle this by now.
Codemod is great, but there are better tools out there (pfff by facebook, IntelliJ plugins, etc). It's too bad the readme doesn't do a great job explaining that.