api-rewrite.rb

May 18, 2018 · View on GitHub

require 'bundler/inline'

gemfile do source 'https://rubygems.org' gem 'ffast', git: 'https://github.com/jonatas/fast.git', require: 'fast' end

[ 'client.read_all_streams_forward(count: 1)', 'client.read_all_streams_forward(count: 1, start: :head)' ].each do |sample| ast = Fast.ast(sample) new_code = Fast.replace ast, '(send (send nil client) _ ... )', ->(node, captures) { method, args = captures new_method_call = 'read' new_method_call << '.backward' if method == :read_all_streams_backward key_values = args.children.map { |e| Fast.capture(e, '(pair (sym _) (_ _)))') } key_values.each do |key, value| new_method_call << '.' new_method_call << case key when :count 'limit' when :start 'from' else key.to_s end new_method_call << "(#{value.inspect})" end

                        replace_range = node.location.selector
                        replace_range = replace_range.join(node.location.end) if node.location.end

                        replace(replace_range, new_method_call)
                      }

puts "#{sample} => #{new_code}" end