Skip to content

Commit

Permalink
use replace and delete action to make snippets shorter
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed Apr 1, 2021
1 parent b5f7831 commit 2d95864
Show file tree
Hide file tree
Showing 17 changed files with 44 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -8,7 +8,7 @@ gem 'coveralls', require: false
gem 'fakefs', require: 'fakefs/safe'
gem 'rake'
gem 'rspec'
gem 'synvert-core', '>= 0.26.1'
gem 'synvert-core', '>= 0.27.4'

gem 'guard'
gem 'guard-rspec'
4 changes: 2 additions & 2 deletions lib/factory_bot/deprecate_static_value.rb
Expand Up @@ -67,9 +67,9 @@

if node.arguments.size == 1 && node.arguments.first.type == :hash
new_arguments = add_curly_brackets_if_necessary(node.arguments.first.to_source)
replace_with "{{message}} { #{new_arguments} }"
replace :arguments, with: "{ #{new_arguments} }"
else
replace_with '{{message}} { {{arguments}} }'
replace :arguments, with: '{ {{arguments}} }'
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/factory_bot/use_short_syntax.rb
Expand Up @@ -112,7 +112,7 @@ class MiniTest::Rails::ActiveSupport::TestCase
within_files '{test,spec,features}/**/*.rb' do
%w[create build attributes_for build_stubbed create_list build_list create_pair build_pair].each do |message|
with_node type: 'send', receiver: 'FactoryBot', message: message do
replace_with "#{message}({{arguments}})"
delete :receiver, :dot
end
end
end
Expand Down
22 changes: 9 additions & 13 deletions lib/factory_girl/fix_2_0_deprecations.rb
Expand Up @@ -94,7 +94,7 @@
argument = node.arguments.first.to_source
with_node type: 'block', caller: { type: 'send', receiver: argument } do
goto_node :caller do
replace_with "{{message}}#{add_arguments_with_parenthesis_if_necessary}"
delete :receiver, :dot
end
end
end
Expand All @@ -108,10 +108,8 @@
# end
%w[after_build after_create].each do |message|
within_node type: 'block', caller: { type: 'send', message: message } do
goto_node :caller do
new_message = message.sub('after_', '')
replace_with "after(:#{new_message})"
end
new_message = message.sub('after_', '')
replace :caller, with: "after(:#{new_message})"
end
end
end
Expand All @@ -135,7 +133,7 @@
} do
argument = node.arguments.first.to_source
with_node type: 'send', receiver: argument do
replace_with '{{message}} {{arguments}}'
delete :receiver, :dot
end
end
end
Expand All @@ -159,16 +157,14 @@
replace_with 'factory {{arguments}}'
end

goto_node :arguments do
replace_with ''
end
delete :arguments
end

# Factory.sequence :login do |n|
# "new_user_#{n}"
# end
# =>
# sequence :user do |n|
# sequence :login do |n|
# "new_user_#{n}"
# end
within_node type: 'block',
Expand All @@ -181,15 +177,15 @@
size: 1
} do
goto_node :caller do
replace_with 'sequence {{arguments}}'
delete :receiver, :dot
end
end
end

within_files '{test,spec}/**/*.rb' do
# Factory(:user) => create(:user)
with_node type: 'send', receiver: nil, message: 'Factory' do
replace_with 'create({{arguments}})'
replace :message, with: 'create'
end

# Factory.next(:email) => generate(:email)
Expand All @@ -207,7 +203,7 @@
# Factory.attributes_for(:user) => attributes_for(:user)
%w[create build attributes_for].each do |message|
with_node type: 'send', receiver: 'Factory', message: message do
replace_with "#{message}({{arguments}})"
delete :receiver, :dot
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/factory_girl/use_short_syntax.rb
Expand Up @@ -114,7 +114,7 @@ class MiniTest::Rails::ActiveSupport::TestCase
within_files '{test,spec,features}/**/*.rb' do
%w[create build attributes_for build_stubbed create_list build_list create_pair build_pair].each do |message|
with_node type: 'send', receiver: 'FactoryGirl', message: message do
replace_with "#{message}({{arguments}})"
delete :receiver, :dot
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rails/convert_active_record_dirty_5_0_to_5_1.rb
Expand Up @@ -112,10 +112,10 @@ def call_after_create
with_node type: 'send', message: before_name do
if before_name.is_a?(Regexp)
if node.message.to_s =~ before_name && attributes.include?($1)
replace_with add_receiver_if_necessary(after_name.sub('{{attribute}}', $1))
replace :message, with: after_name.sub('{{attribute}}', $1)
end
else
replace_with add_receiver_if_necessary(after_name)
replace :message, with: after_name
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rails/fix_model_3_2_deprecations.rb
Expand Up @@ -66,7 +66,7 @@
%w[set_table_name set_inheritance_column set_sequence_name set_primary_key set_locking_column].each do |message|
with_node type: 'send', message: message do
new_message = message.sub('set_', '')
replace_with "self.#{new_message} = {{arguments}}"
replace :message, with: "self.#{new_message} ="
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rails/redirect_with_flash.rb
Expand Up @@ -29,7 +29,7 @@
```
EOS

within_file 'app/controllers/**/*rb' do
within_file 'app/controllers/**/*.rb' do
within_node type: 'def' do
line = nil
msg = nil
Expand Down
7 changes: 1 addition & 6 deletions lib/rails/use_migrations_instance_methods.rb
Expand Up @@ -30,12 +30,7 @@ def down
# def self.down => def down
%w[up down].each do |name|
with_node type: 'defs', name: name do
new_code = <<~EOS
def #{name}
{{body}}
end
EOS
replace_with new_code.strip, autoindent: false
delete :self, :dot
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/method_stub.rb
Expand Up @@ -52,7 +52,7 @@
# obj.unstub!(:message) => obj.unstub(:message)
{ stub!: 'stub', unstub!: 'unstub' }.each do |old_message, new_message|
with_node type: 'send', message: old_message do
replace_with "{{receiver}}.#{new_message}({{arguments}})"
replace :message, with: new_message
end
end

Expand Down
16 changes: 8 additions & 8 deletions lib/rspec/new_config_options.rb
Expand Up @@ -86,7 +86,7 @@
'warnings' => 'warnings?'
}.each do |old_message, new_message|
with_node type: 'send', receiver: config_name, message: old_message do
replace_with "#{config_name}.#{new_message}"
replace :message, with: new_message
end
end

Expand All @@ -106,14 +106,14 @@
# c.pattern = '**/*_test.rb'
# end
{
'backtrace_clean_patterns=' => 'backtrace_exclusion_patterns = ',
'color_enabled=' => 'color = ',
'out=' => 'output_stream = ',
'output=' => 'output_stream = ',
'filename_pattern=' => 'pattern = '
'backtrace_clean_patterns=' => 'backtrace_exclusion_patterns =',
'color_enabled=' => 'color =',
'out=' => 'output_stream =',
'output=' => 'output_stream =',
'filename_pattern=' => 'pattern ='
}.each do |old_message, new_message|
with_node type: 'send', receiver: config_name, message: old_message do
replace_with "#{config_name}.#{new_message}{{arguments}}"
replace :message, with: new_message
end
end

Expand All @@ -125,7 +125,7 @@
# c.color_enabled?(output)
# end
with_node type: 'send', receiver: config_name, message: 'color?' do
replace_with "#{config_name}.color_enabled?({{arguments}})"
replace :message, with: 'color_enabled?'
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rspec/new_hook_scope.rb
Expand Up @@ -29,11 +29,11 @@
# before(:context) { do_something }
%w[before after around].each do |scope|
with_node type: 'send', message: scope, arguments: [:all] do
replace_with add_receiver_if_necessary("#{scope}(:context)")
replace :arguments, with: ':context'
end

with_node type: 'send', message: scope, arguments: [:each] do
replace_with add_receiver_if_necessary("#{scope}(:example)")
replace :arguments, with: ':example'
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rspec/stub_and_mock_to_double.rb
Expand Up @@ -23,11 +23,11 @@
# stub('something') => double('something')
# mock('something') => double('something')
with_node type: 'send', receiver: nil, message: 'stub' do
replace_with 'double({{arguments}})'
replace :message, with: 'double'
end

with_node type: 'send', receiver: nil, message: 'mock' do
replace_with 'double({{arguments}})'
replace :message, with: 'double'
end
end
end
2 changes: 1 addition & 1 deletion lib/ruby/deprecate_big_decimal_new.rb
Expand Up @@ -17,7 +17,7 @@

within_files '**/*.rb' do
with_node type: 'send', receiver: 'BigDecimal', message: 'new' do
replace_with 'BigDecimal({{arguments}})'
delete :dot, :message
end
end
end
2 changes: 1 addition & 1 deletion lib/ruby/kernel_open_to_uri_open.rb
Expand Up @@ -20,7 +20,7 @@
# =>
# URI.open('http://test.com')
with_node type: 'send', receiver: nil, message: 'open' do
replace_with 'URI.open({{arguments}})'
replace :message, with: 'URI.open'
end
end
end
7 changes: 6 additions & 1 deletion lib/ruby/map_and_flatten_to_flat_map.rb
Expand Up @@ -28,7 +28,12 @@
# # do something
# end
with_node type: 'send', receiver: { type: 'block', caller: { type: 'send', message: 'map' } }, message: 'flatten' do
replace_with "{{receiver.to_source.sub('.map', '.flat_map')}}"
delete :dot, :message
goto_node :receiver do
goto_node :caller do
replace :message, with: 'flat_map'
end
end
end
end
end
8 changes: 4 additions & 4 deletions spec/factory_bot/deprecate_static_value_spec.rb
Expand Up @@ -43,13 +43,13 @@
comments_count { 0 }
tag { Tag::MAGIC }
recent_statuses { [] }
status { [:draft, :published].sample }
status({ [:draft, :published].sample })
published_at { 1.day.from_now }
created_at { 1.day.ago }
created_at({ 1.day.ago })
updated_at { Time.current }
update_times { [Time.current] }
meta_tags { { foo: Time.current } }
other_tags { { foo: Time.current } }
meta_tags({ { foo: Time.current } })
other_tags({ { foo: Time.current } })
options { { color: :blue } }
trait :old do
published_at { 1.week.ago }
Expand Down

0 comments on commit 2d95864

Please sign in to comment.