
Codemod – A library to assist with large-scale codebase refactoring - pestkranker
https://github.com/facebook/codemod
======
xaedes
I also like Clang Tools for C++ AST and programmatic refactoring. Much better
than regex replace. What exactly is the benefit of Codemod? Too me it seems
like a glorified regex replacer, nothing more.

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...](http://eli.thegreenplace.net/2014/07/29/ast-matchers-and-clang-
refactoring-tools)

Refactoring C++ with Clang with cool examples:
[https://www.youtube.com/watch?v=yuIOGfcOH0k](https://www.youtube.com/watch?v=yuIOGfcOH0k)

Clang rename refactoring tool: [https://clang.llvm.org/extra/clang-
rename.html](https://clang.llvm.org/extra/clang-rename.html)

------
Etheryte
This is nice, but I don't see the upside over using the tools provided by the
IDE or editor of your choice. Some of the additional flags are interesting,
true, but I can't really say I've ever felt a need for something like it.

~~~
mayank
It's meant for large codebases that can't be loaded into an IDE all at once,
or for cases where you need to modify other people's code and automatically
send them individual pull requests for approval.

------
stringham
I find the idea of
[https://github.com/facebook/jscodeshift](https://github.com/facebook/jscodeshift)
to be really useful.

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.

------
mygosh
Or just use a language that supports large scale refactoring, say Java, C#,
perhaps even C++ if you are careful. I generally don't like dynamic languages
for large projects exactly because of this. You can't safely and quickly do
large scale refactorings, a point that will come back and haunt you sooner or
later. No matter how good you are or how much care you take, the code will rot
significantly more than in Java/C# because you just can't quickly refactor
stuff across the entire codebase, so you won't.

------
jameslk
I'd also recommend checking out
[https://github.com/Yelp/undebt](https://github.com/Yelp/undebt) which is a
bit more robust it seems in the search aspect, since it's grammar-based
instead of regex-based.

~~~
paulie_a
I have explored undebt and it has some very useful examples. But generally
they seem to be low hanging fruit. I really would love to see more specific
implementations and use cases

------
pspeter3
I think this was open sourced earlier but I just discovered it.

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.

~~~
nathancahill
Facebook also maintains a JS codemod tool that React uses:
[https://github.com/facebook/jscodeshift](https://github.com/facebook/jscodeshift)

~~~
bm_i
jscodeshift (recast) is one of my favourite tools

------
amenghra
This code was open sourced in 2008, with a few improvements here and there
until 2010.

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.

------
allendoerfer
Nice project, but a less powerful version of vim's :argdo.

~~~
fphilipe
Or :cdo when using the quickfix window.

