Ruby ffast

From wikinotes

A library and cli tool for searching/matching nodes within ruby's AST.

Documentation

github https://github.com/jonatas/fast

Install

gem install ffast

Usage

fast --ast some/file.rb            # show ast
fast '(const _ _)' some/file.rb    # find matching in some/file.rb
# find all method-calls for 'say_hello()' parallelized in a rails codebase
find app bin lib components -type f -name '*.rb' -not -path '*/test/*' -print0 \
    | xargs -0 -n 500 -P 4 fast '(send {nil _} :say_hello)' \
    | less -RSX +F


# search for files where 'MyClass.new' is instantiated
# that also refer to a symbol called 'fulfill_at'
# (ex: kwargs may be composed before initialization in hash, or args)
rg -g '*.rb' -l 'MyClass.new' \
    | grep -v '/test/' \
    | xargs -n 10 -P 4 fast -p --no-color '(sym :fulfill_at)'

sample patterns

# an object 'Foo' initialized with the keyword-argument 'two'
'(send (const {nil _} :Foo) :new (hash ... (pair (sym :two)) ))'
Foo.new(one: "abc", two: "def", three: "ghi")


# assignment of 'two' in a specific method call
fast --ast '(send {nil _} :sayhi {nil ...} (str "two") {nil ...})' foo.rb
sayhi("one", "two", "three")