synvert-core-ruby
![]()
Synvert core provides a set of DSLs to rewrite ruby code. e.g.
Synvert::Rewriter.new 'factory_bot', 'convert_factory_girl_to_factory_bot' do
description " It converts FactoryGirl to FactoryBot\n\n ```ruby\n require 'factory_girl'\n require 'factory_girl_rails'\n ```\n\n =>\n\n ```ruby\n require 'factory_bot'\n require 'factory_bot_rails'\n ```\n\n ```ruby\n FactoryGirl.create(:user)\n FactoryGirl.build(:user)\n ```\n\n =>\n\n ```ruby\n FactoryBot.create(:user)\n FactoryBot.build(:user)\n ```\n EOS\n\n within_files Synvert::RAILS_TEST_FILES do\n find_node '.const[name=FactoryGirl]' do\n replace_with 'FactoryBot'\n end\n\n find_node \".send[receiver=nil][message=require][arguments.size=1][arguments.first='factory_girl']\" do\n replace :arguments, with: \"'factory_bot'\"\n end\n\n with_node type: 'send', receiver: nil, message: 'require', arguments: { size: 1, first: \"'factory_girl_rails'\" } do\n replace :arguments, with: \"'factory_bot_rails'\"\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
- description - set description of the rewriter
- if_ruby - check if ruby version is greater than or equal to the specified ruby version
- if_gem - compare version of specified gem
- within_files - find specified files
- within_file - alias to within_files
- add_file - add a new file
- remove_file - remove a file
- helper_method - define a helper method
- add_snippet - call another rewriter
- todo - set todo
- redo_until_no_change - run the snippet until no change
Scopes:
- find_node - recursively find matching ast nodes by node query language
- within_node - recursively find matching ast nodes
- with_node - alias to within_node
- goto_node - go to a child node
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