Home

Awesome

synvert-core-ruby

<img src="https://synvert.net/img/logo_96.png" alt="logo" width="32" height="32" />

AwesomeCode Status for synvert-hq/synvert-core-ruby Build Status Gem Version

Synvert core provides a set of DSLs to rewrite (find and replace) ruby code. e.g.

Synvert::Rewriter.new 'ruby', 'map_and_flatten_to_flat_map' do
  configure(parser: Synvert::PARSER_PARSER)

  description <<~EOS
    It converts `map` and `flatten` to `flat_map`

    ```ruby
    enum.map do
      # do something
    end.flatten
    ```

    =>

    ```ruby
    enum.flat_map do
      # do something
    end
    ```
  EOS

  within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do
    find_node '.send [receiver=.block [caller=.send[message=map]]] [message=flatten] [arguments.size=0]' do
      group do
        delete :message, :dot
        replace 'receiver.caller.message', with: 'flat_map'
      end
    end
  end
end

It also supports to add callbacks to visit ast nodes.

Synvert::Helper.new 'ruby/parse' do |options|
  configure(parser: Synvert::PRISM_PARSER)

  with_configurations(number_of_workers: 1) do
    class_names = []
    within_file Synvert::ALL_RUBY_FILES do
      add_callback :class_node, at: 'start' do |node|
        class_names << node.name.to_source
      end
    end
    # class_names is an array of class names
  end
end

Want to see more examples, check out synvert-snippets-ruby.

Want to use the CLI, check out synvert-ruby.

DSLs are as follows

Scopes:

Conditions:

Actions:

Callbacks:

Others:

Attributes: