synvert-core-ruby

logo

AwesomeCode Status for xinminlabs/synvert-core-ruby Main workflow

Synvert core provides a set of DSLs to rewrite ruby code. e.g.

Synvert::Rewriter.new 'ruby', 'map_and_flatten_to_flat_map' do
  description "    It converts `map` and `flatten` to `flat_map`\n\n    ```ruby\n    enum.map do\n      # do something\n    end.flatten\n    ```\n\n    =>\n\n    ```ruby\n    enum.flat_map do\n      # do something\n    end\n    ```\n  EOS\n\n  within_files Synvert::ALL_RUBY_FILES do\n    with_node type: 'send', receiver: { type: 'block', caller: { type: 'send', message: 'map' } }, message: 'flatten', arguments: { size: 0 } do\n      delete :message, :dot\n      replace 'receiver.caller.message', with: 'flat_map'\n    end\n  end\nend\n"

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:

  • if_exist_node - check if matching node exist in the child nodes
  • unless_exist_node - check if matching node doesn't exist in the child nodes
  • if_only_exist_node - check if current node has only one child node and the child node matches rules

Actions:

  • append - append the code to the bottom of current node body
  • prepend - prepend the code to the bottom of current node body
  • insert - insert code
  • insert_after - insert the code next to the current node
  • replace - replace the code of specified child nodes
  • delete - delete the code specified child nodes
  • wrap - wrap the current node with code
  • replace_with - replace the whole code of current node
  • warn - warn message
  • replace_erb_stmt_with_expr - replace erb stmt code to expr code