From 2d95864f2d574cac772ef225fcf1c7f0a13df20a Mon Sep 17 00:00:00 2001 From: Richard Huang Date: Thu, 1 Apr 2021 21:32:01 +0800 Subject: [PATCH] use replace and delete action to make snippets shorter --- Gemfile | 2 +- lib/factory_bot/deprecate_static_value.rb | 4 ++-- lib/factory_bot/use_short_syntax.rb | 2 +- lib/factory_girl/fix_2_0_deprecations.rb | 22 ++++++++----------- lib/factory_girl/use_short_syntax.rb | 2 +- .../convert_active_record_dirty_5_0_to_5_1.rb | 4 ++-- lib/rails/fix_model_3_2_deprecations.rb | 2 +- lib/rails/redirect_with_flash.rb | 2 +- lib/rails/use_migrations_instance_methods.rb | 7 +----- lib/rspec/method_stub.rb | 2 +- lib/rspec/new_config_options.rb | 16 +++++++------- lib/rspec/new_hook_scope.rb | 4 ++-- lib/rspec/stub_and_mock_to_double.rb | 4 ++-- lib/ruby/deprecate_big_decimal_new.rb | 2 +- lib/ruby/kernel_open_to_uri_open.rb | 2 +- lib/ruby/map_and_flatten_to_flat_map.rb | 7 +++++- .../deprecate_static_value_spec.rb | 8 +++---- 17 files changed, 44 insertions(+), 48 deletions(-) diff --git a/Gemfile b/Gemfile index 0f412fa5..d06596b8 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/lib/factory_bot/deprecate_static_value.rb b/lib/factory_bot/deprecate_static_value.rb index 4931f8ac..ff707563 100644 --- a/lib/factory_bot/deprecate_static_value.rb +++ b/lib/factory_bot/deprecate_static_value.rb @@ -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 diff --git a/lib/factory_bot/use_short_syntax.rb b/lib/factory_bot/use_short_syntax.rb index 5c106cc4..fce23ce3 100644 --- a/lib/factory_bot/use_short_syntax.rb +++ b/lib/factory_bot/use_short_syntax.rb @@ -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 diff --git a/lib/factory_girl/fix_2_0_deprecations.rb b/lib/factory_girl/fix_2_0_deprecations.rb index cea97241..99329ecf 100644 --- a/lib/factory_girl/fix_2_0_deprecations.rb +++ b/lib/factory_girl/fix_2_0_deprecations.rb @@ -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 @@ -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 @@ -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 @@ -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', @@ -181,7 +177,7 @@ size: 1 } do goto_node :caller do - replace_with 'sequence {{arguments}}' + delete :receiver, :dot end end end @@ -189,7 +185,7 @@ 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) @@ -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 diff --git a/lib/factory_girl/use_short_syntax.rb b/lib/factory_girl/use_short_syntax.rb index f2668058..639df619 100644 --- a/lib/factory_girl/use_short_syntax.rb +++ b/lib/factory_girl/use_short_syntax.rb @@ -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 diff --git a/lib/rails/convert_active_record_dirty_5_0_to_5_1.rb b/lib/rails/convert_active_record_dirty_5_0_to_5_1.rb index 421a31e1..50a1d322 100644 --- a/lib/rails/convert_active_record_dirty_5_0_to_5_1.rb +++ b/lib/rails/convert_active_record_dirty_5_0_to_5_1.rb @@ -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 diff --git a/lib/rails/fix_model_3_2_deprecations.rb b/lib/rails/fix_model_3_2_deprecations.rb index 72932ffb..69ad0738 100644 --- a/lib/rails/fix_model_3_2_deprecations.rb +++ b/lib/rails/fix_model_3_2_deprecations.rb @@ -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 diff --git a/lib/rails/redirect_with_flash.rb b/lib/rails/redirect_with_flash.rb index 1905d57d..6fb476fa 100644 --- a/lib/rails/redirect_with_flash.rb +++ b/lib/rails/redirect_with_flash.rb @@ -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 diff --git a/lib/rails/use_migrations_instance_methods.rb b/lib/rails/use_migrations_instance_methods.rb index de22ad45..a387a788 100644 --- a/lib/rails/use_migrations_instance_methods.rb +++ b/lib/rails/use_migrations_instance_methods.rb @@ -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 diff --git a/lib/rspec/method_stub.rb b/lib/rspec/method_stub.rb index 04d4be1d..2fcdd000 100644 --- a/lib/rspec/method_stub.rb +++ b/lib/rspec/method_stub.rb @@ -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 diff --git a/lib/rspec/new_config_options.rb b/lib/rspec/new_config_options.rb index e65d752e..c011e9c9 100644 --- a/lib/rspec/new_config_options.rb +++ b/lib/rspec/new_config_options.rb @@ -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 @@ -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 @@ -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 diff --git a/lib/rspec/new_hook_scope.rb b/lib/rspec/new_hook_scope.rb index 635ff8d7..1e8fe677 100644 --- a/lib/rspec/new_hook_scope.rb +++ b/lib/rspec/new_hook_scope.rb @@ -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 diff --git a/lib/rspec/stub_and_mock_to_double.rb b/lib/rspec/stub_and_mock_to_double.rb index 9bcb49b7..776faf32 100644 --- a/lib/rspec/stub_and_mock_to_double.rb +++ b/lib/rspec/stub_and_mock_to_double.rb @@ -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 diff --git a/lib/ruby/deprecate_big_decimal_new.rb b/lib/ruby/deprecate_big_decimal_new.rb index 92495e23..e18c809a 100644 --- a/lib/ruby/deprecate_big_decimal_new.rb +++ b/lib/ruby/deprecate_big_decimal_new.rb @@ -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 diff --git a/lib/ruby/kernel_open_to_uri_open.rb b/lib/ruby/kernel_open_to_uri_open.rb index 58e117a7..d2d0044d 100644 --- a/lib/ruby/kernel_open_to_uri_open.rb +++ b/lib/ruby/kernel_open_to_uri_open.rb @@ -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 diff --git a/lib/ruby/map_and_flatten_to_flat_map.rb b/lib/ruby/map_and_flatten_to_flat_map.rb index 9ac232bf..415f22f0 100644 --- a/lib/ruby/map_and_flatten_to_flat_map.rb +++ b/lib/ruby/map_and_flatten_to_flat_map.rb @@ -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 diff --git a/spec/factory_bot/deprecate_static_value_spec.rb b/spec/factory_bot/deprecate_static_value_spec.rb index 23b2abd9..f1b41422 100644 --- a/spec/factory_bot/deprecate_static_value_spec.rb +++ b/spec/factory_bot/deprecate_static_value_spec.rb @@ -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 }