From 03da01e13956b2631fbd57e26e6465b98424be67 Mon Sep 17 00:00:00 2001 From: Richard Huang Date: Fri, 27 Dec 2024 19:14:20 +0200 Subject: [PATCH 1/2] update synvert-core to 2.2.1 --- Gemfile.lock | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 0faaeb6..8e613ce 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -13,9 +13,9 @@ GEM tzinfo (~> 2.0) ast (2.4.2) base64 (0.2.0) - bigdecimal (3.1.8) + bigdecimal (3.1.9) coderay (1.1.3) - concurrent-ruby (1.3.3) + concurrent-ruby (1.3.4) connection_pool (2.4.1) diff-lcs (1.5.0) drb (2.2.1) @@ -40,36 +40,36 @@ GEM guard (~> 2.1) guard-compat (~> 1.1) rspec (>= 2.99.0, < 4.0) - i18n (1.14.5) + i18n (1.14.6) concurrent-ruby (~> 1.0) listen (3.8.0) rb-fsevent (~> 0.10, >= 0.10.3) rb-inotify (~> 0.9, >= 0.9.10) lumberjack (1.2.8) method_source (1.0.0) - minitest (5.24.1) - mutex_m (0.2.0) + minitest (5.25.4) + mutex_m (0.3.0) nenv (0.3.0) node_mutation (1.24.4) - node_query (1.15.4) + node_query (1.16.0) node_visitor (1.1.0) notiffany (0.1.3) nenv (~> 0.1) shellany (~> 0.0) - parallel (1.25.1) - parser (3.3.4.0) + parallel (1.26.3) + parser (3.3.6.0) ast (~> 2.4.1) racc parser_node_ext (1.4.2) parser prettier_print (1.2.1) - prism (0.30.0) + prism (1.3.0) prism_ext (0.4.2) prism pry (0.14.2) coderay (~> 1.1) method_source (~> 1.0) - racc (1.8.0) + racc (1.8.1) rake (13.0.3) rb-fsevent (0.11.2) rb-inotify (0.10.1) @@ -92,8 +92,8 @@ GEM prettier_print (>= 1.2.0) syntax_tree_ext (0.9.2) syntax_tree - synvert-core (2.1.2) - activesupport + synvert-core (2.2.1) + activesupport (= 7.1.3.4) node_mutation (>= 1.24.4) node_query (>= 1.15.4) node_visitor (>= 1.1.0) From 361aed6761ec53a322233b6500ba4193691f50da Mon Sep 17 00:00:00 2001 From: Awesome Code Date: Fri, 27 Dec 2024 11:14:40 +0000 Subject: [PATCH 2/2] Auto corrected by following Format Ruby Code --- lib/helpers/parse_rails.rb | 8 +- lib/helpers/parse_ruby.rb | 44 +++++-- lib/helpers/set_rails_load_defaults.rb | 5 +- lib/rails/convert_after_commit.rb | 16 ++- lib/rails/convert_configs_3_0_to_3_1.rb | 8 +- lib/rails/convert_configs_3_2_to_4_0.rb | 26 +++- lib/rails/convert_configs_4_2_to_5_0.rb | 5 +- .../convert_dynamic_finders_for_rails_3.rb | 8 +- .../convert_dynamic_finders_for_rails_4.rb | 8 +- lib/rails/convert_head_response.rb | 21 +++- lib/rails/convert_mailers_2_3_to_3_0.rb | 23 +++- lib/rails/convert_model_lambda_scope.rb | 11 +- lib/rails/convert_models_2_3_to_3_0.rb | 74 +++++++++--- lib/rails/convert_models_3_2_to_4_0.rb | 5 +- lib/rails/convert_rails_env.rb | 18 ++- lib/rails/convert_rails_root.rb | 12 +- lib/rails/convert_routes_2_3_to_3_0.rb | 56 +++++++-- ...convert_test_request_methods_4_2_to_5_0.rb | 9 +- lib/rails/convert_views_3_2_to_4_0.rb | 8 +- lib/rails/fix_model_3_2_deprecations.rb | 11 +- lib/rails/migrate-ujs-to-turbo.rb | 10 +- lib/rails/new_enum_syntax.rb | 107 ++++++++--------- lib/rails/prefer_nor_conditions.rb | 8 +- lib/rails/redirect_with_flash.rb | 5 +- lib/rails/strong_parameters.rb | 10 +- ...t_request_methods_use_keyword_arguments.rb | 16 ++- .../update_active_storage_variant_argument.rb | 41 ++++++- ..._active_storage_image_processing_macros.rb | 10 +- .../always_add_db_index.rb | 5 +- lib/rspec/one_liner_expectation.rb | 27 +++-- lib/ruby/nested_class_definition.rb | 12 +- lib/ruby/prefer-endless-method.rb | 3 +- lib/ruby/use_keyword_arguments.rb | 16 ++- spec/helpers/parse_rails_spec.rb | 70 ++++++----- spec/helpers/parse_ruby_spec.rb | 112 +++++++++--------- spec/rails/convert_configs_3_2_to_4_0_spec.rb | 1 - .../always_add_db_index_spec.rb | 10 +- spec/ruby/kernel_open_to_uri_open_spec.rb | 4 +- 38 files changed, 592 insertions(+), 251 deletions(-) diff --git a/lib/helpers/parse_rails.rb b/lib/helpers/parse_rails.rb index 03ff7b5..e3b2b22 100644 --- a/lib/helpers/parse_rails.rb +++ b/lib/helpers/parse_rails.rb @@ -10,7 +10,7 @@ # Set number_of_workers to 1 to skip parallel. with_configurations(number_of_workers: 1) do within_file 'db/schema.rb' do - within_node node_type: 'call_node', name: 'create_table' do + within_node node_type: 'call_node', name: 'create_table' do table_name = node.arguments.arguments.first.to_value with_node node_type: 'call_node', receiver: 't', message: { not: 'index' } do column_name = node.arguments.arguments.first.to_value @@ -92,7 +92,7 @@ def find_or_create_table_definition(table_name) end def find_table_definition_by_table_name(table_name) - @table_definitions.find { |table| table.name == table_name } + @table_definitions.find { |table| table.name == table_name } end def to_h @@ -118,7 +118,7 @@ def add_index(name, columns) end def find_index_definition_by_column_names(column_names) - @indices.find { |index_definition| index_definition.columns == column_names } + @indices.find { |index_definition| index_definition.columns == column_names } end def get_column_names @@ -175,7 +175,7 @@ def add_model_association(model_name, association_name, association_type, **asso end def find_or_create_model_definition(model_name) - model_definition = @model_definitions.find { |model_definition| model_definition.name == model_name } + model_definition = @model_definitions.find { |model_definition| model_definition.name == model_name } return model_definition if model_definition model_definition = ModelDefinition.new(name: model_name) diff --git a/lib/helpers/parse_ruby.rb b/lib/helpers/parse_ruby.rb index 113dadc..aa226b9 100644 --- a/lib/helpers/parse_ruby.rb +++ b/lib/helpers/parse_ruby.rb @@ -44,13 +44,22 @@ end add_callback :call_node, at: 'start' do |node| - if node.receiver.nil? && node.name == :include && definitions.current_node_type == "class" && !node.arguments.nil? && %i[constant_read_node constant_path_node].include?(node.arguments.arguments.first.type) + if node.receiver.nil? && node.name == :include && definitions.current_node_type == "class" && !node.arguments.nil? && %i[ + constant_read_node constant_path_node + ].include?(node.arguments.arguments.first.type) + definitions.add_include_module(node.arguments.arguments.first.to_source) end - if node.receiver.nil? && node.name == :prepend && definitions.current_node_type == "class" && !node.arguments.nil? && %i[constant_read_node constant_path_node].include?(node.arguments.arguments.first.type) + if node.receiver.nil? && node.name == :prepend && definitions.current_node_type == "class" && !node.arguments.nil? && %i[ + constant_read_node constant_path_node + ].include?(node.arguments.arguments.first.type) + definitions.add_prepend_module(node.arguments.arguments.first.to_source) end - if node.receiver.nil? && node.name == :extend && definitions.current_node_type == "class" && !node.arguments.nil? && %i[constant_read_node constant_path_node].include?(node.arguments.arguments.first.type) + if node.receiver.nil? && node.name == :extend && definitions.current_node_type == "class" && !node.arguments.nil? && %i[ + constant_read_node constant_path_node + ].include?(node.arguments.arguments.first.type) + definitions.add_extend_module(node.arguments.arguments.first.to_source) end end @@ -182,7 +191,7 @@ def find_classes_by_ancestor(superclass) end def find_method_by_name(name) - methods.find { |method_definition| method_definition.name == name } + methods.find { |method_definition| method_definition.name == name } end def full_name @@ -259,7 +268,12 @@ def initialize end def to_h - { modules: @modules.map(&:to_h), classes: @classes.map(&:to_h), constants: @constants, methods: @methods.map(&:to_h) } + { + modules: @modules.map(&:to_h), + classes: @classes.map(&:to_h), + constants: @constants, + methods: @methods.map(&:to_h) + } end end @@ -286,14 +300,24 @@ def to_h methods: @methods.map(&:to_h), static_methods: @static_methods.map(&:to_h), constants: @constants, - singleton: @singleton &.to_h, + singleton: @singleton&.to_h, ancestors: @ancestors } end end class ClassDefinition < BaseDefinition - attr_reader :parent, :name, :superclass, :modules, :classes, :methods, :static_methods, :constants, :include_modules, :prepend_modules, :extend_modules + attr_reader :parent, + :name, + :superclass, + :modules, + :classes, + :methods, + :static_methods, + :constants, + :include_modules, + :prepend_modules, + :extend_modules attr_accessor :singleton, :ancestors def initialize(parent:, name:, superclass:) @@ -360,11 +384,13 @@ def initialize(parent:, name:) def call_method?(method_name) local_calls.include?(method_name) || - local_calls.any? { |local_call_method_name| parent.find_method_by_name(local_call_method_name)&.call_method?(method_name) } + local_calls.any? { |local_call_method_name| + parent.find_method_by_name(local_call_method_name)&.call_method?(method_name) + } end def call_any_method?(method_names) - method_names.any? { |method_name| call_method?(method_name) } + method_names.any? { |method_name| call_method?(method_name) } end def to_h diff --git a/lib/helpers/set_rails_load_defaults.rb b/lib/helpers/set_rails_load_defaults.rb index c5ac6ae..d28c7a7 100644 --- a/lib/helpers/set_rails_load_defaults.rb +++ b/lib/helpers/set_rails_load_defaults.rb @@ -8,7 +8,10 @@ within_file 'config/application.rb' do with_node node_type: 'class_node', constant_path: 'Application' do exists = false - with_node node_type: 'call_node', receiver: 'config', name: 'load_defaults', arguments: { arguments: { length: 1 } } do + with_node node_type: 'call_node', + receiver: 'config', + name: 'load_defaults', + arguments: { arguments: { length: 1 } } do exists = true replace_with "config.load_defaults #{rails_version}" end diff --git a/lib/rails/convert_after_commit.rb b/lib/rails/convert_after_commit.rb index 1661413..d6d5109 100644 --- a/lib/rails/convert_after_commit.rb +++ b/lib/rails/convert_after_commit.rb @@ -41,7 +41,13 @@ with_node node_type: 'call_node', receiver: nil, name: 'after_commit', - arguments: { node_type: 'arguments_node', arguments: { size: 2, '1': { node_type: 'keyword_hash_node', on_value: { in: %i[create update destroy] } } } } do + arguments: { + node_type: 'arguments_node', + arguments: { + size: 2, + '1': { node_type: 'keyword_hash_node', on_value: { in: %i[create update destroy] } } + } + } do group do replace :name, with: 'after_{{arguments.arguments.-1.on_value.to_value}}_commit' delete 'arguments.arguments.-1.on_element', and_comma: true @@ -60,7 +66,13 @@ with_node node_type: 'call_node', receiver: nil, message: 'after_commit', - arguments: { node_type: 'arguments_node', arguments: { size: 2, '1': { node_type: 'keyword_hash_node', on_value: { node_type: 'array_node' } } } } do + arguments: { + node_type: 'arguments_node', + arguments: { + size: 2, + '1': { node_type: 'keyword_hash_node', on_value: { node_type: 'array_node' } } + } + } do group do if node.arguments.arguments[1].on_value.elements.size == 1 replace :message, with: 'after_{{arguments.arguments.-1.on_value.elements.0.to_value}}_commit' diff --git a/lib/rails/convert_configs_3_0_to_3_1.rb b/lib/rails/convert_configs_3_0_to_3_1.rb index 72f0735..9ce2481 100644 --- a/lib/rails/convert_configs_3_0_to_3_1.rb +++ b/lib/rails/convert_configs_3_0_to_3_1.rb @@ -173,16 +173,12 @@ def down within_file 'config/environments/test.rb' do # prepend config.static_cache_control = "public, max-age=3600" - unless_exist_node node_type: 'call_node', - receiver: 'config', - name: 'static_cache_control=' do + unless_exist_node node_type: 'call_node', receiver: 'config', name: 'static_cache_control=' do prepend 'config.static_cache_control = "public, max-age=3600"' end # prepend config.serve_static_assets = true - unless_exist_node node_type: 'call_node', - receiver: 'config', - name: 'serve_static_assets=' do + unless_exist_node node_type: 'call_node', receiver: 'config', name: 'serve_static_assets=' do prepend 'config.serve_static_assets = true' end end diff --git a/lib/rails/convert_configs_3_2_to_4_0.rb b/lib/rails/convert_configs_3_2_to_4_0.rb index d515297..4d69ef5 100644 --- a/lib/rails/convert_configs_3_2_to_4_0.rb +++ b/lib/rails/convert_configs_3_2_to_4_0.rb @@ -102,7 +102,11 @@ end # remove config.middleware.xxx(..., ActionDispatch::BestStandardsSupport) - with_node node_type: 'call_node', arguments: { node_type: 'arguments_node', arguments: { includes: 'ActionDispatch::BestStandardsSupport' } } do + with_node node_type: 'call_node', + arguments: { + node_type: 'arguments_node', + arguments: { includes: 'ActionDispatch::BestStandardsSupport' } + } do remove end @@ -153,9 +157,23 @@ receiver: 'ActiveSupport', name: 'on_load', arguments: { node_type: 'arguments_node', arguments: { size: 1, first: :active_record } }, - block: { node_type: 'block_node', body: { node_type: 'statements_node', body: { size: 1, first: { - node_type: 'call_node', receiver: 'self', name: 'include_root_in_json=', arguments: { node_type: 'arguments_node', arguments: { size: 1, first: false } } - } } } } do + block: { + node_type: 'block_node', + body: { + node_type: 'statements_node', + body: { + size: 1, + first: { + node_type: 'call_node', + receiver: 'self', + name: 'include_root_in_json=', + arguments: { + node_type: 'arguments_node', arguments: { size: 1, first: false } + } + } + } + } + } do remove end end diff --git a/lib/rails/convert_configs_4_2_to_5_0.rb b/lib/rails/convert_configs_4_2_to_5_0.rb index 4baee65..0ad965b 100644 --- a/lib/rails/convert_configs_4_2_to_5_0.rb +++ b/lib/rails/convert_configs_4_2_to_5_0.rb @@ -55,7 +55,10 @@ name: 'middleware' }, name: 'use', - arguments: { node_type: 'arguments_node', arguments: { size: { gt: 0 }, first: { node_type: 'string_node' } } } do + arguments: { + node_type: 'arguments_node', + arguments: { size: { gt: 0 }, first: { node_type: 'string_node' } } + } do replace 'arguments.arguments.first', with: "{{arguments.arguments.first.to_value}}" end end diff --git a/lib/rails/convert_dynamic_finders_for_rails_3.rb b/lib/rails/convert_dynamic_finders_for_rails_3.rb index 4bd5c1b..1322ea2 100644 --- a/lib/rails/convert_dynamic_finders_for_rails_3.rb +++ b/lib/rails/convert_dynamic_finders_for_rails_3.rb @@ -28,14 +28,18 @@ EOS definitions = call_helper 'rails/parse' - table_columns = definitions.table_definitions.flat_map { |table_definition| table_definition.get_column_names }.uniq + ['id'] + table_columns = definitions.table_definitions.flat_map { |table_definition| + table_definition.get_column_names + } +.uniq + ['id'] helper_method :dynamic_finder_to_hash do |prefix| fields = node.name.to_s[prefix.length..-1].split('_and_') return nil if (fields - table_columns).present? if fields.length == node.arguments.arguments.length && :hash_node != node.arguments.arguments.first.type - fields.length.times.map { |i| fields[i] + ': ' + node.arguments.arguments[i].to_source }.join(', ') + fields.length.times.map { |i| fields[i] + ': ' + node.arguments.arguments[i].to_source } + .join(', ') else '{{arguments}}' end diff --git a/lib/rails/convert_dynamic_finders_for_rails_4.rb b/lib/rails/convert_dynamic_finders_for_rails_4.rb index 326212f..90b93e7 100644 --- a/lib/rails/convert_dynamic_finders_for_rails_4.rb +++ b/lib/rails/convert_dynamic_finders_for_rails_4.rb @@ -20,14 +20,18 @@ EOS definitions = call_helper 'rails/parse' - table_columns = definitions.table_definitions.flat_map { |table_definition| table_definition.get_column_names }.uniq + ['id'] + table_columns = definitions.table_definitions.flat_map { |table_definition| + table_definition.get_column_names + } +.uniq + ['id'] helper_method :dynamic_finder_to_hash do |prefix| fields = node.name.to_s[prefix.length..-1].split('_and_') return nil if (fields - table_columns).present? if fields.length == node.arguments.arguments.length && :hash_node != node.arguments.arguments.first.type - fields.length.times.map { |i| fields[i] + ': ' + node.arguments.arguments[i].to_source }.join(', ') + fields.length.times.map { |i| fields[i] + ': ' + node.arguments.arguments[i].to_source } + .join(', ') else '{{arguments}}' end diff --git a/lib/rails/convert_head_response.rb b/lib/rails/convert_head_response.rb index e46636a..1d07bd7 100644 --- a/lib/rails/convert_head_response.rb +++ b/lib/rails/convert_head_response.rb @@ -36,7 +36,10 @@ with_node node_type: 'call_node', receiver: nil, name: 'render', - arguments: { node_type: 'arguments_node', arguments: { size: 1, first: { node_type: 'keyword_hash_node', nothing_value: true } } } do + arguments: { + node_type: 'arguments_node', + arguments: { size: 1, first: { node_type: 'keyword_hash_node', nothing_value: true } } + } do group do replace :message, with: 'head' if node.arguments.arguments.first.status_value.nil? @@ -53,7 +56,13 @@ with_node node_type: 'call_node', receiver: nil, name: 'head', - arguments: { node_type: 'arguments_node', arguments: { size: 1, first: { node_type: 'keyword_hash_node', location_value: { not: nil } } } } do + arguments: { + node_type: 'arguments_node', + arguments: { + size: 1, + first: { node_type: 'keyword_hash_node', location_value: { not: nil } } + } + } do replace 'arguments.arguments.0', with: ':ok, {{arguments.arguments.0.to_source}}' end @@ -63,7 +72,13 @@ with_node node_type: 'call_node', receiver: nil, name: 'head', - arguments: { node_type: 'arguments_node', arguments: { size: 1, first: { node_type: 'keyword_hash_node', status_value: { not: nil } } } } do + arguments: { + node_type: 'arguments_node', + arguments: { + size: 1, + first: { node_type: 'keyword_hash_node', status_value: { not: nil } } + } + } do replace 'arguments.arguments.0', with: '{{arguments.arguments.0.status_source}}' end end diff --git a/lib/rails/convert_mailers_2_3_to_3_0.rb b/lib/rails/convert_mailers_2_3_to_3_0.rb index 22f5efc..0f0fda4 100644 --- a/lib/rails/convert_mailers_2_3_to_3_0.rb +++ b/lib/rails/convert_mailers_2_3_to_3_0.rb @@ -79,7 +79,10 @@ def signup_notification(recipient) class_name = node.name within_node node_type: 'def_node' do args = {} - with_node node_type: 'call_node', receiver: nil, name: 'recipients', arguments: { node_type: 'arguments_node', arguments: { size: 1 } } do + with_node node_type: 'call_node', + receiver: nil, + name: 'recipients', + arguments: { node_type: 'arguments_node', arguments: { size: 1 } } do args[:to] = node.arguments.arguments.first.to_source remove end @@ -97,13 +100,20 @@ def signup_notification(recipient) with_node node_type: 'call_node', receiver: nil, name: 'body', - arguments: { node_type: 'arguments_node', arguments: { size: 1, first: { node_type: 'keyword_hash_node' } } } do - replace_with node.arguments.arguments.first.elements.map { |element| "@#{element.key.to_value} = #{element.value.to_source}" }.join("\n") + arguments: { + node_type: 'arguments_node', + arguments: { size: 1, first: { node_type: 'keyword_hash_node' } } + } do + replace_with node.arguments.arguments.first.elements.map { |element| + "@#{element.key.to_value} = #{element.value.to_source}" + } +.join("\n") end if args.size > 0 mailer_methods[class_name] ||= [] mailer_methods[class_name] << node.name - args_str = args.map { |key, value| ":#{key} => #{value}" }.join(', ') + args_str = args.map { |key, value| ":#{key} => #{value}" } + .join(', ') append "mail(#{args_str})" end end @@ -134,7 +144,10 @@ def signup_notification(recipient) # Notifier.deliver(message) # => # message.deliver - with_node node_type: 'call_node', receiver: { not: nil }, name: 'deliver', arguments: { node_type: 'arguments_node', arguments: { size: 1 } } do + with_node node_type: 'call_node', + receiver: { not: nil }, + name: 'deliver', + arguments: { node_type: 'arguments_node', arguments: { size: 1 } } do if mailer_methods[node.receiver.name] replace_with '{{arguments}}.{{message}}' end diff --git a/lib/rails/convert_model_lambda_scope.rb b/lib/rails/convert_model_lambda_scope.rb index ec1fc52..fc092c0 100644 --- a/lib/rails/convert_model_lambda_scope.rb +++ b/lib/rails/convert_model_lambda_scope.rb @@ -66,7 +66,8 @@ class Post < ActiveRecord::Base } } do if node.arguments.arguments.last.block.parameters - replace 'arguments.arguments.last', with: '->({{arguments.arguments.last.block.parameters.parameters}}) { {{arguments.arguments.last.block.body}} }' + replace 'arguments.arguments.last', + with: '->({{arguments.arguments.last.block.parameters.parameters}}) { {{arguments.arguments.last.block.body}} }' else replace 'arguments.arguments.last', with: '-> { {{arguments.arguments.last.block.body}} }' end @@ -87,7 +88,8 @@ class Post < ActiveRecord::Base } } do if node.arguments.arguments.last.block.parameters - replace 'arguments.arguments.last', with: '->({{arguments.arguments.last.block.parameters.parameters}}) { {{arguments.arguments.last.block.body}} }' + replace 'arguments.arguments.last', + with: '->({{arguments.arguments.last.block.parameters.parameters}}) { {{arguments.arguments.last.block.body}} }' else replace 'arguments.arguments.last', with: '-> { {{arguments.arguments.last.block.body}} }' end @@ -102,10 +104,7 @@ class Post < ActiveRecord::Base end # default_scope { order("updated_at DESC") } => default_scope -> { order("updated_at DESC") } - with_node node_type: 'call_node', - receiver: nil, - name: 'default_scope', - block: { node_type: 'block_node' } do + with_node node_type: 'call_node', receiver: nil, name: 'default_scope', block: { node_type: 'block_node' } do replace_with 'default_scope -> { {{block.body.body}} }' end end diff --git a/lib/rails/convert_models_2_3_to_3_0.rb b/lib/rails/convert_models_2_3_to_3_0.rb index 0c9b6d8..f5e3c74 100644 --- a/lib/rails/convert_models_2_3_to_3_0.rb +++ b/lib/rails/convert_models_2_3_to_3_0.rb @@ -221,7 +221,10 @@ arguments: { node_type: 'arguments_node', arguments: { - last: { node_type: 'call_node', block: { node_type: 'block_node', body: { body: { first: { node_type: 'hash_node' } } } } } + last: { + node_type: 'call_node', + block: { node_type: 'block_node', body: { body: { first: { node_type: 'hash_node' } } } } + } } } do goto_node 'arguments.arguments.-1.block.body.body.0' do @@ -241,7 +244,12 @@ # scoped(:conditions => {:active => true}) # => # where(:active => true) - within_node node_type: 'call_node', name: 'scoped', arguments: { node_type: 'arguments_node', arguments: { size: 1, first: { node_type: 'keyword_hash_node' } } } do + within_node node_type: 'call_node', + name: 'scoped', + arguments: { + node_type: 'arguments_node', + arguments: { size: 1, first: { node_type: 'keyword_hash_node' } } + } do if keys & node.arguments.arguments.first.keys.map(&:to_value) replace :message, :closing, with: generate_new_queries(node.arguments.arguments.first) end @@ -252,7 +260,10 @@ # Post.joins(:comments).all within_node node_type: 'call_node', name: 'all', - arguments: { node_type: 'arguments_node', arguments: { size: 1, first: { node_type: 'keyword_hash_node' } } } do + arguments: { + node_type: 'arguments_node', + arguments: { size: 1, first: { node_type: 'keyword_hash_node' } } + } do if keys & node.arguments.arguments.first.keys.map(&:to_value) replace :message, :closing, with: "#{generate_new_queries(node.arguments.arguments.first)}" end @@ -263,7 +274,10 @@ # Post.where(:title => "test").first within_node node_type: 'call_node', name: { in: ['first', 'last'] }, - arguments: { node_type: 'arguments_node', arguments: { size: 1, first: { node_type: 'keyword_hash_node' } } } do + arguments: { + node_type: 'arguments_node', + arguments: { size: 1, first: { node_type: 'keyword_hash_node' } } + } do if keys & node.arguments.arguments.first.keys.map(&:to_value) replace :message, :closing, with: "#{generate_new_queries(node.arguments.arguments.first)}.{{name}}" end @@ -282,7 +296,10 @@ # Client.where(:active => true).sum("orders_count") within_node node_type: 'call_node', name: { in: %w[count average min max sum] }, - arguments: { node_type: 'arguments_node', arguments: { size: 2, last: { node_type: 'keyword_hash_node' } } } do + arguments: { + node_type: 'arguments_node', + arguments: { size: 2, last: { node_type: 'keyword_hash_node' } } + } do if keys & node.arguments.arguments.last.keys.map(&:to_value) group do insert ".#{generate_new_queries(node.arguments.arguments.last)}", to: 'receiver', at: 'end' @@ -296,7 +313,10 @@ # Post.limit(2) with_node node_type: 'call_node', name: 'find', - arguments: { node_type: 'arguments_node', arguments: { size: 2, first: :all, last: { node_type: 'keyword_hash_node' } } } do + arguments: { + node_type: 'arguments_node', + arguments: { size: 2, first: :all, last: { node_type: 'keyword_hash_node' } } + } do if keys & node.arguments.arguments.last.keys.map(&:to_value) replace :message, :closing, with: generate_new_queries(node.arguments.arguments.last) end @@ -305,7 +325,9 @@ # Post.find(:all) # => # Post.all - with_node node_type: 'call_node', name: 'find', arguments: { node_type: 'arguments_node', arguments: { size: 1, first: :all } } do + with_node node_type: 'call_node', + name: 'find', + arguments: { node_type: 'arguments_node', arguments: { size: 1, first: :all } } do group do replace :message, with: 'all' delete :opening, :closing @@ -317,9 +339,18 @@ # Post.where(:title => "title").last within_node node_type: 'call_node', name: 'find', - arguments: { node_type: 'arguments_node', arguments: { size: 2, first: { in: [:first, :last] }, last: { node_type: 'keyword_hash_node' } } } do + arguments: { + node_type: 'arguments_node', + arguments: { + size: 2, + first: { in: [:first, :last] }, + last: { node_type: 'keyword_hash_node' } + } + } do if keys & node.arguments.arguments.last.keys.map(&:to_value) - replace :message, :closing, with: "#{generate_new_queries(node.arguments.arguments.last)}.{{arguments.arguments.first.to_value}}" + replace :message, + :closing, + with: "#{generate_new_queries(node.arguments.arguments.last)}.{{arguments.arguments.first.to_value}}" end end @@ -339,7 +370,9 @@ # Post.where(:title => "test").update_all(:title => "title") # Post.where("title = \'test\'").update_all("title = \'title\'") # Post.where("title = ?", title).update_all("title = \'title\'") - within_node node_type: 'call_node', name: :update_all, arguments: { node_type: 'arguments_node', arguments: { size: 2 } } do + within_node node_type: 'call_node', + name: :update_all, + arguments: { node_type: 'arguments_node', arguments: { size: 2 } } do group do insert '.where({{arguments.arguments.first}})', to: 'receiver', at: 'end' delete 'arguments.arguments.first', and_comma: true @@ -349,7 +382,9 @@ # Post.update_all({:title => "title"}, {:title => "test"}, {:limit => 2}) # => # Post.where(:title => "test").limit(2).update_all(:title => "title") - within_node node_type: 'call_node', name: :update_all, arguments: { node_type: 'arguments_node', arguments: { size: 3 } } do + within_node node_type: 'call_node', + name: :update_all, + arguments: { node_type: 'arguments_node', arguments: { size: 3 } } do group do insert '.where({{arguments.arguments.first}})', to: 'receiver', at: 'end' insert ".#{generate_new_queries(node.arguments.arguments.last)}", to: 'receiver', at: 'end' @@ -369,7 +404,9 @@ # => # Post.where("title = \'test\'").destroy_all # Post.where("title = ?", title).destroy_all - within_node node_type: 'call_node', name: { in: ['delete_all', 'destroy_all'] }, arguments: { node_type: 'arguments_node', arguments: { size: 1 } } do + within_node node_type: 'call_node', + name: { in: ['delete_all', 'destroy_all'] }, + arguments: { node_type: 'arguments_node', arguments: { size: 1 } } do group do replace :message, with: 'where' insert ".{{message}}" @@ -389,7 +426,10 @@ # end within_node node_type: 'call_node', name: { in: ['find_each', 'find_in_batches'] }, - arguments: { node_type: 'arguments_node', arguments: { size: 1, first: { node_type: 'keyword_hash_node' } } } do + arguments: { + node_type: 'arguments_node', + arguments: { size: 1, first: { node_type: 'keyword_hash_node' } } + } do argument_node = node.arguments.arguments.first if keys & argument_node.keys.map(&:to_value) batch_options = generate_batch_options(argument_node) @@ -410,7 +450,13 @@ # with_exclusive_scope(limit(1)) { Post.last } within_node node_type: 'call_node', name: { in: ['with_scope', 'with_exclusive_scope'] }, - arguments: { node_type: 'arguments_node', arguments: { size: 1, first: { node_type: 'keyword_hash_node', find_value: { not: nil } } } } do + arguments: { + node_type: 'arguments_node', + arguments: { + size: 1, + first: { node_type: 'keyword_hash_node', find_value: { not: nil } } + } + } do replace :arguments, with: generate_new_queries(node.arguments.arguments.first.find_value.to_value) end end diff --git a/lib/rails/convert_models_3_2_to_4_0.rb b/lib/rails/convert_models_3_2_to_4_0.rb index beb0d12..a0ec966 100644 --- a/lib/rails/convert_models_3_2_to_4_0.rb +++ b/lib/rails/convert_models_3_2_to_4_0.rb @@ -33,7 +33,10 @@ within_node node_type: 'call_node', receiver: nil, name: { in: ['has_one', 'has_many'] }, - arguments: { node_type: 'arguments_node', arguments: { last: { node_type: 'keyword_hash_node', dependent_value: :restrict } } } do + arguments: { + node_type: 'arguments_node', + arguments: { last: { node_type: 'keyword_hash_node', dependent_value: :restrict } } + } do replace 'arguments.arguments.-1.dependent_value', with: ':restrict_with_exception' end end diff --git a/lib/rails/convert_rails_env.rb b/lib/rails/convert_rails_env.rb index a4dd4ed..1764a57 100644 --- a/lib/rails/convert_rails_env.rb +++ b/lib/rails/convert_rails_env.rb @@ -42,28 +42,38 @@ # Rails.env == 'test' # => # Rails.env.test? - with_node node_type: 'call_node', receiver: 'Rails.env', name: '==', arguments: { node_type: 'arguments_node', arguments: { size: 1 } } do + with_node node_type: 'call_node', + receiver: 'Rails.env', + name: '==', + arguments: { node_type: 'arguments_node', arguments: { size: 1 } } do replace_with "Rails.env.{{arguments.arguments.0.to_value}}?" end # 'development' == Rails.env # => # Rails.env.development? - with_node node_type: 'call_node', name: '==', arguments: { node_type: 'arguments_node', arguments: { size: 1, first: 'Rails.env' } } do + with_node node_type: 'call_node', + name: '==', + arguments: { node_type: 'arguments_node', arguments: { size: 1, first: 'Rails.env' } } do replace_with "Rails.env.{{receiver.to_value}}?" end # Rails.env != 'test' # => # !Rails.env.test? - with_node node_type: 'call_node', receiver: 'Rails.env', name: '!=', arguments: { node_type: 'arguments_node', arguments: { size: 1 } } do + with_node node_type: 'call_node', + receiver: 'Rails.env', + name: '!=', + arguments: { node_type: 'arguments_node', arguments: { size: 1 } } do replace_with "!Rails.env.{{arguments.arguments.0.to_value}}?" end # 'development' != Rails.env # => # !Rails.env.development? - with_node node_type: 'call_node', name: '!=', arguments: { node_type: 'arguments_node', arguments: { size: 1, first: 'Rails.env' } } do + with_node node_type: 'call_node', + name: '!=', + arguments: { node_type: 'arguments_node', arguments: { size: 1, first: 'Rails.env' } } do replace_with "!Rails.env.{{receiver.to_value}}?" end end diff --git a/lib/rails/convert_rails_root.rb b/lib/rails/convert_rails_root.rb index 0b00c99..eb37bb4 100644 --- a/lib/rails/convert_rails_root.rb +++ b/lib/rails/convert_rails_root.rb @@ -38,7 +38,10 @@ # File.join(Rails.root, 'config/database.yml') # => # Rails.root.join('config/database.yml') - with_node node_type: 'call_node', receiver: 'File', name: 'join', arguments: { node_type: 'arguments_node', arguments: { first: 'Rails.root' } } do + with_node node_type: 'call_node', + receiver: 'File', + name: 'join', + arguments: { node_type: 'arguments_node', arguments: { first: 'Rails.root' } } do other_arguments = node.arguments.arguments[1..-1].map(&:to_source).join(', ') replace_with "Rails.root.join(#{other_arguments})" end @@ -56,7 +59,12 @@ # => # Rails.root.join('config/database.yml') with_node node_type: 'interpolated_string_node', - parts: { first: { node_type: 'embedded_statements_node', statements: { body: { first: 'Rails.root' } } } } do + parts: { + first: { + node_type: 'embedded_statements_node', + statements: { body: { first: 'Rails.root' } } + } + } do source = node.to_source source[1..14] = '' replace_with "Rails.root.join(#{source})" diff --git a/lib/rails/convert_routes_2_3_to_3_0.rb b/lib/rails/convert_routes_2_3_to_3_0.rb index d67fa7b..8e3778a 100644 --- a/lib/rails/convert_routes_2_3_to_3_0.rb +++ b/lib/rails/convert_routes_2_3_to_3_0.rb @@ -146,11 +146,15 @@ end helper_method :generate_new_child_routes do |block_node| - block_node.body.body.map { |child_node| " #{child_node.to_source.sub("#{block_node.parameters.parameters.requireds.first.to_source}.", '')}\n" }.join('') + block_node.body.body.map { |child_node| + " #{child_node.to_source.sub("#{block_node.parameters.parameters.requireds.first.to_source}.", '')}\n" + } +.join('') end helper_method :reject_keys_from_hash do |hash_node, *keys| - hash_node.elements.reject { |element_node| keys.include?(element_node.key.to_value) }.map(&:to_source).join(', ') + hash_node.elements.reject { |element_node| keys.include?(element_node.key.to_value) } + .map(&:to_source).join(', ') end within_files Synvert::RAILS_ROUTE_FILES do @@ -161,7 +165,11 @@ # namespace :admin do # resources :users # end - with_node node_type: 'call_node', receiver: { not: nil }, name: 'namespace', block: { node_type: 'block_node' }, arguments: { not: nil } do + with_node node_type: 'call_node', + receiver: { not: nil }, + name: 'namespace', + block: { node_type: 'block_node' }, + arguments: { not: nil } do new_routes = [] new_routes << "namespace {{arguments}} do\n" new_routes << generate_new_child_routes(node.block) @@ -217,7 +225,10 @@ within_node node_type: 'call_node', receiver: { not: nil }, name: { in: ['resource', 'resources'] }, - arguments: { node_type: 'arguments_node', arguments: { size: 2, last: { node_type: 'keyword_hash_node' } } } do + arguments: { + node_type: 'arguments_node', + arguments: { size: 2, last: { node_type: 'keyword_hash_node' } } + } do hash_argument = node.arguments.arguments.last new_routes = [] if !hash_argument.collection_value.nil? || !hash_argument.member_value.nil? @@ -244,7 +255,16 @@ within_node node_type: 'call_node', receiver: 'map', name: 'connect', - arguments: { node_type: 'arguments_node', arguments: { last: { node_type: 'keyword_hash_node', action_value: { not: nil }, controller_value: { not: nil } } } } do + arguments: { + node_type: 'arguments_node', + arguments: { + last: { + node_type: 'keyword_hash_node', + action_value: { not: nil }, + controller_value: { not: nil } + } + } + } do hash_node = node.arguments.arguments.last controller_action_name = extract_controller_action_name(hash_node) method = hash_node.method_value ? hash_node.method_value.to_value : 'match' @@ -283,7 +303,14 @@ with_node node_type: 'call_node', receiver: 'map', name: 'connect', - arguments: { node_type: 'arguments_node', arguments: { size: 2, first: %r|(.*?)/:action/:id|, last: { node_type: 'keyword_hash_node', controller_value: { not: nil } } } } do + arguments: { + node_type: 'arguments_node', + arguments: { + size: 2, + first: %r|(.*?)/:action/:id|, + last: { node_type: 'keyword_hash_node', controller_value: { not: nil } } + } + } do controller_name = node.arguments.arguments.last.controller_value.to_value replace_with "match #{wrap_with_quotes(controller_name + "(/:action(/:id))(.:format)")}, {{arguments.arguments.last}}" end @@ -293,7 +320,14 @@ with_node node_type: 'call_node', receiver: 'map', name: 'connect', - arguments: { node_type: 'arguments_node', arguments: { size: 2, first: %r|(.*?)/:action['"]$|, last: { node_type: 'keyword_hash_node', controller_value: { not: nil } } } } do + arguments: { + node_type: 'arguments_node', + arguments: { + size: 2, + first: %r|(.*?)/:action['"]$|, + last: { node_type: 'keyword_hash_node', controller_value: { not: nil } } + } + } do controller_name = node.arguments.arguments.last.controller_value.to_value replace_with "match #{wrap_with_quotes(controller_name + "(/:action)(.:format)")}, {{arguments.arguments.last}}" end @@ -304,7 +338,13 @@ within_node node_type: 'call_node', receiver: 'map', name: { not_in: ['root', 'connect', 'resource', 'resources'] }, - arguments: { node_type: 'arguments_node', arguments: { size: 2, last: { node_type: 'keyword_hash_node', controller_value: { not: nil } } } } do + arguments: { + node_type: 'arguments_node', + arguments: { + size: 2, + last: { node_type: 'keyword_hash_node', controller_value: { not: nil } } + } + } do url = node.arguments.arguments.first.to_value hash_node = node.arguments.arguments.last if hash_node.action_value || url !~ /:action/ diff --git a/lib/rails/convert_test_request_methods_4_2_to_5_0.rb b/lib/rails/convert_test_request_methods_4_2_to_5_0.rb index 6de5bbd..cd2d423 100644 --- a/lib/rails/convert_test_request_methods_4_2_to_5_0.rb +++ b/lib/rails/convert_test_request_methods_4_2_to_5_0.rb @@ -40,7 +40,10 @@ if argument_node.type == :keyword_hash_node || argument_node.type == :hash_node new_value = - argument_node.elements.reject { |element_node| element_node.type == :assoc_node && %i[format xhr as].include?(element_node.key.to_value) }.map(&:to_source).join(', ') + argument_node.elements.reject { |element_node| + element_node.type == :assoc_node && %i[format xhr as].include?(element_node.key.to_value) + } +.map(&:to_source).join(', ') "#{key}: #{add_curly_brackets_if_necessary(new_value)}" if new_value.length > 0 else "#{key}: #{argument_node.to_source}" @@ -79,7 +82,9 @@ replace :arguments, with: '{{arguments.arguments.1}}, xhr: true' end - with_node node_type: 'call_node', name: 'xhr', arguments: { node_type: 'arguments_node', arguments: { size: { gt: 2 } } } do + with_node node_type: 'call_node', + name: 'xhr', + arguments: { node_type: 'arguments_node', arguments: { size: { gt: 2 } } } do format_value = node.arguments.arguments[2].type == :hash && node.arguments.arguments[2].format_value options = [] options << make_up_hash_element('params', node.arguments.arguments[2]) diff --git a/lib/rails/convert_views_3_2_to_4_0.rb b/lib/rails/convert_views_3_2_to_4_0.rb index d5df708..4269065 100644 --- a/lib/rails/convert_views_3_2_to_4_0.rb +++ b/lib/rails/convert_views_3_2_to_4_0.rb @@ -33,7 +33,9 @@ } do group do delete 'arguments.arguments.last.confirm_element', and_comma: true - insert 'data: { confirm: {{arguments.arguments.last.confirm_value}} }', to: 'arguments.arguments.last', and_comma: true + insert 'data: { confirm: {{arguments.arguments.last.confirm_value}} }', + to: 'arguments.arguments.last', + and_comma: true end end @@ -47,7 +49,9 @@ } do group do delete 'arguments.arguments.last.confirm_element', and_comma: true - insert '{{arguments.arguments.last.confirm_element}}', to: 'arguments.arguments.last.data_element.value.elements', and_comma: true + insert '{{arguments.arguments.last.confirm_element}}', + to: 'arguments.arguments.last.data_element.value.elements', + and_comma: true end end end diff --git a/lib/rails/fix_model_3_2_deprecations.rb b/lib/rails/fix_model_3_2_deprecations.rb index 538fdf5..3335800 100644 --- a/lib/rails/fix_model_3_2_deprecations.rb +++ b/lib/rails/fix_model_3_2_deprecations.rb @@ -65,7 +65,16 @@ # set_sequence_name = "seq" => self.sequence_name = "seq" # set_primary_key = "id" => self.primary_key = "id" # set_locking_column = "lock" => self.locking_column = "lock" - with_node node_type: 'call_node', name: { in: %w[set_table_name set_inheritance_column set_sequence_name set_primary_key set_locking_column] } do + with_node node_type: 'call_node', + name: { + in: %w[ + set_table_name + set_inheritance_column + set_sequence_name + set_primary_key + set_locking_column + ] + } do replace :message, with: "self.#{node.name.to_s.sub('set_', '')} =" end end diff --git a/lib/rails/migrate-ujs-to-turbo.rb b/lib/rails/migrate-ujs-to-turbo.rb index 3729618..a094f61 100644 --- a/lib/rails/migrate-ujs-to-turbo.rb +++ b/lib/rails/migrate-ujs-to-turbo.rb @@ -60,7 +60,10 @@ } } do delete 'arguments.arguments.-1.method_element', and_comma: true - insert 'data: { turbo_method: {{arguments.arguments.-1.method_source}} }', to: 'arguments.arguments.-1', at: 'end', and_comma: true + insert 'data: { turbo_method: {{arguments.arguments.-1.method_source}} }', + to: 'arguments.arguments.-1', + at: 'end', + and_comma: true end # submit_tag "Create", data: { disable_with: "Submitting..." } @@ -73,7 +76,10 @@ node_type: 'arguments_node', arguments: { size: 2, - last: { node_type: 'keyword_hash_node', data_value: { node_type: 'hash_node', disable_with_value: { not: nil } } } + last: { + node_type: 'keyword_hash_node', + data_value: { node_type: 'hash_node', disable_with_value: { not: nil } } + } } } do replace "arguments.arguments.1.data_value.disable_with_element.key", with: 'turbo_submits_with:' diff --git a/lib/rails/new_enum_syntax.rb b/lib/rails/new_enum_syntax.rb index ad465ff..72585a4 100644 --- a/lib/rails/new_enum_syntax.rb +++ b/lib/rails/new_enum_syntax.rb @@ -1,75 +1,76 @@ # frozen_string_literal: true Synvert::Rewriter.new 'rails', 'new_enum_syntax' do - configure(parser: Synvert::PRISM_PARSER) + configure(parser: Synvert::PRISM_PARSER) - description <<~EOS - New enum syntax introduced in Rails 7.0. + description <<~EOS + New enum syntax introduced in Rails 7.0. - ```ruby - class Post < ApplicationRecord - enum status: [ :draft, :published, :archived ], _prefix: true, _scopes: false - enum category: [ :free, :premium ], _suffix: true, _default: :free + ```ruby + class Post < ApplicationRecord + enum status: [ :draft, :published, :archived ], _prefix: true, _scopes: false + enum category: [ :free, :premium ], _suffix: true, _default: :free - enum status: { draft: 0, published: 1, archived: 2 }, prefix: true, scopes: false - enum category: { free: 0, premium: 1 }, suffix: true, default: :free - end - ``` + enum status: { draft: 0, published: 1, archived: 2 }, prefix: true, scopes: false + enum category: { free: 0, premium: 1 }, suffix: true, default: :free + end + ``` - => + => - ```ruby - class Post < ApplicationRecord - enum :status, [ :draft, :published, :archived ], prefix: true, scopes: false - enum :category, [ :free, :premium ], suffix: true, default: :free + ```ruby + class Post < ApplicationRecord + enum :status, [ :draft, :published, :archived ], prefix: true, scopes: false + enum :category, [ :free, :premium ], suffix: true, default: :free - enum :status, { draft: 0, published: 1, archived: 2 }, prefix: true, scopes: false - enum :category, { free: 0, premium: 1 }, suffix: true, default: :free - end - ``` - EOS + enum :status, { draft: 0, published: 1, archived: 2 }, prefix: true, scopes: false + enum :category, { free: 0, premium: 1 }, suffix: true, default: :free + end + ``` + EOS - if_gem 'rails', '>= 7.0' + if_gem 'rails', '>= 7.0' - within_files Synvert::RAILS_MODEL_FILES do - with_node node_type: 'call_node', - receiver: nil, - name: 'enum', + within_files Synvert::RAILS_MODEL_FILES do + with_node node_type: 'call_node', + receiver: nil, + name: 'enum', + arguments: { + node_type: 'arguments_node', arguments: { - node_type: 'arguments_node', - arguments: { - size: 1, - first: { - node_type: 'keyword_hash_node', - elements: { - first: { - node_type: 'assoc_node', - value: { node_type: { in: ['array_node', 'hash_node'] } } - } + size: 1, + first: { + node_type: 'keyword_hash_node', + elements: { + first: { + node_type: 'assoc_node', + value: { node_type: { in: ['array_node', 'hash_node'] } } } } } - } do - replace 'arguments.arguments.0.elements.0', with: ":{{arguments.arguments.0.elements.0.key.unescaped}}, {{arguments.arguments.0.elements.0.value}}" - end + } + } do + replace 'arguments.arguments.0.elements.0', + with: ":{{arguments.arguments.0.elements.0.key.unescaped}}, {{arguments.arguments.0.elements.0.value}}" + end - with_node node_type: 'call_node', - receiver: nil, - name: 'enum', + with_node node_type: 'call_node', + receiver: nil, + name: 'enum', + arguments: { + node_type: 'arguments_node', arguments: { - node_type: 'arguments_node', - arguments: { - size: 1, - last: { - node_type: 'keyword_hash_node', - } + size: 1, + last: { + node_type: 'keyword_hash_node', } - } do - goto_node 'arguments.arguments.-1' do - with_node node_type: 'assoc_node', key: /\A_/ do - replace 'key.value', with: node.key.value[1..-1] - end + } + } do + goto_node 'arguments.arguments.-1' do + with_node node_type: 'assoc_node', key: /\A_/ do + replace 'key.value', with: node.key.value[1..-1] end end end end +end diff --git a/lib/rails/prefer_nor_conditions.rb b/lib/rails/prefer_nor_conditions.rb index be05c6e..47313c2 100644 --- a/lib/rails/prefer_nor_conditions.rb +++ b/lib/rails/prefer_nor_conditions.rb @@ -23,7 +23,13 @@ with_node node_type: 'call_node', receiver: { node_type: 'call_node', name: 'where', arguments: nil }, name: 'not', - arguments: { node_type: 'arguments_node', arguments: { size: 1, first: { node_type: 'keyword_hash_node', elements: { length: { gt: 1 } } } } } do + arguments: { + node_type: 'arguments_node', + arguments: { + size: 1, + first: { node_type: 'keyword_hash_node', elements: { length: { gt: 1 } } } + } + } do new_source = node.arguments.arguments.first.elements.map { |element| "where.not(#{element.to_source})" } .join('.') replace 'receiver.message', :closing, with: new_source diff --git a/lib/rails/redirect_with_flash.rb b/lib/rails/redirect_with_flash.rb index 35c8283..e86e31b 100644 --- a/lib/rails/redirect_with_flash.rb +++ b/lib/rails/redirect_with_flash.rb @@ -37,7 +37,10 @@ msg = nil remover_action = nil flash_type = nil - with_node node_type: 'call_node', receiver: 'flash', name: '[]=', arguments: { node_type: 'arguments_node', arguments: { size: 2 } } do + with_node node_type: 'call_node', + receiver: 'flash', + name: '[]=', + arguments: { node_type: 'arguments_node', arguments: { size: 2 } } do line = mutation_adapter.get_start_loc(node).line flash_type = node.arguments.arguments.first.to_source msg = node.arguments.arguments.last.to_source diff --git a/lib/rails/strong_parameters.rb b/lib/rails/strong_parameters.rb index a16e386..ad198b8 100644 --- a/lib/rails/strong_parameters.rb +++ b/lib/rails/strong_parameters.rb @@ -75,17 +75,21 @@ def xxx_params # assign and remove attr_protected ... with_node node_type: 'call_node', name: 'attr_protected' do - parameters[object_name] = table_columns.map { |column| ":#{column}" } - node.arguments.arguments.map(&:to_source) + parameters[object_name] = table_columns.map { |column| + ":#{column}" + } - node.arguments.arguments.map(&:to_source) remove end end end - within_file Synvert::RAILS_CONTROLLER_FILES do within_node node_type: 'class_node' do object_name = node.name.to_s.sub('Controller', '').singularize.underscore - if_exist_node node_type: 'call_node', receiver: 'params', name: '[]', arguments: { node_type: 'arguments_node', arguments: [object_name.to_sym] } do + if_exist_node node_type: 'call_node', + receiver: 'params', + name: '[]', + arguments: { node_type: 'arguments_node', arguments: [object_name.to_sym] } do if parameters[object_name] # append def xxx_params; ...; end permit_params = parameters[object_name].join(', ') diff --git a/lib/rails/test_request_methods_use_keyword_arguments.rb b/lib/rails/test_request_methods_use_keyword_arguments.rb index e0fc05e..b504d00 100644 --- a/lib/rails/test_request_methods_use_keyword_arguments.rb +++ b/lib/rails/test_request_methods_use_keyword_arguments.rb @@ -42,7 +42,21 @@ within_files Synvert::RAILS_CONTROLLER_TEST_FILES + Synvert::RAILS_INTEGRATION_TEST_FILES do with_node node_type: 'call_node', name: { in: request_methods }, - arguments: { node_type: 'arguments_node', arguments: { size: 2, '-1': { node_type: { in: %w[local_variable_read_node instance_variable_read_node call_node] } } } } do + arguments: { + node_type: 'arguments_node', + arguments: { + size: 2, + '-1': { + node_type: { + in: %w[ + local_variable_read_node + instance_variable_read_node + call_node + ] + } + } + } + } do insert '**', to: 'arguments.arguments.-1', at: 'beginning' end end diff --git a/lib/rails/update_active_storage_variant_argument.rb b/lib/rails/update_active_storage_variant_argument.rb index 8d10934..972a673 100644 --- a/lib/rails/update_active_storage_variant_argument.rb +++ b/lib/rails/update_active_storage_variant_argument.rb @@ -28,27 +28,60 @@ within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do with_node node_type: 'call_node', name: 'variant', - arguments: { node_type: 'arguments_node', arguments: { size: 1, first: { node_type: 'keyword_hash_node', resize_value: { node_type: 'string_node', unescaped: { last: 'x' } } } } } do + arguments: { + node_type: 'arguments_node', + arguments: { + size: 1, + first: { + node_type: 'keyword_hash_node', + resize_value: { node_type: 'string_node', unescaped: { last: 'x' } } + } + } + } do width = node.arguments.arguments.first.resize_value.to_value.to_i replace 'arguments.arguments.first.resize_element', with: "resize_to_limit: [#{width}, nil]" end with_node node_type: 'call_node', name: 'variant', - arguments: { node_type: 'arguments_node', arguments: { size: 1, first: { node_type: 'keyword_hash_node', crop_value: { node_type: 'string_node' } } } } do + arguments: { + node_type: 'arguments_node', + arguments: { + size: 1, + first: { node_type: 'keyword_hash_node', crop_value: { node_type: 'string_node' } } + } + } do width, height, x, y = node.arguments.arguments.first.crop_value.to_value.split(/x|\+/) replace :arguments, with: "crop: [#{x}, #{y}, #{width}, #{height}]" end with_node node_type: 'call_node', name: 'variant', - arguments: { node_type: 'arguments_node', arguments: { size: 1, first: { node_type: 'keyword_hash_node', resize_and_pad_value: { node_type: 'array_node', elements: { size: 2 } } } } } do + arguments: { + node_type: 'arguments_node', + arguments: { + size: 1, + first: { + node_type: 'keyword_hash_node', + resize_and_pad_value: { + node_type: 'array_node', + elements: { size: 2 } + } + } + } + } do insert 'background: [255]', to: 'arguments.arguments.0.elements.0.value.elements.-1', at: 'end', and_comma: true end with_node node_type: 'call_node', name: 'variant', - arguments: { node_type: 'arguments_node', arguments: { size: 1, first: { node_type: 'keyword_hash_node', monochrome_value: true } } } do + arguments: { + node_type: 'arguments_node', + arguments: { + size: 1, + first: { node_type: 'keyword_hash_node', monochrome_value: true } + } + } do replace 'arguments.arguments.0.monochrome_element', with: 'colourspace: "b-w"' end end diff --git a/lib/rails/use_active_storage_image_processing_macros.rb b/lib/rails/use_active_storage_image_processing_macros.rb index cb41301..8f7221d 100644 --- a/lib/rails/use_active_storage_image_processing_macros.rb +++ b/lib/rails/use_active_storage_image_processing_macros.rb @@ -24,7 +24,15 @@ if_gem 'activestorage', '>= 6.1' within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do - with_node node_type: 'call_node', name: 'preview', arguments: { node_type: 'arguments_node', arguments: { size: 1, first: { node_type: 'keyword_hash_node', resize_value: { not: nil } } } } do + with_node node_type: 'call_node', + name: 'preview', + arguments: { + node_type: 'arguments_node', + arguments: { + size: 1, + first: { node_type: 'keyword_hash_node', resize_value: { not: nil } } + } + } do resize_value = node.arguments.arguments.first.resize_value.to_value width, height = resize_value.split('x') if resize_value.ends_with?('>') diff --git a/lib/rails_best_practices/always_add_db_index.rb b/lib/rails_best_practices/always_add_db_index.rb index b732dc7..8c49bd9 100644 --- a/lib/rails_best_practices/always_add_db_index.rb +++ b/lib/rails_best_practices/always_add_db_index.rb @@ -17,7 +17,10 @@ table_name = model_definition.name.tableize table_definition = definitions.find_table_definition_by_table_name(table_name) - model_definition.associations.select { |association_definition| association_definition.type == 'belongs_to' }.each do |association_definition| + model_definition.associations.select { |association_definition| + association_definition.type == 'belongs_to' + } +.each do |association_definition| if association_definition.options[:polymorphic] foreign_key = association_definition.options[:foreign_key]&.to_s || association_definition.name.foreign_key foreign_type = association_definition.options[:foreign_type]&.to_s || (association_definition.name.demodulize + '_type').underscore diff --git a/lib/rspec/one_liner_expectation.rb b/lib/rspec/one_liner_expectation.rb index f219082..fd344a4 100644 --- a/lib/rspec/one_liner_expectation.rb +++ b/lib/rspec/one_liner_expectation.rb @@ -38,7 +38,9 @@ { should: 'to', should_not: 'not_to' }.each do |old_message, new_message| # it { should matcher } => it { is_expected.to matcher } # it { should_not matcher } => it { is_expected.not_to matcher } - with_node node_type: 'block', caller: { message: 'it' }, body: { size: 1, first: { node_type: 'send', receiver: nil, message: old_message } } do + with_node node_type: 'block', + caller: { message: 'it' }, + body: { size: 1, first: { node_type: 'send', receiver: nil, message: old_message } } do receiver = node.body.first.arguments.first.receiver unless receiver && matcher_converters.include?(receiver.message) matcher = node.body.first.arguments.first.to_source @@ -60,20 +62,23 @@ matcher_converters.each do |old_matcher, new_matcher| with_node node_type: 'block', caller: { message: 'it' }, - body: { size: 1, first: { - node_type: 'send', - receiver: nil, - message: old_message, - arguments: { - first: { - node_type: 'send', - receiver: { + body: { + size: 1, + first: { + node_type: 'send', + receiver: nil, + message: old_message, + arguments: { + first: { node_type: 'send', - message: old_matcher + receiver: { + node_type: 'send', + message: old_matcher + } } } } - } } do + } do times = node.body.first.arguments.first.receiver.arguments.first.to_source items_name = node.body.first.arguments.first.message if :items == items_name diff --git a/lib/ruby/nested_class_definition.rb b/lib/ruby/nested_class_definition.rb index 5fa8afe..a0490f7 100644 --- a/lib/ruby/nested_class_definition.rb +++ b/lib/ruby/nested_class_definition.rb @@ -38,8 +38,16 @@ def test; end parts = node.name.to_source.split('::') class_name = parts.pop new_parts = parts.map.with_index { |mod, index| ' ' * NodeMutation.tab_width * index + "module #{mod}" } - new_parts.concat(source.sub(parts.join('::') + '::', '').split("\n").map { |line| (' ' * NodeMutation.tab_width * parts.size) + line }) - new_parts.concat(parts.map.with_index { |mod, index| ' ' * NodeMutation.tab_width * (parts.size - index - 1) + "end" }) + new_parts.concat( + source.sub(parts.join('::') + '::', '').split("\n").map { |line| + (' ' * NodeMutation.tab_width * parts.size) + line + } + ) + new_parts.concat( + parts.map.with_index { |mod, index| + ' ' * NodeMutation.tab_width * (parts.size - index - 1) + "end" + } + ) replace_with new_parts.join("\n") end diff --git a/lib/ruby/prefer-endless-method.rb b/lib/ruby/prefer-endless-method.rb index 37ffd63..361ea4d 100644 --- a/lib/ruby/prefer-endless-method.rb +++ b/lib/ruby/prefer-endless-method.rb @@ -32,7 +32,8 @@ def one_plus_one = 1 + 1 break if first_body_node.type == :call_node && first_body_node.opening.nil? && !first_body_node.arguments.nil? && first_body_node.closing.nil? && !first_body_node.block.nil? body_column = mutation_adapter.get_start_loc(first_body_node).column - new_body = first_body_node.to_source.split("\n").map { |line| line.sub(/^ {#{body_column}}/, '') }.join("\n") + new_body = first_body_node.to_source.split("\n").map { |line| line.sub(/^ {#{body_column}}/, '') } + .join("\n") receiver_and_name = node.receiver ? "#{node.receiver.to_source}.#{node.name}" : node.name.to_s replace_with "def #{receiver_and_name}{{lparen}}{{parameters}}{{rparen}} = #{new_body}" end diff --git a/lib/ruby/use_keyword_arguments.rb b/lib/ruby/use_keyword_arguments.rb index 871ae6c..7eee618 100644 --- a/lib/ruby/use_keyword_arguments.rb +++ b/lib/ruby/use_keyword_arguments.rb @@ -28,7 +28,21 @@ with_node node_type: 'call_node', receiver: 'CSV', name: 'generate', - arguments: { node_type: 'arguments_node', arguments: { size: 1, '0': { node_type: { in: %w[local_variable_read_node instance_variable_read_node call_node] } } } } do + arguments: { + node_type: 'arguments_node', + arguments: { + size: 1, + '0': { + node_type: { + in: %w[ + local_variable_read_node + instance_variable_read_node + call_node + ] + } + } + } + } do insert '**', to: 'arguments.arguments.0', at: 'beginning' end end diff --git a/spec/helpers/parse_rails_spec.rb b/spec/helpers/parse_rails_spec.rb index 7933683..7de8977 100644 --- a/spec/helpers/parse_rails_spec.rb +++ b/spec/helpers/parse_rails_spec.rb @@ -29,20 +29,22 @@ definitions = rewriter.process - expect(definitions.table_definitions.to_h).to eq([ - { - name: "users", - columns: [ - { name: "name", type: "string" }, - { name: "email", type: "string" }, - { name: "created_at", type: "datetime" }, - { name: "updated_at", type: "datetime" } - ], - indices: [ - { name: "index_users_on_email", columns: ["email"] } - ] - } - ]) + expect(definitions.table_definitions.to_h).to eq( + [ + { + name: "users", + columns: [ + { name: "name", type: "string" }, + { name: "email", type: "string" }, + { name: "created_at", type: "datetime" }, + { name: "updated_at", type: "datetime" } + ], + indices: [ + { name: "index_users_on_email", columns: ["email"] } + ] + } + ] + ) end it 'gets model definitions' do @@ -66,20 +68,30 @@ class Picture < ApplicationRecord definitions = rewriter.process - expect(definitions.model_definitions.to_h).to eq([ - { - name: 'Picture', - associations: [ - { name: 'imageable', type: 'belongs_to', options: { polymorphic: true } }, - ] - }, - { - name: 'User', - associations: [ - { name: 'organization', type: 'belongs_to', options: {} }, - { name: 'posts', type: 'has_many', options: {} } - ] - } - ]) + expect(definitions.model_definitions.to_h).to eq( + [ + { + name: 'Picture', + associations: [ + { + name: 'imageable', + type: 'belongs_to', + options: { polymorphic: true } + }, + ] + }, + { + name: 'User', + associations: [ + { + name: 'organization', + type: 'belongs_to', + options: {} + }, + { name: 'posts', type: 'has_many', options: {} } + ] + } + ] + ) end end diff --git a/spec/helpers/parse_ruby_spec.rb b/spec/helpers/parse_ruby_spec.rb index 3580b04..4ab01f3 100644 --- a/spec/helpers/parse_ruby_spec.rb +++ b/spec/helpers/parse_ruby_spec.rb @@ -51,62 +51,66 @@ def user_type definitions = rewriter.process - expect(definitions.to_h).to eq({ - classes: [], - modules: [ - { - name: "Synvert", - classes: [ - { - name: "Admin", - superclass: "User", - classes: [], - modules: [], - methods: [{ name: "user_type", local_calls: [] }], - static_methods: [], - constants: [], - include_modules: [], - prepend_modules: [], - extend_modules: [], - singleton: nil, - ancestors: ["Synvert::User", "Authenticatable", "Trackable"] - }, - { - name: "User", - superclass: nil, - singleton: { + expect(definitions.to_h).to eq( + { + classes: [], + modules: [ + { + name: "Synvert", + classes: [ + { + name: "Admin", + superclass: "User", + classes: [], + modules: [], + methods: [{ name: "user_type", local_calls: [] }], + static_methods: [], constants: [], - methods: [ - { name: 'system', local_calls: [] }, - { name: 'bot', local_calls: [] } - ], - ancestors: [] + include_modules: [], + prepend_modules: [], + extend_modules: [], + singleton: nil, + ancestors: ["Synvert::User", "Authenticatable", "Trackable"] }, - classes: [], - modules: [], - methods: [{ name: "user_type", local_calls: [] }], - static_methods: [{ - name: 'authenticate?', - local_calls: ['find_by'] - }], - constants: [{ name: "ROLES" }], - include_modules: ["Trackable"], - prepend_modules: ["Authenticatable"], - extend_modules: ["ClassMethods"], - ancestors: ["Authenticatable", "Trackable"] - } - ], - modules: [], - methods: [], - static_methods: [], - constants: [], - singleton: nil, - ancestors: [] - } - ], - constants: [], - methods: [] - }) + { + name: "User", + superclass: nil, + singleton: { + constants: [], + methods: [ + { name: 'system', local_calls: [] }, + { name: 'bot', local_calls: [] } + ], + ancestors: [] + }, + classes: [], + modules: [], + methods: [{ name: "user_type", local_calls: [] }], + static_methods: [ + { + name: 'authenticate?', + local_calls: ['find_by'] + } + ], + constants: [{ name: "ROLES" }], + include_modules: ["Trackable"], + prepend_modules: ["Authenticatable"], + extend_modules: ["ClassMethods"], + ancestors: ["Authenticatable", "Trackable"] + } + ], + modules: [], + methods: [], + static_methods: [], + constants: [], + singleton: nil, + ancestors: [] + } + ], + constants: [], + methods: [] + } + ) end it 'finds class by full_name' do diff --git a/spec/rails/convert_configs_3_2_to_4_0_spec.rb b/spec/rails/convert_configs_3_2_to_4_0_spec.rb index bc3d5f1..8a4c33d 100644 --- a/spec/rails/convert_configs_3_2_to_4_0_spec.rb +++ b/spec/rails/convert_configs_3_2_to_4_0_spec.rb @@ -96,7 +96,6 @@ class Application < Rails::Application Synvert::Application.config.secret_key_base = "bf4f3f46924ecd9adcb6515681c78144545bba454420973a274d7021ff946b8ef043a95ca1a15a9d1b75f9fbdf85d1a3afaf22f4e3c2f3f78e24a0a188b581df" EOS - let(:fake_file_paths) { %w[ config/application.rb diff --git a/spec/rails_best_practices/always_add_db_index_spec.rb b/spec/rails_best_practices/always_add_db_index_spec.rb index 08027b4..e6a18b2 100644 --- a/spec/rails_best_practices/always_add_db_index_spec.rb +++ b/spec/rails_best_practices/always_add_db_index_spec.rb @@ -50,10 +50,12 @@ class Comment < ApplicationRecord belongs_to :user end EOF - let(:warnings) { [ - '/db/schema.rb#6: always add db index comments => ["post_id"]', - '/db/schema.rb#7: always add db index comments => ["user_id"]' - ] } + let(:warnings) { + [ + '/db/schema.rb#6: always add db index comments => ["post_id"]', + '/db/schema.rb#7: always add db index comments => ["user_id"]' + ] + } include_examples 'warnable' end diff --git a/spec/ruby/kernel_open_to_uri_open_spec.rb b/spec/ruby/kernel_open_to_uri_open_spec.rb index 872c498..470af73 100644 --- a/spec/ruby/kernel_open_to_uri_open_spec.rb +++ b/spec/ruby/kernel_open_to_uri_open_spec.rb @@ -13,13 +13,13 @@ context 'not convert when define open method' do let(:rewriter_name) { 'ruby/kernel_open_to_uri_open' } - let(:test_content) {<<~EOS} + let(:test_content) { <<~EOS } def open(url) end open('http://test.com') EOS - let(:test_rewritten_content) {<<~EOS} + let(:test_rewritten_content) { <<~EOS } def open(url) end