├── .github └── workflows │ ├── close_inactive_issues.yml │ └── main.yml ├── .gitignore ├── .rspec ├── Gemfile ├── Gemfile.lock ├── Guardfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── lib ├── bullet │ └── rename_whitelist_to_safelist.rb ├── bundler │ └── use_shortcut_git_source.rb ├── debug_me │ └── remove_debug_me_calls.rb ├── default │ └── check_syntax.rb ├── factory_bot │ ├── convert_factory_girl_to_factory_bot.rb │ ├── deprecate_static_value.rb │ ├── use_short_syntax.rb │ └── use_string_as_class_name.rb ├── faker │ └── use_keyword_arguments.rb ├── helpers │ ├── parse_rails.rb │ ├── parse_ruby.rb │ └── set_rails_load_defaults.rb ├── minitest │ ├── assert_empty.rb │ ├── assert_equal_arguments_order.rb │ ├── assert_includes.rb │ ├── assert_instance_of.rb │ ├── assert_kind_of.rb │ ├── assert_match.rb │ ├── assert_nil.rb │ ├── assert_operator.rb │ ├── assert_path_exists.rb │ ├── assert_predicate.rb │ ├── assert_respond_to.rb │ ├── assert_same.rb │ ├── assert_silent.rb │ ├── assert_truthy.rb │ ├── hooks_super.rb │ ├── refute_empty.rb │ ├── refute_equal.rb │ ├── refute_false.rb │ ├── refute_includes.rb │ ├── refute_instance_of.rb │ ├── refute_kind_of.rb │ ├── refute_match.rb │ ├── refute_nil.rb │ ├── refute_operator.rb │ ├── refute_path_exists.rb │ ├── refute_predicate.rb │ ├── refute_respond_to.rb │ ├── refute_same.rb │ └── use_better_syntax.rb ├── rails │ ├── active_record_association_call_use_keyword_arguments.rb │ ├── add_active_record_migration_rails_version.rb │ ├── add_application_job.rb │ ├── add_application_mailer.rb │ ├── add_application_record.rb │ ├── application_secrets_use_symbol_keys.rb │ ├── convert_active_record_dirty_5_0_to_5_1.rb │ ├── convert_after_commit.rb │ ├── convert_configs_2_3_to_3_0.rb │ ├── convert_configs_3_0_to_3_1.rb │ ├── convert_configs_3_1_to_3_2.rb │ ├── convert_configs_3_2_to_4_0.rb │ ├── convert_configs_4_0_to_4_1.rb │ ├── convert_configs_4_1_to_4_2.rb │ ├── convert_configs_4_2_to_5_0.rb │ ├── convert_configs_5_0_to_5_1.rb │ ├── convert_configs_5_1_to_5_2.rb │ ├── convert_configs_5_2_to_6_0.rb │ ├── convert_configs_6_0_to_6_1.rb │ ├── convert_configs_6_1_to_7_0.rb │ ├── convert_configs_7_0_to_7_1.rb │ ├── convert_configs_7_1_to_7_2.rb │ ├── convert_configs_7_2_to_8_0.rb │ ├── convert_constants_3_1_to_3_2.rb │ ├── convert_constants_3_2_to_4_0.rb │ ├── convert_constants_4_2_to_5_0.rb │ ├── convert_constants_5_0_to_5_1.rb │ ├── convert_controller_filter_to_action.rb │ ├── convert_dynamic_finders_for_rails_3.rb │ ├── convert_dynamic_finders_for_rails_4.rb │ ├── convert_env_to_request_env.rb │ ├── convert_head_response.rb │ ├── convert_mailers_2_3_to_3_0.rb │ ├── convert_model_errors_add.rb │ ├── convert_model_lambda_scope.rb │ ├── convert_models_2_3_to_3_0.rb │ ├── convert_models_3_2_to_4_0.rb │ ├── convert_rails_env.rb │ ├── convert_rails_logger.rb │ ├── convert_rails_root.rb │ ├── convert_render_text_to_render_plain.rb │ ├── convert_routes_2_3_to_3_0.rb │ ├── convert_routes_3_2_to_4_0.rb │ ├── convert_test_request_methods_4_2_to_5_0.rb │ ├── convert_to_response_parsed_body.rb │ ├── convert_update_attributes_to_update.rb │ ├── convert_views_2_3_to_3_0.rb │ ├── convert_views_3_2_to_4_0.rb │ ├── deprecate_active_record_migration_check_pending.rb │ ├── deprecate_errors_as_hash.rb │ ├── deprecate_multi_json.rb │ ├── enqueue_job_in_after_commit_callback.rb │ ├── explicitly-render-with-formats.rb │ ├── fix_model_3_2_deprecations.rb │ ├── migrate-ujs-to-turbo.rb │ ├── new_enum_syntax.rb │ ├── prefer_nor_conditions.rb │ ├── redirect_with_flash.rb │ ├── remove_active_support_dependencies_private_api.rb │ ├── rename_errors_keys_to_attribute_names.rb │ ├── strong_parameters.rb │ ├── test_request_methods_use_keyword_arguments.rb │ ├── update_active_storage_variant_argument.rb │ ├── upgrade_2_3_to_3_0.rb │ ├── upgrade_3_0_to_3_1.rb │ ├── upgrade_3_1_to_3_2.rb │ ├── upgrade_3_2_to_4_0.rb │ ├── upgrade_4_0_to_4_1.rb │ ├── upgrade_4_1_to_4_2.rb │ ├── upgrade_4_2_to_5_0.rb │ ├── upgrade_5_0_to_5_1.rb │ ├── upgrade_5_1_to_5_2.rb │ ├── upgrade_5_2_to_6_0.rb │ ├── upgrade_6_0_to_6_1.rb │ ├── upgrade_6_1_to_7_0.rb │ ├── upgrade_7_0_to_7_1.rb │ ├── upgrade_7_1_to_7_2.rb │ ├── upgrade_7_2_to_8_0.rb │ ├── use_active_storage_image_processing_macros.rb │ └── use_migrations_instance_methods.rb ├── rails_best_practices │ ├── always_add_db_index.rb │ └── use_scope_access.rb ├── redis │ ├── deprecate_calling_redis_inside_multi.rb │ └── deprecate_calling_redis_inside_pipelined.rb ├── rspec │ ├── be_close_to_be_within.rb │ ├── block_to_expect.rb │ ├── boolean_matcher.rb │ ├── collection_matcher.rb │ ├── custom_matcher_new_syntax.rb │ ├── explicit_spec_type.rb │ ├── its_to_it.rb │ ├── message_expectation.rb │ ├── method_stub.rb │ ├── negative_error_expectation.rb │ ├── new_config_options.rb │ ├── new_hook_scope.rb │ ├── one_liner_expectation.rb │ ├── pending_to_skip.rb │ ├── remove_monkey_patches.rb │ ├── should_to_expect.rb │ ├── stub_and_mock_to_double.rb │ └── use_new_syntax.rb ├── ruby │ ├── block_to_yield.rb │ ├── deprecate_big_decimal_new.rb │ ├── deprecate_dir_exists.rb │ ├── deprecate_file_exists.rb │ ├── deprecate_fixnum_and_bignum.rb │ ├── fast_syntax.rb │ ├── frozen_string_literal_comment.rb │ ├── gsub_to_tr.rb │ ├── kernel_open_to_uri_open.rb │ ├── keys_each_to_each_key.rb │ ├── map_and_flatten_to_flat_map.rb │ ├── merge_to_square_brackets.rb │ ├── nested_class_definition.rb │ ├── new_1_9_hash_syntax.rb │ ├── new_2_2_hash_syntax.rb │ ├── new_lambda_syntax.rb │ ├── new_safe_navigation_operator.rb │ ├── numbered_parameters.rb │ ├── parallel_assignment_to_sequential_assignment.rb │ ├── prefer-endless-method.rb │ ├── prefer_dig.rb │ ├── prefer_nil.rb │ ├── remove_debug_code.rb │ ├── shorthand_hash_syntax.rb │ ├── upgrade_1_8_to_1_9.rb │ ├── upgrade_2_0_to_2_1.rb │ ├── upgrade_2_1_to_2_2.rb │ ├── upgrade_2_2_to_2_3.rb │ ├── upgrade_2_3_to_2_4.rb │ ├── upgrade_2_4_to_2_5.rb │ ├── upgrade_2_5_to_2_6.rb │ ├── upgrade_2_6_to_2_7.rb │ ├── upgrade_2_7_to_3_0.rb │ ├── upgrade_3_0_to_3_1.rb │ ├── upgrade_3_1_to_3_2.rb │ ├── upgrade_3_3_to_3_4.rb │ ├── uri_escape_to_uri_default_parser_escape.rb │ ├── use_it_keyword.rb │ ├── use_keyword_arguments.rb │ └── use_symbol_to_proc.rb ├── shoulda │ ├── fix_1_5_deprecations.rb │ ├── fix_2_6_deprecations.rb │ ├── use_matcher_syntax.rb │ └── use_new_syntax.rb └── will_paginate │ └── use_new_syntax.rb └── spec ├── bullet └── rename_whitelist_to_safelist_spec.rb ├── bundler └── use_shortcut_git_source_spec.rb ├── debug_me └── remove_debug_me_calls_spec.rb ├── factory_bot ├── convert_factory_girl_to_factory_bot_spec.rb ├── deprecate_static_value_spec.rb ├── use_short_syntax_spec.rb └── use_string_as_class_name_spec.rb ├── faker └── use_keyword_arguments_spec.rb ├── helpers ├── parse_rails_spec.rb ├── parse_ruby_spec.rb └── set_rails_load_defaults_spec.rb ├── minitest ├── assert_empty_spec.rb ├── assert_equal_arguments_order_spec.rb ├── assert_includes_spec.rb ├── assert_instance_of_spec.rb ├── assert_kind_of_spec.rb ├── assert_match_spec.rb ├── assert_nil_spec.rb ├── assert_operator_spec.rb ├── assert_path_exists_spec.rb ├── assert_predicate_spec.rb ├── assert_respond_to_spec.rb ├── assert_same_spec.rb ├── assert_silent_spec.rb ├── assert_truthy_spec.rb ├── hooks_super_spec.rb ├── refute_empty_spec.rb ├── refute_equal_spec.rb ├── refute_false_spec.rb ├── refute_includes_spec.rb ├── refute_instance_of_spec.rb ├── refute_kind_of_spec.rb ├── refute_match_spec.rb ├── refute_nil_spec.rb ├── refute_operator_spec.rb ├── refute_path_exists_spec.rb ├── refute_predicate_spec.rb ├── refute_respond_to_spec.rb └── refute_same_spec.rb ├── rails ├── active_record_association_call_use_keyword_arguments_spec.rb ├── add_active_record_migration_rails_version_spec.rb ├── add_application_job_spec.rb ├── add_application_mailer_spec.rb ├── add_application_record_spec.rb ├── application_secrets_use_symbol_keys_spec.rb ├── convert_active_record_dirty_5_0_to_5_1_spec.rb ├── convert_after_commit_spec.rb ├── convert_configs_2_3_to_3_0_spec.rb ├── convert_configs_3_0_to_3_1_spec.rb ├── convert_configs_3_1_to_3_2_spec.rb ├── convert_configs_3_2_to_4_0_spec.rb ├── convert_configs_4_0_to_4_1_spec.rb ├── convert_configs_4_1_to_4_2_spec.rb ├── convert_configs_4_2_to_5_0_spec.rb ├── convert_configs_5_0_to_5_1_spec.rb ├── convert_configs_5_1_to_5_2_spec.rb ├── convert_configs_5_2_to_6_0_spec.rb ├── convert_configs_6_0_to_6_1_spec.rb ├── convert_configs_6_1_to_7_0_spec.rb ├── convert_configs_7_0_to_7_1_spec.rb ├── convert_constants_3_1_to_3_2_spec.rb ├── convert_constants_3_2_to_4_0_spec.rb ├── convert_constants_4_2_to_5_0_spec.rb ├── convert_constants_5_0_to_5_1_spec.rb ├── convert_controller_filter_to_action_spec.rb ├── convert_dynamic_finders_for_rails_3_spec.rb ├── convert_dynamic_finders_for_rails_4_spec.rb ├── convert_env_to_request_env_spec.rb ├── convert_head_response_spec.rb ├── convert_mailers_2_3_to_3_0_spec.rb ├── convert_model_errors_add_spec.rb ├── convert_model_lambda_scope_spec.rb ├── convert_models_2_3_to_3_0_spec.rb ├── convert_models_3_2_to_4_0_spec.rb ├── convert_rails_env_spec.rb ├── convert_rails_logger_spec.rb ├── convert_rails_root_spec.rb ├── convert_render_text_to_render_plain_spec.rb ├── convert_routes_2_3_to_3_0_spec.rb ├── convert_routes_3_2_to_4_0_spec.rb ├── convert_test_request_methods_4_2_to_5_0_spec.rb ├── convert_to_response_parsed_body_spec.rb ├── convert_update_attributes_to_update_spec.rb ├── convert_views_2_3_to_3_0_spec.rb ├── convert_views_3_2_to_4_0_spec.rb ├── deprecate_active_record_migration_check_pending_spec.rb ├── deprecate_errors_as_hash_spec.rb ├── deprecate_multi_json_spec.rb ├── enqueue_job_in_after_commit_callback_spec.rb ├── explicitly-render-with-formats_spec.rb ├── fix_model_3_2_deprecations_spec.rb ├── migrate-ujs-to-turbo_spec.rb ├── new_enum_syntax_spec.rb ├── prefer_nor_conditions_spec.rb ├── redirect_with_flash_spec.rb ├── remove_active_support_dependencies_private_api_spec.rb ├── rename_errors_keys_to_attribute_names_spec.rb ├── strong_parameters_spec.rb ├── test_request_methods_use_keyword_arguments_spec.rb ├── update_active_storage_variant_argument_spec.rb ├── use_active_storage_image_processing_macros_spec.rb └── use_migrations_instance_methods_spec.rb ├── rails_best_practices └── always_add_db_index_spec.rb ├── redis ├── deprecate_calling_redis_inside_multi_spec.rb └── deprecate_calling_redis_inside_pipelined_spec.rb ├── rspec ├── be_close_to_be_within_spec.rb ├── block_to_expect_spec.rb ├── boolean_matcher_spec.rb ├── collection_matcher_spec.rb ├── custom_matcher_new_syntax_spec.rb ├── explicit_spec_type_spec.rb ├── its_to_it_spec.rb ├── message_expectation_spec.rb ├── method_stub_spec.rb ├── negative_error_expectation_spec.rb ├── new_config_options_spec.rb ├── new_hook_scope_spec.rb ├── one_liner_expectation_spec.rb ├── pending_to_skip_spec.rb ├── remove_monkey_patches_spec.rb ├── should_to_expect_spec.rb └── stub_and_mock_to_double_spec.rb ├── ruby ├── block_to_yield_spec.rb ├── deprecate_big_decimal_new_spec.rb ├── deprecate_dir_exists.rb ├── deprecate_file_exists.rb ├── deprecate_fixnum_and_bignum.rb ├── frozen_string_literal_comment_spec.rb ├── gsub_to_tr_spec.rb ├── kernel_open_to_uri_open_spec.rb ├── keys_each_to_each_key_spec.rb ├── map_and_flatten_to_flat_map_spec.rb ├── merge_to_square_brackets_spec.rb ├── nested_class_definition_spec.rb ├── new_1_9_hash_syntax_spec.rb ├── new_2_2_hash_syntax_spec.rb ├── new_lambda_syntax_spec.rb ├── new_safe_navigation_operator_spec.rb ├── numbered_parameters_spec.rb ├── parallel_assignment_to_sequential_assignment_spec.rb ├── prefer-endless-method_spec.rb ├── prefer_dig_spec.rb ├── prefer_nil_spec.rb ├── remove_debug_code_spec.rb ├── shorthand_hash_syntax_spec.rb ├── uri_escape_to_uri_default_parser_escape_spec.rb ├── use_it_keyword_spec.rb ├── use_keyword_arguments_spec.rb └── use_symbol_to_proc_spec.rb ├── shoulda ├── fix_1_5_deprecations_spec.rb ├── fix_2_6_deprecations_spec.rb └── use_matcher_syntax_spec.rb ├── spec_helper.rb ├── support ├── convertable.rb └── warnable.rb └── will_paginate └── use_new_syntax_spec.rb /.github/workflows/close_inactive_issues.yml: -------------------------------------------------------------------------------- 1 | name: Close inactive issues 2 | on: 3 | schedule: 4 | - cron: "30 1 * * *" 5 | 6 | jobs: 7 | close-issues: 8 | runs-on: ubuntu-latest 9 | permissions: 10 | issues: write 11 | pull-requests: write 12 | steps: 13 | - uses: actions/stale@v5 14 | with: 15 | days-before-issue-stale: 30 16 | days-before-issue-close: 14 17 | stale-issue-label: "stale" 18 | stale-issue-message: "This issue is stale because it has been open for 30 days with no activity." 19 | close-issue-message: "This issue was closed because it has been inactive for 14 days since being marked as stale." 20 | days-before-pr-stale: -1 21 | days-before-pr-close: -1 22 | repo-token: ${{ secrets.GITHUB_TOKEN }} 23 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | # This workflow uses actions that are not certified by GitHub. 2 | # They are provided by a third-party and are governed by 3 | # separate terms of service, privacy policy, and support 4 | # documentation. 5 | # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake 6 | # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby 7 | 8 | name: CI 9 | 10 | on: 11 | push: 12 | branches: [ main ] 13 | pull_request: 14 | branches: [ main ] 15 | 16 | jobs: 17 | test: 18 | 19 | runs-on: ubuntu-latest 20 | strategy: 21 | matrix: 22 | ruby-version: ['2.7', '3.0', '3.1', '3.2', '3.3', '3.4'] 23 | 24 | steps: 25 | - uses: actions/checkout@v2 26 | - name: Set up Ruby 27 | uses: ruby/setup-ruby@v1 28 | with: 29 | ruby-version: ${{ matrix.ruby-version }} 30 | bundler-cache: true 31 | - name: Run tests 32 | run: bundle exec rake 33 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | InstalledFiles 7 | _yardoc 8 | coverage 9 | doc/ 10 | lib/bundler/man 11 | pkg 12 | rdoc 13 | spec/reports 14 | test/tmp 15 | test/version_tmp 16 | tmp 17 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --color 2 | --format documentation 3 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | source 'https://rubygems.org' 4 | 5 | gem 'activesupport', '7.1.3.4' # support ruby 2.7 6 | gem 'fakefs', require: 'fakefs/safe' 7 | gem 'rake' 8 | gem 'rspec' 9 | gem 'synvert-core' 10 | 11 | gem 'guard' 12 | gem 'guard-bundler' 13 | gem 'guard-rspec' 14 | -------------------------------------------------------------------------------- /Guardfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | guard :rspec, cmd: 'bundle exec rspec' do 4 | watch(%r{^spec/.+_spec\.rb$}) 5 | watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } 6 | watch('spec/spec_helper.rb') { 'spec' } 7 | end 8 | 9 | guard :bundler do 10 | require 'guard/bundler' 11 | require 'guard/bundler/verify' 12 | helper = Guard::Bundler::Verify.new 13 | 14 | files = ['Gemfile'] 15 | files += Dir['*.gemspec'] if files.any? { |f| helper.uses_gemspec?(f) } 16 | 17 | # Assume files are symlinked from somewhere 18 | files.each { |file| watch(helper.real_path(file)) } 19 | end 20 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Xinmin Labs 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # synvert-snippets-ruby 2 | 3 | logo 4 | 5 | [![AwesomeCode Status for synvert-hq/synvert-snippets-ruby](https://awesomecode.io/projects/85ed8f64-5e54-4b8f-ab2f-dbfdaf377b3b/status)](https://awesomecode.io/repos/synvert-hq/synvert-snippets-ruby) 6 | [![Build Status](https://github.com/synvert-hq/synvert-snippets-ruby/actions/workflows/main.yml/badge.svg)](https://github.com/synvert-hq/synvert-snippets-ruby/actions/workflows/main.yml) 7 | 8 | Synvert snippets are used to describe how to rewrite ruby code based on [synvert-core-ruby](https://github.com/synvert-hq/synvert-core-ruby). 9 | 10 | To use snippets to convert ruby code, you need the CLI [synvert-ruby](https://github.com/synvert-hq/synvert-ruby). 11 | 12 | To practice how to write the snippet, you can try [Synvert Playground](https://playground.synvert.net/ruby). 13 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'rspec/core/rake_task' 4 | 5 | RSpec::Core::RakeTask.new(:spec) 6 | 7 | task :default => :spec 8 | -------------------------------------------------------------------------------- /lib/debug_me/remove_debug_me_calls.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'debug_me', 'remove_debug_me_calls' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | It removes `debug_me` calls. 8 | 9 | debug_me A tool to print the labeled value of variables. 10 | |__ http://github.com/MadBomber/debug_me 11 | EOS 12 | 13 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 14 | # removes debug_me methods 15 | # removes all debug_me calls that have a block 16 | find_node '.block[caller=.send[message=debug_me]]' do 17 | remove 18 | end 19 | end 20 | 21 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 22 | # removes all debug_me calls that DO NOT have a block 23 | find_node '.send[message=debug_me]' do 24 | remove 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /lib/default/check_syntax.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'default', 'check_syntax' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description 'Just used to check if there is a syntax error.' 7 | 8 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /lib/factory_bot/use_string_as_class_name.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'factory_bot', 'use_string_as_class_name' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | It uses string as class name 8 | 9 | ```ruby 10 | FactoryBot.define do 11 | factory :admin, class: User do 12 | name { 'Admin' } 13 | end 14 | end 15 | ``` 16 | 17 | => 18 | 19 | ```ruby 20 | FactoryBot.define do 21 | factory :admin, class: 'User' do 22 | name { 'Admin' } 23 | end 24 | end 25 | ``` 26 | EOS 27 | 28 | within_files Synvert::RAILS_FACTORY_FILES do 29 | find_node '.block[caller=.send[message=factory][arguments.size=2][arguments.-1=.hash[class_value=.const]]]' do 30 | replace 'caller.arguments.-1.class_value', with: wrap_with_quotes(node.caller.arguments.last.class_source) 31 | end 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /lib/helpers/set_rails_load_defaults.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Helper.new 'rails/set_load_defaults' do |options| 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | rails_version = options[:rails_version] 7 | 8 | within_file 'config/application.rb' do 9 | with_node node_type: 'class_node', constant_path: 'Application' do 10 | exists = false 11 | with_node node_type: 'call_node', receiver: 'config', name: 'load_defaults', arguments: { arguments: { length: 1 } } do 12 | exists = true 13 | replace_with "config.load_defaults #{rails_version}" 14 | end 15 | unless exists 16 | prepend "config.load_defaults #{rails_version}" 17 | end 18 | end 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /lib/minitest/assert_empty.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'minitest', 'assert_empty' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | Use `assert_empty` if expecting object to be empty. 8 | 9 | ```ruby 10 | assert(object.empty?) 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | assert_empty(object) 17 | ``` 18 | EOS 19 | 20 | within_files Synvert::RAILS_MINITEST_FILES do 21 | find_node '.send[receiver=nil][message=assert][arguments.size=1] 22 | [arguments.first=.send[message=empty?][arguments.size=0]]' do 23 | group do 24 | replace :message, with: 'assert_empty' 25 | replace :arguments, with: '{{arguments.first.receiver}}' 26 | end 27 | end 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /lib/minitest/assert_equal_arguments_order.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'minitest', 'assert_equal_arguments_order' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | `assert_equal` should always have expected value as first argument because if the assertion fails the error message would say expected "rubocop-minitest" received "rubocop" not the other way around. 8 | 9 | ```ruby 10 | assert_equal(actual, "rubocop-minitest") 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | assert_equal("rubocop-minitest", actual) 17 | ``` 18 | EOS 19 | 20 | within_files Synvert::RAILS_MINITEST_FILES do 21 | find_node ".send[receiver=nil][message=assert_equal][arguments.size=2] 22 | [arguments.1.type IN (str sym int float true false)]" do 23 | replace :arguments, with: '{{arguments.1}}, {{arguments.first}}' 24 | end 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /lib/minitest/assert_includes.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'minitest', 'assert_includes' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | Use `assert_includes` to assert if the object is included in the collection. 8 | 9 | ```ruby 10 | assert(collection.include?(object)) 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | assert_includes(collection, object) 17 | ``` 18 | EOS 19 | 20 | within_files Synvert::RAILS_MINITEST_FILES do 21 | find_node '.send[receiver=nil][message=assert][arguments.size=1] 22 | [arguments.first=.send[message=include?][arguments.size=1]]' do 23 | group do 24 | replace :message, with: 'assert_includes' 25 | replace :arguments, with: '{{arguments.first.receiver}}, {{arguments.first.arguments.first}}' 26 | end 27 | end 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /lib/minitest/assert_instance_of.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'minitest', 'assert_instance_of' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | Prefer `assert_instance_of(class, object)` over `assert(object.instance_of?(class))`. 8 | 9 | ```ruby 10 | assert('rubocop-minitest'.instance_of?(String)) 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | assert_instance_of(String, 'rubocop-minitest') 17 | ``` 18 | EOS 19 | 20 | within_files Synvert::RAILS_MINITEST_FILES do 21 | find_node '.send[receiver=nil][message=assert][arguments.size=1] 22 | [arguments.first=.send[message=instance_of?][arguments.size=1]]' do 23 | group do 24 | replace :message, with: 'assert_instance_of' 25 | replace :arguments, with: '{{arguments.first.arguments.first}}, {{arguments.first.receiver}}' 26 | end 27 | end 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /lib/minitest/assert_kind_of.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'minitest', 'assert_kind_of' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | Prefer `assert_kind_of(class, object)` over `assert(object.kind_of?(class))`. 8 | 9 | ```ruby 10 | assert('rubocop-minitest'.kind_of?(String)) 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | assert_kind_of(String, 'rubocop-minitest') 17 | ``` 18 | EOS 19 | 20 | within_files Synvert::RAILS_MINITEST_FILES do 21 | find_node '.send[receiver=nil][message=assert][arguments.size=1] 22 | [arguments.first=.send[message=kind_of?][arguments.size=1]]' do 23 | group do 24 | replace :message, with: 'assert_kind_of' 25 | replace :arguments, with: '{{arguments.first.arguments.first}}, {{arguments.first.receiver}}' 26 | end 27 | end 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /lib/minitest/assert_match.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'minitest', 'assert_match' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | Use `assert_match` if expecting matcher regex to match actual object. 8 | 9 | ```ruby 10 | assert(pattern.match?(object)) 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | assert_match(pattern, object) 17 | ``` 18 | EOS 19 | 20 | within_files Synvert::RAILS_MINITEST_FILES do 21 | find_node '.send[receiver=nil][message=assert][arguments.size=1] 22 | [arguments.first=.send[message=match?][arguments.size=1]]' do 23 | group do 24 | replace :message, with: 'assert_match' 25 | replace :arguments, with: '{{arguments.first.receiver}}, {{arguments.first.arguments.first}}' 26 | end 27 | end 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /lib/minitest/assert_nil.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'minitest', 'assert_nil' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | Use `assert_nil` if expecting `nil`. 8 | 9 | ```ruby 10 | assert_equal(nil, actual) 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | assert_nil(actual) 17 | ``` 18 | EOS 19 | 20 | within_files Synvert::RAILS_MINITEST_FILES do 21 | find_node '.send[receiver=nil][message=assert_equal][arguments.size=2][arguments.first=nil]' do 22 | group do 23 | replace :message, with: 'assert_nil' 24 | delete 'arguments.first', and_comma: true 25 | end 26 | end 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /lib/minitest/assert_operator.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'minitest', 'assert_operator' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | Use `assert_operator` if comparing expected and actual object using operator. 8 | 9 | ```ruby 10 | assert(expected < actual) 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | assert_operator(expected, :<, actual) 17 | ``` 18 | EOS 19 | 20 | within_files Synvert::RAILS_MINITEST_FILES do 21 | find_node ".send[receiver=nil][message=assert][arguments.size=1] 22 | [arguments.first=.send[message IN (< > <= >=)][arguments.size=1]]" do 23 | group do 24 | replace :message, with: 'assert_operator' 25 | replace :arguments, 26 | with: "{{arguments.first.receiver}}, :#{node.arguments.first.message}, {{arguments.first.arguments.first}}" 27 | end 28 | end 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /lib/minitest/assert_path_exists.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'minitest', 'assert_path_exists' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | Use `assert_path_exists` if expecting path to exist. 8 | 9 | ```ruby 10 | assert(File.exist?(path)) 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | assert_path_exists(path) 17 | ``` 18 | EOS 19 | 20 | within_files Synvert::RAILS_MINITEST_FILES do 21 | find_node '.send[receiver=nil][message=assert][arguments.size=1] 22 | [arguments.first=.send[receiver=File][message=exist?][arguments.size=1]]' do 23 | group do 24 | replace :message, with: 'assert_path_exists' 25 | replace :arguments, with: '{{arguments.first.arguments.first}}' 26 | end 27 | end 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /lib/minitest/assert_respond_to.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'minitest', 'assert_respond_to' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | Use `assert_respond_to` if expecting object to respond to a method. 8 | 9 | ```ruby 10 | assert(object.respond_to?(some_method)) 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | assert_respond_to(object, some_method) 17 | ``` 18 | EOS 19 | 20 | within_files Synvert::RAILS_MINITEST_FILES do 21 | find_node '.send[receiver=nil][message=assert][arguments.size=1] 22 | [arguments.first=.send[message=respond_to?][arguments.size=1]]' do 23 | group do 24 | replace :message, with: 'assert_respond_to' 25 | replace :arguments, with: '{{arguments.first.receiver}}, {{arguments.first.arguments.first}}' 26 | end 27 | end 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /lib/minitest/assert_same.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'minitest', 'assert_same' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | Use `assert_same` instead of `assert` with `equal?`. 8 | 9 | ```ruby 10 | assert(expected.equal?(actual)) 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | assert_same(expected, actual) 17 | ``` 18 | EOS 19 | 20 | within_files Synvert::RAILS_MINITEST_FILES do 21 | find_node '.send[receiver=nil][message=assert][arguments.size=1] 22 | [arguments.0=.send[message=equal?][arguments.size=1] 23 | [arguments.0=.send[receiver=nil][arguments.size=0]]]' do 24 | group do 25 | replace :arguments, with: '{{arguments.0.receiver}}, {{arguments.0.arguments.0}}' 26 | replace :message, with: 'assert_same' 27 | end 28 | end 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /lib/minitest/assert_silent.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'minitest', 'assert_silent' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | Use `assert_silent` to assert that nothing was written to stdout and stderr. 8 | 9 | ```ruby 10 | assert_output('', '') { puts object.do_something } 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | assert_silent { puts object.do_something } 17 | ``` 18 | EOS 19 | 20 | within_files Synvert::RAILS_MINITEST_FILES do 21 | find_node ".send[receiver=nil][message=assert_output][arguments.size=2][arguments.first=''][arguments.last='']" do 22 | group do 23 | replace :message, with: 'assert_silent' 24 | delete :arguments, :parentheses 25 | end 26 | end 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /lib/minitest/assert_truthy.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'minitest', 'assert_truthy' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | Use `assert` if expecting truthy value. 8 | 9 | ```ruby 10 | assert_equal(true, actual) 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | assert(actual) 17 | ``` 18 | EOS 19 | 20 | within_files Synvert::RAILS_MINITEST_FILES do 21 | find_node '.send[receiver=nil][message=assert_equal][arguments.size=2][arguments.first=true]' do 22 | group do 23 | replace :message, with: 'assert' 24 | delete 'arguments.first', and_comma: true 25 | end 26 | end 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /lib/minitest/hooks_super.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'minitest', 'hooks_super' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | If using a module containing `setup` or `teardown` methods, be sure to call `super` in the test class `setup` or `teardown`. 8 | 9 | ```ruby 10 | class TestMeme < Minitest::Test 11 | include MyHelper 12 | 13 | def setup 14 | do_something 15 | end 16 | 17 | def teardown 18 | clean_something 19 | end 20 | end 21 | ``` 22 | 23 | => 24 | 25 | ```ruby 26 | class TestMeme < Minitest::Test 27 | include MyHelper 28 | 29 | def setup 30 | super 31 | do_something 32 | end 33 | 34 | def teardown 35 | clean_something 36 | super 37 | end 38 | end 39 | ``` 40 | EOS 41 | 42 | within_files Synvert::RAILS_MINITEST_FILES do 43 | find_node '.class[parent_class=Minitest::Test] .def[name=setup]:not_has(> .super)' do 44 | prepend 'super' 45 | end 46 | 47 | find_node '.class[parent_class=Minitest::Test] .def[name=teardown]:not_has(> .super)' do 48 | append 'super' 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /lib/minitest/refute_empty.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'minitest', 'refute_empty' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | Use `refute_empty` if expecting object to be not empty. 8 | 9 | ```ruby 10 | refute(object.empty?) 11 | assert(!object.empty?) 12 | ``` 13 | 14 | => 15 | 16 | ```ruby 17 | refute_empty(object) 18 | refute_empty(object) 19 | ``` 20 | EOS 21 | 22 | within_files Synvert::RAILS_MINITEST_FILES do 23 | # refute(object.empty?) 24 | # => 25 | # refute_empty(object) 26 | find_node '.send[receiver=nil][message=refute][arguments.size=1] 27 | [arguments.first=.send[message=empty?][arguments.size=0]]' do 28 | group do 29 | replace :message, with: 'refute_empty' 30 | replace :arguments, with: '{{arguments.first.receiver}}' 31 | end 32 | end 33 | 34 | # assert(!object.empty?) 35 | # => 36 | # refute_empty(object) 37 | find_node '.send[receiver=nil][message=assert][arguments.size=1] 38 | [arguments.first=.send[message=!][receiver=.send[message=empty?][arguments.size=0]]]' do 39 | group do 40 | replace :message, with: 'refute_empty' 41 | replace :arguments, with: '{{arguments.first.receiver.receiver}}' 42 | end 43 | end 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /lib/minitest/refute_false.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'minitest', 'refute_false' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | Use `refute` if expecting false. 8 | 9 | ```ruby 10 | assert_equal(false, actual) 11 | assert(!something) 12 | ``` 13 | 14 | => 15 | 16 | ```ruby 17 | refute(actual) 18 | refute(something) 19 | ``` 20 | EOS 21 | 22 | within_files Synvert::RAILS_MINITEST_FILES do 23 | # assert_equal(false, actual) 24 | # => 25 | # refute(actual) 26 | find_node '.send[receiver=nil][message=assert_equal][arguments.size=2][arguments.first=false]' do 27 | group do 28 | replace :message, with: 'refute' 29 | delete 'arguments.first', and_comma: true 30 | end 31 | end 32 | 33 | # assert(!something) 34 | # => 35 | # refute(something) 36 | find_node '.send[receiver=nil][message=assert][arguments.size=1][arguments.first=.send[receiver=.send[receiver=nil]][message=!]]' do 37 | group do 38 | replace :message, with: 'refute' 39 | replace :arguments, with: '{{arguments.first.receiver}}' 40 | end 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /lib/minitest/refute_nil.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'minitest', 'refute_nil' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | Use `refute_nil` if not expecting `nil`. 8 | 9 | ```ruby 10 | assert(!actual.nil?) 11 | refute(actual.nil?) 12 | ``` 13 | 14 | => 15 | 16 | ```ruby 17 | refute_nil(actual) 18 | refute_nil(actual) 19 | ``` 20 | EOS 21 | 22 | within_files Synvert::RAILS_MINITEST_FILES do 23 | # assert(!actual.nil?) 24 | # => 25 | # refute_nil(actual) 26 | find_node '.send[receiver=nil][message=assert][arguments.size=1] 27 | [arguments.first=.send[message=!][receiver=.send[message=nil?]]]' do 28 | group do 29 | replace :message, with: 'refute_nil' 30 | replace :arguments, with: '{{arguments.first.receiver.receiver}}' 31 | end 32 | end 33 | 34 | # refute(actual.nil?) 35 | # => 36 | # refute_nil(actual) 37 | find_node '.send[receiver=nil][message=refute][arguments.size=1] [arguments.first=.send[message=nil?]]' do 38 | group do 39 | replace :message, with: 'refute_nil' 40 | replace :arguments, with: '{{arguments.first.receiver}}' 41 | end 42 | end 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /lib/rails/active_record_association_call_use_keyword_arguments.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'active_record_association_call_use_keyword_arguments' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It converts active_record association call to use keyword arguments 8 | 9 | ```ruby 10 | has_many :comments, { :dependent => :destroy } 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | has_many :comments, :dependent => :destroy 17 | ``` 18 | EOS 19 | 20 | if_ruby '2.7' 21 | if_gem 'rails', '>= 5.2' 22 | 23 | association_call_methods = %i[belongs_to has_one has_many has_and_belongs_to_many] 24 | 25 | within_files Synvert::RAILS_MODEL_FILES do 26 | with_node node_type: 'call_node', 27 | receiver: nil, 28 | message: { in: association_call_methods }, 29 | arguments: { node_type: 'arguments_node', arguments: { size: 2, last: { node_type: 'hash_node' } } } do 30 | replace 'arguments.arguments.-1', with: '{{arguments.arguments.-1.elements}}' 31 | end 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /lib/rails/add_active_record_migration_rails_version.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'add_active_record_migration_rails_version' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It adds default ActiveRecord::Migration rails version. 8 | 9 | ```ruby 10 | class CreateUsers < ActiveRecord::Migration 11 | end 12 | ``` 13 | 14 | => 15 | 16 | ```ruby 17 | class CreateUsers < ActiveRecord::Migration[4.2] 18 | end 19 | ``` 20 | EOS 21 | 22 | if_gem 'rails', '>= 5.0' 23 | 24 | within_files Synvert::RAILS_MIGRATION_FILES do 25 | with_node node_type: 'class_node', superclass: 'ActiveRecord::Migration' do 26 | replace :superclass, with: '{{superclass}}[4.2]' 27 | end 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /lib/rails/add_application_job.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'add_application_job' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It adds ApplicationJob 8 | 9 | 1. it adds app/models/application_job.rb file. 10 | 11 | 2. it replaces ActiveJob::Base with ApplicationJob in job files. 12 | 13 | ```ruby 14 | class PostJob < ActiveJob::Base 15 | end 16 | ``` 17 | 18 | => 19 | 20 | ```ruby 21 | class PostJob < ApplicationJob 22 | end 23 | ``` 24 | EOS 25 | 26 | if_gem 'activejob', '>= 5.0' 27 | 28 | # adds file app/jobs/application_job.rb 29 | add_file 'app/jobs/application_job.rb', <<~EOS 30 | class ApplicationJob < ActiveJob::Base 31 | end 32 | EOS 33 | 34 | within_files Synvert::RAILS_JOB_FILES do 35 | # class PostJob < ActiveJob::Base 36 | # end 37 | # => 38 | # class PostJob < ApplicationJob 39 | # end 40 | with_node node_type: 'class_node', name: { not: 'ApplicationJob' }, superclass: 'ActiveJob::Base' do 41 | replace :superclass, with: 'ApplicationJob' 42 | end 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /lib/rails/add_application_mailer.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'add_application_mailer' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It adds ApplicationMailer 8 | 9 | 1. it adds app/mailers/application_mailer.rb file. 10 | 11 | 2. it replaces ActionMailer::Base with ApplicationMailer in mailer files. 12 | 13 | ```ruby 14 | class UserMailer < ActionMailer::Base 15 | end 16 | ``` 17 | 18 | => 19 | 20 | ```ruby 21 | class UserMailer < ApplicationMailer 22 | end 23 | ``` 24 | EOS 25 | 26 | if_gem 'actionmailer', '>= 5.0' 27 | 28 | # adds file app/mailers/application_mailer.rb 29 | add_file 'app/mailers/application_mailer.rb', <<~EOS 30 | class ApplicationMailer < ActionMailer::Base 31 | end 32 | EOS 33 | 34 | within_files Synvert::RAILS_MAILER_FILES do 35 | # class UserMailer < ActionMailer::Base 36 | # end 37 | # => 38 | # class UserMailer < ApplicationMailer 39 | # end 40 | with_node node_type: 'class_node', name: { not: 'ApplicationMailer' }, superclass: 'ActionMailer::Base' do 41 | replace :superclass, with: 'ApplicationMailer' 42 | end 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /lib/rails/add_application_record.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'add_application_record' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It adds ApplicationRecord 8 | 9 | 1. it adds app/models/application_record.rb file. 10 | 2. it replaces ActiveRecord::Base with ApplicationRecord in model files. 11 | 12 | ```ruby 13 | class Post < ActiveRecord::Base 14 | end 15 | ``` 16 | 17 | => 18 | 19 | ```ruby 20 | class Post < ApplicationRecord 21 | end 22 | ``` 23 | EOS 24 | 25 | if_gem 'activerecord', '>= 5.0' 26 | 27 | # adds file app/models/application_record.rb 28 | add_file 'app/models/application_record.rb', <<~EOS 29 | class ApplicationRecord < ActiveRecord::Base 30 | self.abstract_class = true 31 | end 32 | EOS 33 | 34 | within_files Synvert::RAILS_MODEL_FILES do 35 | # class Post < ActiveRecord::Base 36 | # end 37 | # => 38 | # class Post < ApplicationRecord 39 | # end 40 | with_node node_type: 'class_node', name: { not: 'ApplicationRecord' }, superclass: 'ActiveRecord::Base' do 41 | replace :superclass, with: 'ApplicationRecord' 42 | end 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /lib/rails/application_secrets_use_symbol_keys.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'application_secrets_use_symbol_keys' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | If your application stores nested configuration in `config/secrets.yml`, all keys are now loaded as symbols, so access using strings should be changed. 8 | 9 | From: 10 | 11 | ```ruby 12 | Rails.application.secrets[:smtp_settings]["address"] 13 | ``` 14 | 15 | To: 16 | 17 | ```ruby 18 | Rails.application.secrets[:smtp_settings][:address] 19 | ``` 20 | EOS 21 | 22 | if_gem 'rails', '>= 5.1' 23 | 24 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 25 | # Rails.appplication.config.secrets[:smtp_settings]["address"] 26 | # => 27 | # Rails.appplication.config.secrets[:smtp_settings][:address] 28 | with_node node_type: 'call_node', 29 | receiver: /^Rails.application.config.secrets/, 30 | name: '[]', 31 | arguments: { node_type: 'arguments_node', arguments: { size: 1, first: { node_type: 'string_node' } } } do 32 | replace 'arguments.arguments.first', with: '{{arguments.arguments.first.to_symbol}}' 33 | end 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /lib/rails/convert_configs_2_3_to_3_0.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'convert_configs_2_3_to_3_0' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It converts rails configs from 2.3 to 3.0. 8 | 9 | It removes `filter_parameter_logging :password` from `app/controllers/application_controller.rb`, 10 | and adds `config.filter_parameters += [:password]` in `config/application.rb`. 11 | EOS 12 | 13 | if_gem 'rails', '~> 3.0.0' 14 | 15 | filter_parameters = [] 16 | within_file 'app/controllers/application_controller.rb' do 17 | with_node node_type: 'call_node', name: 'filter_parameter_logging' do 18 | filter_parameters = node.arguments.arguments.map(&:to_source) 19 | remove 20 | end 21 | end 22 | 23 | if filter_parameters.present? 24 | within_file 'config/application.rb' do 25 | with_node node_type: 'class_node', superclass: 'Rails::Application' do 26 | unless_exist_node node_type: 'call_node', receiver: 'config', name: 'filter_parameters' do 27 | append "config.filter_parameters += [#{filter_parameters.join(', ')}]" 28 | end 29 | end 30 | end 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /lib/rails/convert_configs_4_0_to_4_1.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'securerandom' 4 | 5 | Synvert::Rewriter.new 'rails', 'convert_configs_4_0_to_4_1' do 6 | configure(parser: Synvert::PRISM_PARSER) 7 | 8 | description <<~EOS 9 | It converts rails configs from 4.0 to 4.1. 10 | 11 | 1. config/secrets.yml 12 | Create a secrets.yml file in your config folder 13 | Copy the existing secret_key_base from the secret_token.rb initializer to secrets.yml under the production section. 14 | Remove the secret_token.rb initializer 15 | 16 | 2. add config/initializers/cookies_serializer.rb 17 | EOS 18 | 19 | secrets_content = <<~EOS 20 | development: 21 | secret_key_base: #{SecureRandom.hex(64)} 22 | 23 | test: 24 | secret_key_base: #{SecureRandom.hex(64)} 25 | 26 | # Do not keep production secrets in the repository, 27 | # instead read values from the environment. 28 | production: 29 | secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> 30 | EOS 31 | 32 | if_gem 'rails', '~> 4.1.0' 33 | 34 | add_file 'config/secrets.yml', secrets_content 35 | 36 | remove_file 'config/initializers/secret_token.rb' 37 | 38 | add_file 'config/initializers/cookies_serializer.rb', 39 | 'Rails.application.config.action_dispatch.cookies_serializer = :json' 40 | end 41 | -------------------------------------------------------------------------------- /lib/rails/convert_configs_5_0_to_5_1.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'convert_configs_5_0_to_5_1' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It converts rails configs from 5.0 to 5.1. 8 | 9 | It sets `config.load_defaults 5.1` in config/application.rb. 10 | EOS 11 | 12 | if_gem 'rails', '~> 5.1.0' 13 | 14 | call_helper 'rails/set_load_defaults', rails_version: '5.1' 15 | end 16 | -------------------------------------------------------------------------------- /lib/rails/convert_configs_5_1_to_5_2.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'convert_configs_5_1_to_5_2' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It converts rails configs from 5.1 to 5.2 8 | 9 | 1. it sets `config.load_defaults 5.2` in config/application.rb. 10 | 11 | 2. it replaces `dalli_store` with `mem_cache_store` 12 | EOS 13 | 14 | if_gem 'rails', '~> 5.2.0' 15 | 16 | call_helper 'rails/set_load_defaults', rails_version: '5.2' 17 | 18 | within_file 'config/**/*.rb' do 19 | # dalli_store => mem_cache_store 20 | with_node node_type: 'symbol_node', to_value: :dalli_store do 21 | replace_with ':mem_cache_store' 22 | end 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /lib/rails/convert_configs_5_2_to_6_0.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'convert_configs_5_2_to_6_0' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It converts rails configs from 5.2 to 6.0 8 | 9 | It sets `config.load_defaults 6.0` in config/application.rb. 10 | EOS 11 | 12 | if_gem 'rails', '~> 6.0.0' 13 | 14 | call_helper 'rails/set_load_defaults', rails_version: '6.0' 15 | end 16 | -------------------------------------------------------------------------------- /lib/rails/convert_configs_6_0_to_6_1.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'convert_configs_6_0_to_6_1' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It converts rails configs from 6.0 to 6.1 8 | 9 | It sets `config.load_defaults 6.1` in config/application.rb. 10 | EOS 11 | 12 | if_gem 'rails', '~> 6.1.0' 13 | 14 | call_helper 'rails/set_load_defaults', rails_version: '6.1' 15 | end 16 | -------------------------------------------------------------------------------- /lib/rails/convert_configs_6_1_to_7_0.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'convert_configs_6_1_to_7_0' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It converts rails configs from 6.1 to 7.0 8 | 9 | It sets `config.load_defaults 7.0` in config/application.rb. 10 | 11 | It removes `config.autoloader = :classic` in config/application.rb. 12 | EOS 13 | 14 | if_gem 'rails', '~> 7.0.0' 15 | 16 | call_helper 'rails/set_load_defaults', rails_version: '7.0' 17 | 18 | within_files 'config/application.rb' do 19 | with_node node_type: 'call_node', 20 | receiver: { node_type: 'call_node', name: 'config' }, 21 | name: 'autoloader=', 22 | arguments: { node_type: 'arguments_node', arguments: { size: { gt: 0 } } } do 23 | remove 24 | end 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /lib/rails/convert_configs_7_1_to_7_2.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'convert_configs_7_1_to_7_2' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It converts rails configs from 7.1 to 7.2 8 | 9 | 1. it sets `config.load_defaults 7.2` in config/application.rb. 10 | EOS 11 | 12 | if_gem 'rails', '~> 7.2.0' 13 | 14 | call_helper 'rails/set_load_defaults', rails_version: '7.2' 15 | end 16 | -------------------------------------------------------------------------------- /lib/rails/convert_configs_7_2_to_8_0.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'convert_configs_7_2_to_8_0' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It converts rails configs from 7.1 to 8.0 8 | 9 | 1. it sets `config.load_defaults 8.0` in config/application.rb. 10 | EOS 11 | 12 | if_gem 'rails', '~> 8.0.0' 13 | 14 | call_helper 'rails/set_load_defaults', rails_version: '8.0' 15 | end 16 | -------------------------------------------------------------------------------- /lib/rails/convert_constants_3_1_to_3_2.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'convert_constants_3_1_to_3_2' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It converts rails constants from 3.1 to 3.2. 8 | 9 | ```ruby 10 | ActionController::UnknownAction 11 | ActionController::DoubleRenderError 12 | ``` 13 | 14 | => 15 | 16 | ```ruby 17 | AbstractController::ActionNotFound 18 | AbstractController::DoubleRenderError 19 | ``` 20 | EOS 21 | 22 | if_gem 'actionpack', '>= 3.2' 23 | 24 | within_files Synvert::RAILS_CONTROLLER_FILES do 25 | # ActionController::UnknownAction => AbstractController::ActionNotFound 26 | # ActionController::DoubleRenderError => AbstractController::DoubleRenderError 27 | { 28 | 'ActionController::UnknownAction' => 'AbstractController::ActionNotFound', 29 | 'ActionController::DoubleRenderError' => 'AbstractController::DoubleRenderError' 30 | }.each do |old_const, new_const| 31 | with_node node_type: 'constant_path_node', to_source: old_const do 32 | replace_with new_const 33 | end 34 | end 35 | end 36 | end 37 | -------------------------------------------------------------------------------- /lib/rails/convert_constants_4_2_to_5_0.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'convert_constants_4_2_to_5_0' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It converts rails constants from 4.2 to 5.0. 8 | 9 | It replaces `MissingSourceFile` with `LoadError`. 10 | EOS 11 | 12 | if_gem 'rails', '>= 5.0' 13 | 14 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 15 | # MissingSourceFile 16 | # => 17 | # LoadError 18 | with_node node_type: 'constant_read_node', name: 'MissingSourceFile' do 19 | replace_with 'LoadError' 20 | end 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /lib/rails/convert_constants_5_0_to_5_1.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'convert_constants_5_0_to_5_1' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | It converts rails constants from 5.0 to 5.1. 8 | 9 | It replaces `HashWithIndifferentAccess` with `ActiveSupport::HashWithIndifferentAccess`. 10 | EOS 11 | 12 | if_gem 'rails', '>= 5.1' 13 | 14 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 15 | # HashWithIndifferentAccess 16 | # => 17 | # ActiveSupport::HashWithIndifferentAccess 18 | with_node node_type: 'const', to_source: 'HashWithIndifferentAccess' do 19 | replace_with 'ActiveSupport::HashWithIndifferentAccess' 20 | end 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /lib/rails/convert_env_to_request_env.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'convert_env_to_request_env' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It replaces env with request.env in controller files. 8 | 9 | ```ruby 10 | env["omniauth.auth"] 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | request.env["omniauth.auth"] 17 | ``` 18 | EOS 19 | 20 | if_gem 'actionpack', '>= 5.0' 21 | 22 | within_file Synvert::RAILS_CONTROLLER_FILES do 23 | # env["omniauth.auth"] 24 | # => 25 | # request.env["omniauth.auth"] 26 | with_node node_type: 'call_node', receiver: nil, name: 'env', arguments: nil do 27 | replace :message, with: 'request.env' 28 | end 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /lib/rails/convert_model_errors_add.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'convert_model_errors_add' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It converts to activerecord `errors.add`. 8 | 9 | ```ruby 10 | errors[:base] = "author not present" 11 | self.errors[:base] = "author not present" 12 | ``` 13 | 14 | => 15 | 16 | ```ruby 17 | errors.add(:base, "author not present") 18 | self.errors.add(:base, "author not present") 19 | ``` 20 | EOS 21 | 22 | if_gem 'activerecord', '>= 5.0' 23 | 24 | within_files Synvert::RAILS_MODEL_FILES do 25 | with_node node_type: 'call_node', 26 | receiver: { node_type: 'call_node', name: 'errors', arguments: nil }, 27 | name: '[]=', 28 | arguments: { node_type: 'arguments_node', arguments: { size: 2 } } do 29 | replace_with '{{receiver}}.add({{arguments.arguments.0}}, {{arguments.arguments.1}})' 30 | end 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /lib/rails/convert_rails_logger.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'convert_rails_logger' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It converts RAILS_DEFAULT_LOGGER to Rails.logger. 8 | 9 | ```ruby 10 | RAILS_DEFAULT_LOGGER 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | Rails.logger 17 | ``` 18 | EOS 19 | 20 | if_gem 'rails', '>= 2.3' 21 | 22 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 23 | with_node node_type: 'constant_read_node', name: 'RAILS_DEFAULT_LOGGER' do 24 | replace_with 'Rails.logger' 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /lib/rails/convert_render_text_to_render_plain.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'convert_render_text_to_render_plain' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It converts `render :text` to `render :plain` 8 | 9 | ```ruby 10 | render text: 'OK' 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | render plain: 'OK' 17 | ``` 18 | EOS 19 | 20 | if_gem 'actionpack', '>= 5.0' 21 | 22 | within_files Synvert::RAILS_CONTROLLER_FILES do 23 | with_node node_type: 'call_node', 24 | receiver: nil, 25 | name: 'render', 26 | arguments: { 27 | node_type: 'arguments_node', 28 | arguments: { size: 1, first: { node_type: 'keyword_hash_node', text_value: { not: nil } } } 29 | } do 30 | old_key = node.arguments.arguments.first.text_element.key.to_source 31 | replace 'arguments.arguments.0.text_element.key', with: old_key.sub('text', 'plain') 32 | end 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /lib/rails/convert_routes_3_2_to_4_0.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'convert_routes_3_2_to_4_0' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It converts rails routes from 3.2 to 4.0. 8 | 9 | 1. it removes `Rack::Utils.escape` in config/routes.rb. 10 | 11 | ```ruby 12 | Rack::Utils.escape('こんにちは') => 'こんにちは' 13 | ``` 14 | 15 | 2. it replaces match in config/routes.rb. 16 | 17 | ```ruby 18 | match "/" => "root#index" 19 | ``` 20 | 21 | => 22 | 23 | ```ruby 24 | get "/" => "root#index" 25 | ``` 26 | EOS 27 | 28 | if_gem 'rails', '>= 4.0' 29 | 30 | within_file Synvert::RAILS_ROUTE_FILES do 31 | # Rack::Utils.escape('こんにちは') => 'こんにちは' 32 | with_node node_type: 'call_node', receiver: 'Rack::Utils', name: 'escape' do 33 | replace_with '{{arguments.arguments}}' 34 | end 35 | 36 | # match "/" => "root#index" => get "/" => "root#index" 37 | with_node node_type: 'call_node', name: 'match' do 38 | replace :message, with: 'get' 39 | end 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /lib/rails/convert_to_response_parsed_body.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'convert_to_response_parsed_body' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It converts `JSON.parse(@response.body)` to `@response.parsed_body`. 8 | EOS 9 | 10 | if_gem 'rails', '>= 5.0' 11 | 12 | within_files Synvert::RAILS_CONTROLLER_TEST_FILES do 13 | with_node node_type: 'call_node', 14 | receiver: 'JSON', 15 | name: 'parse', 16 | arguments: { 17 | node_type: 'arguments_node', 18 | arguments: { 19 | size: 1, 20 | first: { 21 | node_type: 'call_node', 22 | receiver: { in: ['response', '@response'] }, 23 | message: 'body', 24 | arguments: nil 25 | } 26 | } 27 | } do 28 | replace_with '{{arguments.arguments.0.receiver}}.parsed_body' 29 | end 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /lib/rails/convert_update_attributes_to_update.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'convert_update_attributes_to_update' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It converts `.update_attributes` to `.update` 8 | 9 | ```ruby 10 | user.update_attributes(title: 'new') 11 | user.update_attributes!(title: 'new') 12 | ``` 13 | 14 | => 15 | 16 | ```ruby 17 | user.update(title: 'new') 18 | user.update!(title: 'new') 19 | ``` 20 | EOS 21 | 22 | if_gem 'activerecord', '>= 6.0' 23 | 24 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 25 | # user.update_attributes(title: 'new') 26 | # => 27 | # user.update(title: 'new') 28 | with_node node_type: 'call_node', name: 'update_attributes' do 29 | replace :message, with: 'update' 30 | end 31 | 32 | # user.update_attributes!(title: 'new') 33 | # => 34 | # user.update!(title: 'new') 35 | with_node node_type: 'call_node', name: 'update_attributes!' do 36 | replace :message, with: 'update!' 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /lib/rails/convert_views_2_3_to_3_0.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'convert_views_2_3_to_3_0' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | 1. remove `h` helper, rails 3 uses it by default. 8 | 9 | ```erb 10 | <%= h user.login %> 11 | ``` 12 | 13 | => 14 | 15 | ```erb 16 | <%= user.login %> 17 | ``` 18 | 19 | 2. use erb expression instead of erb statement for view helpers. 20 | 21 | ```erb 22 | <% form_for post do |f| %> 23 | <% end %> 24 | ``` 25 | 26 | => 27 | 28 | ```erb 29 | <%= form_for post do |f| %> 30 | <% end %> 31 | ``` 32 | EOS 33 | 34 | if_gem 'actionview', '>= 3.0' 35 | 36 | within_files Synvert::RAILS_VIEW_FILES + Synvert::RAILS_HELPER_FILES do 37 | # <%= h user.login %> => <%= user.login %> 38 | with_node node_type: 'call_node', receiver: nil, name: 'h' do 39 | replace_with '{{arguments}}' 40 | end 41 | 42 | # <% form_for post do |f| %> 43 | # <% end %> 44 | # => 45 | # <%= form_for post do |f| %> 46 | # <% end %> 47 | with_node node_type: 'call_node', 48 | receiver: nil, 49 | name: { in: %w[form_for form_tag fields_for div_for content_tag_for] }, 50 | block: { node_type: 'block_node' } do 51 | replace_erb_stmt_with_expr 52 | end 53 | end 54 | end 55 | -------------------------------------------------------------------------------- /lib/rails/deprecate_active_record_migration_check_pending.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'deprecate_active_record_migration_check_pending' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It removes `ActiveRecord::Migration.check_pending!` in `test/test_helper.rb` 8 | EOS 9 | 10 | if_gem 'rails', '>= 4.1' 11 | 12 | within_file 'test/test_helper.rb' do 13 | # ActiveRecord::Migration.check_pending! => require 'test_help' 14 | with_node node_type: 'call_node', receiver: 'ActiveRecord::Migration', name: 'check_pending!' do 15 | remove 16 | end 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /lib/rails/deprecate_multi_json.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'deprecate_multi_json' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It replaces multi_json with json. 8 | 9 | 1. replace `MultiJson.dump` with `obj.to_json` 10 | 11 | 2. replace `MultiJson.load` with `JSON.parse(str)` 12 | EOS 13 | 14 | if_gem 'rails', '>= 4.1' 15 | 16 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 17 | # MultiJson.dump(obj) => obj.to_json 18 | with_node node_type: 'call_node', receiver: 'MultiJson', name: 'dump' do 19 | replace_with '{{arguments.arguments}}.to_json' 20 | end 21 | 22 | # MultiJson.load(str) => JSON.parse(str) 23 | with_node node_type: 'call_node', receiver: 'MultiJson', name: 'load' do 24 | replace_with 'JSON.parse {{arguments.arguments}}' 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /lib/rails/explicitly-render-with-formats.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'explicitly-render-with-formats' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It calls render with formats explicitly. 8 | 9 | ```ruby 10 | render template: 'index.json' 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | render template: 'index', foramts: [:json] 17 | ``` 18 | EOS 19 | 20 | if_gem 'active_support', '>= 7.0' 21 | 22 | within_files Synvert::RAILS_CONTROLLER_FILES do 23 | find_node ".call_node[receiver=nil][name=render][arguments=.arguments_node[arguments.size=1][arguments.0=.keyword_hash_node]]" do 24 | template_value = node.arguments.arguments.first.template_value.to_value 25 | if template_value&.split('.')&.size == 2 26 | replace 'arguments.arguments.0.template_value', with: "'#{template_value.split('.').first}'" 27 | insert "formats: [:#{template_value.split('.').last}]", at: 'end', to: 'arguments.arguments.0', and_comma: true 28 | end 29 | end 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /lib/rails/prefer_nor_conditions.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'prefer_nor_conditions' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | Prefer NOR conditions 8 | 9 | ```ruby 10 | where.not(first_name: nil, last_name: nil) 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | where.not(first_name: nil).where.not(last_name: nil) 17 | ``` 18 | EOS 19 | 20 | if_gem 'rails', '>= 6.0' 21 | 22 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 23 | with_node node_type: 'call_node', 24 | receiver: { node_type: 'call_node', name: 'where', arguments: nil }, 25 | name: 'not', 26 | arguments: { node_type: 'arguments_node', arguments: { size: 1, first: { node_type: 'keyword_hash_node', elements: { length: { gt: 1 } } } } } do 27 | new_source = node.arguments.arguments.first.elements.map { |element| "where.not(#{element.to_source})" } 28 | .join('.') 29 | replace 'receiver.message', :closing, with: new_source 30 | end 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /lib/rails/remove_active_support_dependencies_private_api.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'remove_active_support_dependencies_private_api' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It removes active support dependencies private api. 8 | 9 | ```ruby 10 | ActiveSupport::Dependencies.constantize("User") 11 | ActiveSupport::Dependencies.safe_constantize("User") 12 | ``` 13 | 14 | => 15 | 16 | ```ruby 17 | "User".constantize 18 | "User".safe_constantize 19 | ``` 20 | EOS 21 | 22 | if_gem 'active_support', '>= 7.0' 23 | 24 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 25 | with_node node_type: 'call_node', 26 | receiver: 'ActiveSupport::Dependencies', 27 | name: { in: ['constantize', 'safe_constantize'] }, 28 | arguments: { node_type: 'arguments_node', arguments: { size: 1 } } do 29 | replace_with '{{arguments.arguments.0}}.{{message}}' 30 | end 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /lib/rails/rename_errors_keys_to_attribute_names.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'rename_errors_keys_to_attribute_names' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It renames `errors#keys` to `erros#attribute_names` 8 | EOS 9 | 10 | if_gem 'rails', '>= 6.1' 11 | 12 | within_files Synvert::RAILS_MODEL_FILES do 13 | with_node node_type: 'call_node', 14 | receiver: { node_type: 'call_node', name: 'errors', arguments: nil }, 15 | name: 'keys', 16 | arguments: nil do 17 | replace :message, with: 'attribute_names' 18 | end 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /lib/rails/test_request_methods_use_keyword_arguments.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'test_request_methods_use_keyword_arguments' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It converts rails test request methods to use keyword arguments 8 | 9 | functional test: 10 | 11 | ```ruby 12 | get :show, options 13 | ``` 14 | 15 | => 16 | 17 | ```ruby 18 | get :show, **options 19 | ``` 20 | 21 | integration test: 22 | 23 | ```ruby 24 | get '/posts/1', options 25 | ``` 26 | 27 | => 28 | 29 | ```ruby 30 | get '/posts/1', **options 31 | ``` 32 | EOS 33 | 34 | if_ruby '2.7' 35 | if_gem 'rails', '>= 5.2' 36 | 37 | request_methods = %i[get post put patch delete] 38 | 39 | # get :show, options 40 | # => 41 | # get :show, **options 42 | within_files Synvert::RAILS_CONTROLLER_TEST_FILES + Synvert::RAILS_INTEGRATION_TEST_FILES do 43 | with_node node_type: 'call_node', 44 | name: { in: request_methods }, 45 | arguments: { node_type: 'arguments_node', arguments: { size: 2, '-1': { node_type: { in: %w[local_variable_read_node instance_variable_read_node call_node] } } } } do 46 | insert '**', to: 'arguments.arguments.-1', at: 'beginning' 47 | end 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /lib/rails/upgrade_2_3_to_3_0.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'upgrade_2_3_to_3_0' do 4 | description 'It upgrades rails from 2.3 to 3.0.' 5 | 6 | add_snippet 'rails', 'convert_configs_2_3_to_3_0' 7 | add_snippet 'rails', 'convert_dynamic_finders_for_rails_3' 8 | add_snippet 'rails', 'convert_mailers_2_3_to_3_0' 9 | add_snippet 'rails', 'convert_models_2_3_to_3_0' 10 | add_snippet 'rails', 'convert_rails_env' 11 | add_snippet 'rails', 'convert_rails_root' 12 | add_snippet 'rails', 'convert_rails_logger' 13 | add_snippet 'rails', 'convert_routes_2_3_to_3_0' 14 | add_snippet 'rails', 'convert_views_2_3_to_3_0' 15 | end 16 | -------------------------------------------------------------------------------- /lib/rails/upgrade_3_0_to_3_1.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'upgrade_3_0_to_3_1' do 4 | description 'It upgrades rails from 3.0 to 3.1.' 5 | 6 | add_snippet 'rails', 'convert_configs_3_0_to_3_1' 7 | add_snippet 'rails', 'use_migrations_instance_methods' 8 | end 9 | -------------------------------------------------------------------------------- /lib/rails/upgrade_3_1_to_3_2.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'upgrade_3_1_to_3_2' do 4 | description 'It upgrades rails from 3.1 to 3.2.' 5 | 6 | add_snippet 'rails', 'convert_configs_3_1_to_3_2' 7 | add_snippet 'rails', 'convert_constants_3_1_to_3_2' 8 | add_snippet 'rails', 'fix_model_3_2_deprecations' 9 | 10 | within_files 'vendor/plugins' do 11 | warn 'Rails::Plugin is deprecated and will be removed in Rails 4.0. Instead of adding plugins to vendor/plugins use gems or bundler with path or git dependencies.' 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /lib/rails/upgrade_3_2_to_4_0.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'upgrade_3_2_to_4_0' do 4 | description 'It upgrades rails from 3.2 to 4.0.' 5 | 6 | add_snippet 'rails', 'convert_configs_3_2_to_4_0' 7 | add_snippet 'rails', 'convert_constants_3_2_to_4_0' 8 | add_snippet 'rails', 'convert_models_3_2_to_4_0' 9 | add_snippet 'rails', 'convert_routes_3_2_to_4_0' 10 | add_snippet 'rails', 'convert_views_3_2_to_4_0' 11 | add_snippet 'rails', 'convert_dynamic_finders_for_rails_4' 12 | add_snippet 'rails', 'strong_parameters' 13 | add_snippet 'rails', 'convert_controller_filter_to_action' 14 | add_snippet 'rails', 'convert_model_lambda_scope' 15 | end 16 | -------------------------------------------------------------------------------- /lib/rails/upgrade_4_0_to_4_1.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'upgrade_4_0_to_4_1' do 4 | description <<~EOS 5 | It upgrades rails from 4.0 to 4.1. 6 | 7 | Warn return within inline callback blocks `before_save { return false }` 8 | EOS 9 | 10 | add_snippet 'rails', 'convert_configs_4_0_to_4_1' 11 | add_snippet 'rails', 'deprecate_active_record_migration_check_pending' 12 | add_snippet 'rails', 'deprecate_multi_json' 13 | end 14 | -------------------------------------------------------------------------------- /lib/rails/upgrade_4_1_to_4_2.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'upgrade_4_1_to_4_2' do 4 | description 'It upgrades rails from 4.1 to 4.2.' 5 | 6 | add_snippet 'rails', 'convert_configs_4_1_to_4_2' 7 | end 8 | -------------------------------------------------------------------------------- /lib/rails/upgrade_4_2_to_5_0.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'upgrade_4_2_to_5_0' do 4 | description 'It upgrades rails 4.2 to 5.0.' 5 | 6 | add_snippet 'rails', 'add_active_record_migration_rails_version' 7 | add_snippet 'rails', 'convert_configs_4_2_to_5_0' 8 | add_snippet 'rails', 'convert_constants_4_2_to_5_0' 9 | add_snippet 'rails', 'convert_env_to_request_env' 10 | add_snippet 'rails', 'convert_head_response' 11 | add_snippet 'rails', 'convert_render_text_to_render_plain' 12 | add_snippet 'rails', 'convert_test_request_methods_4_2_to_5_0' 13 | add_snippet 'rails', 'convert_to_response_parsed_body' 14 | add_snippet 'rails', 'add_application_record' 15 | add_snippet 'rails', 'add_application_job' 16 | add_snippet 'rails', 'add_application_mailer' 17 | add_snippet 'rails', 'convert_after_commit' 18 | add_snippet 'rails', 'convert_model_errors_add' 19 | end 20 | -------------------------------------------------------------------------------- /lib/rails/upgrade_5_0_to_5_1.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'upgrade_5_0_to_5_1' do 4 | description <<~EOS 5 | It upgrades rails 5.0 to 5.1. 6 | 7 | 1. it replaces `HashWithIndifferentAccess` with `ActiveSupport::HashWithIndifferentAccess`. 8 | 9 | 2. it replaces `Rails.application.config.secrets[:smtp_settings]["address"]` with 10 | `Rails.application.config.secrets[:smtp_settings][:address]` 11 | EOS 12 | 13 | add_snippet 'rails', 'application_secrets_use_symbol_keys' 14 | add_snippet 'rails', 'convert_active_record_dirty_5_0_to_5_1' 15 | add_snippet 'rails', 'convert_constants_5_0_to_5_1' 16 | 17 | call_helper 'rails/set_load_defaults', rails_version: '5.1' 18 | end 19 | -------------------------------------------------------------------------------- /lib/rails/upgrade_5_1_to_5_2.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'upgrade_5_1_to_5_2' do 4 | description 'It upgrades rails 5.1 to 5.2.' 5 | 6 | add_snippet 'rails', 'convert_configs_5_1_to_5_2' 7 | add_snippet 'rails', 'active_record_association_call_use_keyword_arguments' 8 | add_snippet 'rails', 'test_request_methods_use_keyword_arguments' 9 | end 10 | -------------------------------------------------------------------------------- /lib/rails/upgrade_5_2_to_6_0.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'upgrade_5_2_to_6_0' do 4 | description 'It upgrades rails 5.2 to 6.0.' 5 | 6 | add_snippet 'rails', 'convert_configs_5_2_to_6_0' 7 | add_snippet 'rails', 'convert_update_attributes_to_update' 8 | add_snippet 'rails', 'prefer_nor_conditions' 9 | end 10 | -------------------------------------------------------------------------------- /lib/rails/upgrade_6_0_to_6_1.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'upgrade_6_0_to_6_1' do 4 | description 'It upgrades rails 6.0 to 6.1.' 5 | 6 | add_snippet 'rails', 'convert_configs_6_0_to_6_1' 7 | add_snippet 'rails', 'convert_update_attributes_to_update' 8 | add_snippet 'rails', 'deprecate_errors_as_hash' 9 | add_snippet 'rails', 'rename_errors_keys_to_attribute_names' 10 | add_snippet 'rails', 'use_active_storage_image_processing_macros' 11 | end 12 | -------------------------------------------------------------------------------- /lib/rails/upgrade_6_1_to_7_0.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'upgrade_6_1_to_7_0' do 4 | description 'It upgrades rails 6.1 to 7.0.' 5 | 6 | add_snippet 'rails', 'convert_configs_6_1_to_7_0' 7 | add_snippet 'rails', 'deprecate_errors_as_hash' 8 | add_snippet 'rails', 'remove_active_support_dependencies_private_api' 9 | add_snippet 'rails', 'update_active_storage_variant_argument' 10 | add_snippet 'rails', 'explicitly-render-with-formats' 11 | end 12 | -------------------------------------------------------------------------------- /lib/rails/upgrade_7_0_to_7_1.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'upgrade_7_0_to_7_1' do 4 | description 'It upgrades rails 7.0 to 7.1.' 5 | 6 | add_snippet 'rails', 'convert_configs_7_0_to_7_1' 7 | add_snippet 'rails', 'new_enum_syntax' 8 | end 9 | -------------------------------------------------------------------------------- /lib/rails/upgrade_7_1_to_7_2.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'upgrade_7_1_to_7_2' do 4 | description 'It upgrades rails 7.1 to 7.2.' 5 | 6 | add_snippet 'rails', 'convert_configs_7_1_to_7_2' 7 | add_snippet 'rails', 'new_enum_syntax' 8 | end 9 | -------------------------------------------------------------------------------- /lib/rails/upgrade_7_2_to_8_0.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'upgrade_7_2_to_8_0' do 4 | description 'It upgrades rails 7.2 to 8.0.' 5 | 6 | add_snippet 'rails', 'convert_configs_7_2_to_8_0' 7 | add_snippet 'rails', 'new_enum_syntax' 8 | end 9 | -------------------------------------------------------------------------------- /lib/rails/use_migrations_instance_methods.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails', 'use_migrations_instance_methods' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It uses instance methods rather than class methods in migrations. 8 | 9 | ```ruby 10 | def self.up 11 | end 12 | 13 | def self.down 14 | end 15 | ``` 16 | 17 | => 18 | 19 | ```ruby 20 | def up 21 | end 22 | 23 | def down 24 | end 25 | ``` 26 | EOS 27 | 28 | if_gem 'activerecord', '>= 3.1' 29 | 30 | within_files Synvert::RAILS_MIGRATION_FILES do 31 | # def self.up => def up 32 | # def self.down => def down 33 | with_node node_type: 'def_node', name: { in: ['up', 'down'] }, receiver: { node_type: 'self_node' } do 34 | delete :receiver, :operator 35 | end 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /lib/rails_best_practices/use_scope_access.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rails_best_practices', 'use_scope_access' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It converts Foo to Bar 8 | 9 | ```ruby 10 | Foo 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | Bar 17 | ``` 18 | EOS 19 | 20 | within_files '**/*.rb' do 21 | with_node type: 'const', to_source: 'Foo' do 22 | replace_with 'Bar' 23 | end 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /lib/redis/deprecate_calling_redis_inside_multi.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'redis', 'deprecate_calling_redis_inside_multi' do 4 | configure(parser: Synvert::SYNTAX_TREE_PARSER) 5 | 6 | description <<~EOS 7 | Deprecate calling commands on `Redis` insdie `Redis#multi`. 8 | 9 | ```ruby 10 | redis.multi do 11 | redis.get("key") 12 | end 13 | ``` 14 | 15 | => 16 | 17 | ```ruby 18 | redis.multi do |transaction| 19 | transaction.get("key") 20 | end 21 | ``` 22 | EOS 23 | 24 | if_gem 'redis', '>= 4.6.0' 25 | 26 | within_files Synvert::ALL_RUBY_FILES do 27 | find_node '.MethodAddBlock[call=.CallNode[message=multi]][block=.BlockNode[block_var=nil]]' do 28 | redis_name = node.call.receiver.to_source 29 | group do 30 | insert ' |transaction|', to: 'block.opening', at: 'end' 31 | goto_node 'block.bodystmt' do 32 | find_node ".CallNode[receiver=#{redis_name}]" do 33 | replace :receiver, with: 'transaction' 34 | end 35 | end 36 | end 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /lib/redis/deprecate_calling_redis_inside_pipelined.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'redis', 'deprecate_calling_redis_inside_pipelined' do 4 | configure(parser: Synvert::SYNTAX_TREE_PARSER) 5 | 6 | description <<~EOS 7 | Deprecate calling commands on `Redis` insdie `Redis#pipelined`. 8 | 9 | ```ruby 10 | redis.pipelined do 11 | redis.get("key") 12 | end 13 | ``` 14 | 15 | => 16 | 17 | ```ruby 18 | redis.pipelined do |pipeline| 19 | pipeline.get("key") 20 | end 21 | ``` 22 | EOS 23 | 24 | if_gem 'redis', '>= 4.6.0' 25 | 26 | within_files Synvert::ALL_RUBY_FILES do 27 | find_node '.MethodAddBlock[call=.CallNode[message=pipelined]][block=.BlockNode[block_var=nil]]' do 28 | redis_name = node.call.receiver.to_source 29 | group do 30 | insert ' |pipeline|', to: 'block.opening', at: 'end' 31 | goto_node 'block.bodystmt' do 32 | find_node ".CallNode[receiver=#{redis_name}]" do 33 | replace :receiver, with: 'pipeline' 34 | end 35 | end 36 | end 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /lib/rspec/be_close_to_be_within.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rspec', 'be_close_to_be_within' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | It converts rspec be_close matcher to be_within matcher. 8 | 9 | ```ruby 10 | expect(1.0 / 3.0).to be_close(0.333, 0.001) 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | expect(1.0 / 3.0).to be_within(0.001).of(0.333) 17 | ``` 18 | EOS 19 | 20 | if_gem 'rspec-core', '>= 2.1' 21 | 22 | within_files Synvert::RAILS_RSPEC_FILES do 23 | # expect(1.0 / 3.0).to be_close(0.333, 0.001) => expect(1.0 / 3.0).to be_within(0.001).of(0.333) 24 | with_node node_type: 'send', message: 'to', arguments: { first: { node_type: 'send', message: 'be_close' } } do 25 | replace :arguments, with: "be_within({{arguments.first.arguments.last}}).of({{arguments.first.arguments.first}})" 26 | end 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /lib/rspec/block_to_expect.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rspec', 'block_to_expect' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | It converts rspec block to expect. 8 | 9 | ```ruby 10 | lambda { do_something }.should raise_error 11 | proc { do_something }.should raise_error 12 | -> { do_something }.should raise_error 13 | ``` 14 | 15 | => 16 | 17 | ```ruby 18 | expect { do_something }.to raise_error 19 | expect { do_something }.to raise_error 20 | expect { do_something }.to raise_error 21 | ``` 22 | EOS 23 | 24 | if_gem 'rspec-core', '>= 2.11' 25 | 26 | within_files Synvert::RAILS_RSPEC_FILES do 27 | # lambda { do_something }.should raise_error => expect { do_something }.to raise_error 28 | # proc { do_something }.should raise_error => expect { do_something }.to raise_error 29 | # -> { do_something }.should raise_error => expect { do_something }.to raise_error 30 | { should: 'to', should_not: 'not_to' }.each do |old_message, new_message| 31 | with_node node_type: 'send', receiver: { node_type: 'block' }, message: old_message do 32 | replace_with "expect { {{receiver.body}} }.#{new_message} {{arguments}}" 33 | end 34 | end 35 | end 36 | end 37 | -------------------------------------------------------------------------------- /lib/rspec/boolean_matcher.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rspec', 'boolean_matcher' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | It converts rspec boolean matcher. 8 | 9 | ```ruby 10 | be_true 11 | be_false 12 | ``` 13 | 14 | => 15 | 16 | ```ruby 17 | be_truthy 18 | be_falsey 19 | ``` 20 | EOS 21 | 22 | if_gem 'rspec-core', '>= 2.99' 23 | 24 | within_files Synvert::RAILS_RSPEC_FILES do 25 | # be_true => be_truthy 26 | # be_false => be_falsey 27 | { be_true: 'be_truthy', be_false: 'be_falsey' }.each do |old_matcher, new_matcher| 28 | with_node node_type: 'send', receiver: nil, message: old_matcher do 29 | replace_with new_matcher 30 | end 31 | end 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /lib/rspec/negative_error_expectation.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rspec', 'negative_error_expectation' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | It converts rspec negative error expectation. 8 | 9 | ```ruby 10 | expect { do_something }.not_to raise_error(SomeErrorClass) 11 | expect { do_something }.not_to raise_error('message') 12 | expect { do_something }.not_to raise_error(SomeErrorClass, 'message') 13 | ``` 14 | 15 | => 16 | 17 | ```ruby 18 | expect { do_something }.not_to raise_error 19 | expect { do_something }.not_to raise_error 20 | expect { do_something }.not_to raise_error 21 | ``` 22 | EOS 23 | if_gem 'rspec-core', '>= 2.14' 24 | 25 | within_files Synvert::RAILS_RSPEC_FILES do 26 | # expect { do_something }.not_to raise_error(SomeErrorClass) => expect { do_something }.not_to raise_error 27 | # expect { do_something }.not_to raise_error('message') => expect { do_something }.not_to raise_error 28 | # expect { do_something }.not_to raise_error(SomeErrorClass, 'message') => expect { do_something }.not_to raise_error 29 | within_node node_type: 'send', receiver: { node_type: 'block' }, message: 'not_to' do 30 | with_node node_type: 'send', message: 'raise_error', arguments: { size: { gt: 0 } } do 31 | replace_with 'raise_error' 32 | end 33 | end 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /lib/rspec/new_hook_scope.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rspec', 'new_hook_scope' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | It converts new hook scope. 8 | 9 | ```ruby 10 | before(:each) { do_something } 11 | before(:all) { do_something } 12 | ``` 13 | 14 | => 15 | 16 | ```ruby 17 | before(:example) { do_something } 18 | before(:context) { do_something } 19 | ``` 20 | EOS 21 | 22 | if_gem 'rspec-core', '>= 3.0' 23 | 24 | within_files Synvert::RAILS_RSPEC_FILES do 25 | # before(:each) { do_something } 26 | # => 27 | # before(:example) { do_something } 28 | # 29 | # before(:all) { do_something } 30 | # => 31 | # before(:context) { do_something } 32 | %w[before after around].each do |scope| 33 | with_node node_type: 'send', message: scope, arguments: [:all] do 34 | replace :arguments, with: ':context' 35 | end 36 | 37 | with_node node_type: 'send', message: scope, arguments: [:each] do 38 | replace :arguments, with: ':example' 39 | end 40 | end 41 | end 42 | end 43 | -------------------------------------------------------------------------------- /lib/rspec/stub_and_mock_to_double.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rspec', 'stub_and_mock_to_double' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | It converts stub and mock to double. 8 | 9 | ```ruby 10 | stub('something') 11 | mock('something') 12 | ``` 13 | 14 | => 15 | 16 | ```ruby 17 | double('something') 18 | double('something') 19 | ``` 20 | EOS 21 | 22 | if_gem 'rspec-core', '>= 2.14' 23 | 24 | within_files Synvert::RAILS_RSPEC_FILES do 25 | # stub('something') => double('something') 26 | # mock('something') => double('something') 27 | with_node node_type: 'send', receiver: nil, message: { in: ['stub', 'mock'] } do 28 | replace :message, with: 'double' 29 | end 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /lib/rspec/use_new_syntax.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'rspec', 'use_new_syntax' do 4 | description 'It converts rspec code to new syntax, it calls all rspec sub snippets.' 5 | 6 | add_snippet 'rspec', 'be_close_to_be_within' 7 | add_snippet 'rspec', 'block_to_expect' 8 | add_snippet 'rspec', 'boolean_matcher' 9 | add_snippet 'rspec', 'collection_matcher' 10 | add_snippet 'rspec', 'custom_matcher_new_syntax' 11 | add_snippet 'rspec', 'explicit_spec_type' 12 | add_snippet 'rspec', 'its_to_it' 13 | add_snippet 'rspec', 'message_expectation' 14 | add_snippet 'rspec', 'method_stub' 15 | add_snippet 'rspec', 'negative_error_expectation' 16 | add_snippet 'rspec', 'new_config_options' 17 | add_snippet 'rspec', 'new_hook_scope' 18 | add_snippet 'rspec', 'one_liner_expectation' 19 | add_snippet 'rspec', 'pending_to_skip' 20 | add_snippet 'rspec', 'remove_monkey_patches' 21 | add_snippet 'rspec', 'should_to_expect' 22 | add_snippet 'rspec', 'stub_and_mock_to_double' 23 | end 24 | -------------------------------------------------------------------------------- /lib/ruby/block_to_yield.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'block_to_yield' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | It converts block to yield. 8 | 9 | ```ruby 10 | def slow(&block) 11 | block.call 12 | end 13 | ``` 14 | 15 | => 16 | 17 | ```ruby 18 | def slow 19 | yield 20 | end 21 | ``` 22 | EOS 23 | 24 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 25 | # def slow(&block) 26 | # block.call 27 | # end 28 | # => 29 | # def slow 30 | # yield 31 | # end 32 | find_node '.def[arguments INCLUDES &block]' do 33 | if node.arguments.size > 1 34 | delete 'arguments.last', and_comma: true 35 | else 36 | delete :arguments, :parentheses 37 | end 38 | find_node '.send[receiver=block][message=call]' do 39 | group do 40 | delete :receiver, :dot 41 | replace :message, with: 'yield' 42 | end 43 | end 44 | end 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /lib/ruby/deprecate_big_decimal_new.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'deprecate_big_decimal_new' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It converts BigDecimal.new to BigDecimal 8 | 9 | ```ruby 10 | BigDecimal.new('1.1') 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | BigDecimal('1.1') 17 | ``` 18 | EOS 19 | 20 | if_ruby '2.6.0' 21 | 22 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 23 | find_node ".call_node[receiver=BigDecimal][name=new][arguments=.arguments_node[arguments.size=1]]" do 24 | delete :message, :call_operator 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /lib/ruby/deprecate_dir_exists.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'deprecate_dir_exists' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | `Dir.exists?` is deprecated in Ruby 2.1.0, and removed in Ruby 3.2.0. 8 | 9 | Replace it with `Dir.exist?`. 10 | EOS 11 | 12 | if_ruby '2.1.0' 13 | 14 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 15 | find_node '.call_node[receiver=Dir][name=exists?][arguments=.arguments_node[arguments.size=1]]' do 16 | replace :name, with: 'exist?' 17 | end 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /lib/ruby/deprecate_file_exists.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'deprecate_file_exists' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | `File.exists?` is deprecated in Ruby 2.1.0, and removed in Ruby 3.2.0. 8 | 9 | Replace it with `File.exist?`. 10 | EOS 11 | 12 | if_ruby '2.1.0' 13 | 14 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 15 | find_node '.call_node[receiver=File][name=exists?][arguments=.arguments_node[arguments.size=1]]' do 16 | replace :name, with: 'exist?' 17 | end 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /lib/ruby/deprecate_fixnum_and_bignum.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'deprecate_fixnum_and_bignum' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | Unify Fixnum and Bignum into Integer. 8 | EOS 9 | 10 | if_ruby '2.4.0' 11 | 12 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 13 | find_node '.constant_read_node[name IN (Fixnum Bignum)]' do 14 | replace_with 'Integer' 15 | end 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /lib/ruby/fast_syntax.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'fast_syntax' do 4 | description <<~EOS 5 | Use ruby fast syntax. 6 | 7 | Reference: https://speakerdeck.com/sferik/writing-fast-ruby 8 | EOS 9 | 10 | add_snippet 'ruby', 'block_to_yield' 11 | add_snippet 'ruby', 'gsub_to_tr' 12 | add_snippet 'ruby', 'keys_each_to_each_key' 13 | add_snippet 'ruby', 'map_and_flatten_to_flat_map' 14 | add_snippet 'ruby', 'merge_to_square_brackets' 15 | add_snippet 'ruby', 'parallel_assignment_to_sequential_assignment' 16 | add_snippet 'ruby', 'use_symbol_to_proc' 17 | add_snippet 'ruby', 'prefer_nil' 18 | end 19 | -------------------------------------------------------------------------------- /lib/ruby/frozen_string_literal_comment.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'frozen_string_literal_comment' do 4 | configure(parser: Synvert::SYNTAX_TREE_PARSER) 5 | 6 | description <<~EOS 7 | It adds frozen_string_literal: true comment. 8 | 9 | ```ruby 10 | 'hello world' 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | # frozen_string_literal: true 17 | 18 | 'hello world' 19 | ``` 20 | EOS 21 | 22 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 23 | find_node ":not_has(> .Comment[value='# frozen_string_literal: true'])" do 24 | insert "# frozen_string_literal: true\n\n", at: 'beginning' 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /lib/ruby/gsub_to_tr.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'gsub_to_tr' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | It converts `String#gsub` to `String#tr` 8 | 9 | ```ruby 10 | 'slug from title'.gsub(' ', '_') 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | 'slug from title'.tr(' ', '_') 17 | ``` 18 | EOS 19 | 20 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 21 | # 'slug from title'.gsub(' ', '_') 22 | # => 23 | # 'slug from title'.tr(' ', '_') 24 | find_node '.send[message=gsub][arguments.size=2][arguments.first=.str][arguments.last=.str]' do 25 | if node.arguments.first.to_value.length == 1 && node.arguments.last.to_value.length < 2 26 | replace :message, with: 'tr' 27 | end 28 | end 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /lib/ruby/kernel_open_to_uri_open.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'kernel_open_to_uri_open' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It converts `Kernel#open` to `URI.open` 8 | 9 | ```ruby 10 | open('http://test.com') 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | URI.open('http://test.com') 17 | ``` 18 | EOS 19 | 20 | if_ruby '2.7.0' 21 | 22 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 23 | # open('http://test.com') 24 | # => 25 | # URI.open('http://test.com') 26 | unless_exist_node('.def_node[name=open]') do 27 | find_node '.call_node[receiver=nil][name=open][arguments=.arguments_node[arguments.size=1]]' do 28 | insert 'URI.', to: 'name', at: 'beginning' 29 | end 30 | end 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /lib/ruby/keys_each_to_each_key.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'keys_each_to_each_key' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | It converts `Hash#keys.each` to `Hash#each_key` 8 | 9 | ```ruby 10 | params.keys.each {} 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | params.each_key {} 17 | ``` 18 | EOS 19 | 20 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 21 | # params.keys.each {} 22 | # => 23 | # params.each_key {} 24 | find_node '.send[receiver=.send[message=keys][arguments.size=0]][message=each][arguments.size=0]' do 25 | group do 26 | replace :receiver, with: '{{receiver.receiver}}' 27 | replace :message, with: 'each_key' 28 | end 29 | end 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /lib/ruby/map_and_flatten_to_flat_map.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'map_and_flatten_to_flat_map' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It converts `map` and `flatten` to `flat_map` 8 | 9 | ```ruby 10 | enum.map do |item| 11 | # do something 12 | end.flatten 13 | ``` 14 | 15 | => 16 | 17 | ```ruby 18 | enum.flat_map do |item| 19 | # do something 20 | end 21 | ``` 22 | EOS 23 | 24 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 25 | # enum.map do |item| 26 | # # do something 27 | # end.flatten 28 | # => 29 | # enum.flat_map do |item| 30 | # # do something 31 | # end 32 | find_node '.call_node[receiver=.call_node[name=map][arguments=nil][block=.block_node]][name=flatten][arguments=nil]' do 33 | group do 34 | delete :call_operator, :name 35 | replace 'receiver.name', with: 'flat_map' 36 | end 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /lib/ruby/nested_class_definition.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'nested_class_definition' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | It converts compact class definition to nested class definition. 8 | 9 | ```ruby 10 | class Foo::Bar < Base 11 | def test; end 12 | end 13 | ``` 14 | 15 | => 16 | 17 | ```ruby 18 | module Foo 19 | class Bar < Base 20 | def test; end 21 | end 22 | end 23 | ``` 24 | EOS 25 | 26 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 27 | # class Foo::Bar < Base 28 | # def test; end 29 | # end 30 | # => 31 | # module Foo 32 | # class Bar < Base 33 | # def test; end 34 | # end 35 | # end 36 | find_node '.class[name=~/::/]' do 37 | source = node.to_source 38 | parts = node.name.to_source.split('::') 39 | class_name = parts.pop 40 | new_parts = parts.map.with_index { |mod, index| ' ' * NodeMutation.tab_width * index + "module #{mod}" } 41 | new_parts.concat(source.sub(parts.join('::') + '::', '').split("\n").map { |line| (' ' * NodeMutation.tab_width * parts.size) + line }) 42 | new_parts.concat(parts.map.with_index { |mod, index| ' ' * NodeMutation.tab_width * (parts.size - index - 1) + "end" }) 43 | 44 | replace_with new_parts.join("\n") 45 | end 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /lib/ruby/new_1_9_hash_syntax.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'new_1_9_hash_syntax' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | Use ruby 1.9 new hash syntax. 8 | 9 | ```ruby 10 | { :foo => 'bar' } 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | { foo: 'bar' } 17 | ``` 18 | EOS 19 | 20 | if_ruby '1.9.0' 21 | 22 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 23 | # {:foo => 'bar'} => {foo: 'bar'} 24 | find_node %q{.hash_node .assoc_node[key=.symbol_node][key=~/\A:([^'"]+)\z/][operator = =>]} do 25 | replace_with "{{key.unescaped}}: {{value}}" 26 | end 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /lib/ruby/new_2_2_hash_syntax.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'new_2_2_hash_syntax' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~'EOS' 7 | Use ruby 2.2 new hash syntax. 8 | 9 | ```ruby 10 | { :foo => 'bar' } 11 | { :'foo-x' => 'bar' } 12 | { :"foo-#{suffix}" 'bar' } 13 | ``` 14 | 15 | => 16 | 17 | ```ruby 18 | { foo: 'bar' } 19 | { 'foo-x': 'bar' } 20 | { "foo-#{suffix}": 'bar' } 21 | ``` 22 | EOS 23 | 24 | add_snippet 'ruby', 'new_1_9_hash_syntax' 25 | 26 | if_ruby '2.2.0' 27 | 28 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 29 | # {:foo => 'bar'} => {foo: 'bar'} 30 | # {:'foo-x' => 'bar'} => {'foo-x': 'bar'} 31 | # {:"foo-#{suffix}" 'bar'} => {"foo-#{suffix}": 'bar'} 32 | find_node '.hash_node > .assoc_node[operator!=nil]' do 33 | case node.key.type 34 | when :symbol_node 35 | case node.key.to_source 36 | when /\A:"([^"'\\]*)"\z/ 37 | replace_with "'#{Regexp.last_match(1)}': {{value}}" 38 | when /\A:(.+)\z/ 39 | replace_with "#{Regexp.last_match(1)}: {{value}}" 40 | end 41 | when :interpolated_symbol_node 42 | if new_key = node.key.to_source[/\A:(.+)/, 1] 43 | replace_with "#{new_key}: {{value}}" 44 | end 45 | end 46 | end 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /lib/ruby/new_lambda_syntax.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'new_lambda_syntax' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | Use ruby new lambda syntax 8 | 9 | ```ruby 10 | lambda { test } 11 | lambda { |a, b, c| a + b + c } 12 | ``` 13 | 14 | => 15 | 16 | ```ruby 17 | -> { test } 18 | ->(a, b, c) { a + b + c } 19 | ``` 20 | EOS 21 | 22 | if_ruby '1.9.0' 23 | 24 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 25 | # lambda { test } => -> { test } 26 | find_node '.call_node[receiver=nil][name=lambda][block=.block_node][parameters=nil]' do 27 | replace_with '-> { {{block.body}} }' 28 | end 29 | 30 | # lambda { |a, b, c| a + b + c } => ->(a, b, c) { a + b + c } 31 | find_node '.call_node[receiver=nil][name=lambda][block=.block_node[parameters=.block_parameters_node]]' do 32 | replace_with '->({{block.parameters.parameters}}) { {{block.body}} }' 33 | end 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /lib/ruby/new_safe_navigation_operator.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'new_safe_navigation_operator' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | Use ruby new safe navigation operator. 8 | 9 | ```ruby 10 | u.try!(:profile).try(:thumbnails).try(:large, 100, format: 'jpg') 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | u&.profile&.thumbnails&.large(100, format: 'jpg') 17 | ``` 18 | EOS 19 | 20 | if_ruby '2.3.0' 21 | 22 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 23 | # u.try!(:profile).try!(:thumbnails).try!(:large, 100, format: 'jpg') 24 | # u.try(:profile).try(:thumbnails).try(:large, 100, format: 'jpg') 25 | # => 26 | # u&.profile&.thumbnails&.large(100, format: 'jpg') 27 | # u&.profile&.thumbnails&.large(100, format: 'jpg') 28 | find_node '.call_node[name IN (try try!)][arguments != nil][arguments.arguments.size > 0]' do 29 | group do 30 | insert '&', to: 'receiver' 31 | replace :message, with: '{{arguments.arguments.first.to_value}}' 32 | if node.arguments.arguments.size == 1 33 | delete :opening, :arguments, :closing 34 | else 35 | delete 'arguments.arguments.first', and_comma: true 36 | end 37 | end 38 | end 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /lib/ruby/numbered_parameters.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'numbered_parameters' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | It uses numbered parameters. 8 | 9 | ```ruby 10 | squared_numbers = (1...10).map { |num| num ** 2 } 11 | 12 | city_populations.each { |city, population| puts "Population of \#{city} is \#{population}" } 13 | ``` 14 | 15 | => 16 | 17 | ```ruby 18 | squared_numbers = (1...10).map { _1 ** 2 } 19 | 20 | city_populations.each { puts "Population of \#{_1} is \#{_2}" } 21 | ``` 22 | EOS 23 | 24 | if_ruby '2.7' 25 | 26 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 27 | find_node '.block[arguments.size > 0]' do 28 | group do 29 | node.arguments.each_with_index do |argument, index| 30 | find_node ".lvar[name=#{argument.name}]" do 31 | replace_with "_#{index + 1}" 32 | end 33 | end 34 | delete :arguments, :pipes 35 | end 36 | end 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /lib/ruby/parallel_assignment_to_sequential_assignment.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'parallel_assignment_to_sequential_assignment' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | It converts parallel assignment to sequential assignment. 8 | 9 | ```ruby 10 | a, b = 1, 2 11 | ``` 12 | 13 | => 14 | 15 | ```ruby 16 | a = 1 17 | b = 2 18 | ``` 19 | EOS 20 | 21 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 22 | # a, b = 1, 2 23 | # => 24 | # a = 1 25 | # b = 2 26 | find_node '.masgn[variable=.mlhs][value=.array][variable.children.size="{{value.children.size}}"]' do 27 | replace_with node.variable.children.zip(node.value.children).map { |left, right| 28 | "#{left.to_source} = #{right.to_source}" 29 | }.join("\n") 30 | end 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /lib/ruby/prefer_nil.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'prefer_nil' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It prefers .nil? 8 | 9 | https://gist.github.com/postmodern/66dfb41c8cc98f3bc3c2fd2fe7385542 10 | 11 | ```ruby 12 | value1 == nil 13 | value2 != nil 14 | ``` 15 | 16 | => 17 | 18 | ```ruby 19 | value1.nil? 20 | !value2.nil? 21 | ``` 22 | EOS 23 | 24 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 25 | find_node '.call_node[receiver=.call_node[receiver=nil]][name=!=][arguments=.arguments_node[arguments.size=1][arguments.0=.nil_node]]' do 26 | replace_with '!{{receiver}}.nil?' 27 | end 28 | find_node '.call_node[receiver=.call_node[receiver=nil]][name===][arguments=.arguments_node[arguments.size=1][arguments.0=.nil_node]]' do 29 | replace_with '{{receiver}}.nil?' 30 | end 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /lib/ruby/remove_debug_code.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'remove_debug_code' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It removes `puts` and `p` calls. 8 | EOS 9 | 10 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 11 | # removes puts and p methods 12 | find_node '.call_node[receiver=nil][name IN (puts p)]' do 13 | remove 14 | end 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /lib/ruby/shorthand_hash_syntax.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'shorthand_hash_syntax' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | Use ruby 3.1 hash shorthand syntax. 8 | 9 | ```ruby 10 | { a: a, b: b, c: c, d: d + 4 } 11 | some_method(a: a, b: b, c: c, d: d + 4) 12 | ``` 13 | 14 | => 15 | 16 | ```ruby 17 | { a:, b:, c:, d: d + 4 } 18 | some_method(a:, b:, c:, d: d + 4) 19 | ``` 20 | EOS 21 | 22 | if_ruby '3.1.0' 23 | 24 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 25 | # { a: a, b: b, c: c, d: d + 4 } 26 | # => 27 | # { a:, b:, c:, d: d + 4 } 28 | find_node '.hash_node .assoc_node[key=.symbol_node][key.unescaped="{{value}}"][operator=nil]' do 29 | replace_with '{{key}}' 30 | end 31 | 32 | # some_method(a: a, b: b, c: c, d: d + 4) 33 | # => 34 | # some_method(a:, b:, c:, d: d + 4) 35 | find_node '.call_node[opening!=nil] .arguments_node .keyword_hash_node .assoc_node[key=.symbol_node][key.unescaped="{{value}}"][operator=nil]' do 36 | replace_with '{{key}}' 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /lib/ruby/upgrade_1_8_to_1_9.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'upgrade_1_8_to_1_9' do 4 | description 'It upgrades ruby 1.8 to 1.9.' 5 | 6 | add_snippet 'ruby', 'new_1_9_hash_syntax' 7 | add_snippet 'ruby', 'new_lambda_syntax' 8 | end 9 | -------------------------------------------------------------------------------- /lib/ruby/upgrade_2_0_to_2_1.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'upgrade_2_0_to_2_1' do 4 | description 'It upgrades ruby 2.0 to 2.1.' 5 | 6 | add_snippet 'ruby', 'deprecate_dir_exists' 7 | add_snippet 'ruby', 'deprecate_file_exists' 8 | end 9 | -------------------------------------------------------------------------------- /lib/ruby/upgrade_2_1_to_2_2.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'upgrade_2_1_to_2_2' do 4 | description 'It upgrades ruby 2.1 to 2.2.' 5 | 6 | add_snippet 'ruby', 'deprecate_dir_exists' 7 | add_snippet 'ruby', 'deprecate_file_exists' 8 | add_snippet 'ruby', 'new_2_2_hash_syntax' 9 | end 10 | -------------------------------------------------------------------------------- /lib/ruby/upgrade_2_2_to_2_3.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'upgrade_2_2_to_2_3' do 4 | description 'It upgrades ruby 2.2 to 2.3.' 5 | 6 | add_snippet 'ruby', 'deprecate_dir_exists' 7 | add_snippet 'ruby', 'deprecate_file_exists' 8 | add_snippet 'ruby', 'new_safe_navigation_operator' 9 | end 10 | -------------------------------------------------------------------------------- /lib/ruby/upgrade_2_3_to_2_4.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'upgrade_2_3_to_2_4' do 4 | description 'It upgrades ruby 2.3 to 2.4.' 5 | 6 | add_snippet 'ruby', 'deprecate_dir_exists' 7 | add_snippet 'ruby', 'deprecate_file_exists' 8 | add_snippet 'ruby', 'deprecate_fixnum_and_bignum' 9 | end 10 | -------------------------------------------------------------------------------- /lib/ruby/upgrade_2_4_to_2_5.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'upgrade_2_4_to_2_5' do 4 | description 'It upgrades ruby 2.4 to 2.5.' 5 | 6 | add_snippet 'ruby', 'deprecate_dir_exists' 7 | add_snippet 'ruby', 'deprecate_file_exists' 8 | add_snippet 'ruby', 'deprecate_fixnum_and_bignum' 9 | end 10 | -------------------------------------------------------------------------------- /lib/ruby/upgrade_2_5_to_2_6.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'upgrade_2_5_to_2_6' do 4 | description 'It upgrades ruby 2.5 to 2.6.' 5 | 6 | add_snippet 'ruby', 'deprecate_big_decimal_new' 7 | add_snippet 'ruby', 'deprecate_dir_exists' 8 | add_snippet 'ruby', 'deprecate_file_exists' 9 | add_snippet 'ruby', 'deprecate_fixnum_and_bignum' 10 | end 11 | -------------------------------------------------------------------------------- /lib/ruby/upgrade_2_6_to_2_7.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'upgrade_2_6_to_2_7' do 4 | description 'It upgrades ruby 2.6 to 2.7.' 5 | 6 | add_snippet 'ruby', 'deprecate_big_decimal_new' 7 | add_snippet 'ruby', 'deprecate_dir_exists' 8 | add_snippet 'ruby', 'deprecate_file_exists' 9 | add_snippet 'ruby', 'deprecate_fixnum_and_bignum' 10 | add_snippet 'ruby', 'kernel_open_to_uri_open' 11 | add_snippet 'ruby', 'uri_escape_to_uri_default_parser_escape' 12 | add_snippet 'ruby', 'use_keyword_arguments' 13 | end 14 | -------------------------------------------------------------------------------- /lib/ruby/upgrade_2_7_to_3_0.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'upgrade_2_7_to_3_0' do 4 | description 'It upgrades ruby 2.7 to 3.0.' 5 | 6 | add_snippet 'ruby', 'deprecate_dir_exists' 7 | add_snippet 'ruby', 'deprecate_file_exists' 8 | add_snippet 'ruby', 'uri_escape_to_uri_default_parser_escape' 9 | end 10 | -------------------------------------------------------------------------------- /lib/ruby/upgrade_3_0_to_3_1.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'upgrade_3_0_to_3_1' do 4 | description 'It upgrades ruby 3.0 to 3.1.' 5 | 6 | add_snippet 'ruby', 'deprecate_dir_exists' 7 | add_snippet 'ruby', 'deprecate_file_exists' 8 | add_snippet 'ruby', 'deprecate_fixnum_and_bignum' 9 | add_snippet 'ruby', 'shorthand_hash_syntax' 10 | end 11 | -------------------------------------------------------------------------------- /lib/ruby/upgrade_3_1_to_3_2.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'upgrade_3_1_to_3_2' do 4 | description 'It upgrades ruby 3.1 to 3.2.' 5 | 6 | add_snippet 'ruby', 'deprecate_dir_exists' 7 | add_snippet 'ruby', 'deprecate_file_exists' 8 | add_snippet 'ruby', 'deprecate_fixnum_and_bignum' 9 | end 10 | -------------------------------------------------------------------------------- /lib/ruby/upgrade_3_3_to_3_4.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'upgrade_3_3_to_3_4' do 4 | description 'It upgrades ruby 3.3 to 3.4.' 5 | 6 | add_snippet 'ruby', 'use_it_keyword' 7 | end 8 | -------------------------------------------------------------------------------- /lib/ruby/uri_escape_to_uri_default_parser_escape.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'uri_escape_to_uri_default_parser_escape' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | Use URI::DEFAULT_PARSER.escape instead of URI.escape. 8 | 9 | ```ruby 10 | URI.escape(url) 11 | URI.encode(url) 12 | ``` 13 | 14 | => 15 | 16 | ```ruby 17 | URI::DEFAULT_PARSER.escape(url) 18 | URI::DEFAULT_PARSER.escape(url) 19 | ``` 20 | EOS 21 | 22 | if_ruby '2.7.0' 23 | 24 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 25 | # URI.escape(url) => URI::DEFAULT_PARSER.escape(url) 26 | # URI.encode(url) => URI::DEFAULT_PARSER.escape(url) 27 | find_node '.call_node[receiver=URI][name IN (escape encode)][arguments=.arguments_node[arguments.size=1]]' do 28 | group do 29 | replace :receiver, with: '{{receiver}}::DEFAULT_PARSER' 30 | replace :message, with: 'escape' 31 | end 32 | end 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /lib/ruby/use_it_keyword.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'use_it_keyword' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It uses it keyword. 8 | 9 | ```ruby 10 | (1...10).map { |num| num ** 2 } 11 | (1...10).map { _1 ** 2 } 12 | ``` 13 | 14 | => 15 | 16 | ```ruby 17 | (1...10).map { it ** 2 } 18 | (1...10).map { it ** 2 } 19 | ``` 20 | EOS 21 | 22 | if_ruby '3.4' 23 | 24 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 25 | find_node '.call_node[block=.block_node[parameters=.block_parameters_node[parameters=.parameters_node[requireds.size=1]]]]' do 26 | group do 27 | find_node ".local_variable_read_node[name=#{node.block.parameters.parameters.requireds[0].name}]" do 28 | replace_with 'it' 29 | end 30 | delete 'block.parameters' 31 | end 32 | end 33 | 34 | find_node '.call_node[block=.block_node[parameters=.numbered_parameters_node[maximum=1]]] .local_variable_read_node[name=_1]' do 35 | replace_with 'it' 36 | end 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /lib/ruby/use_keyword_arguments.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'use_keyword_arguments' do 4 | configure(parser: Synvert::PRISM_PARSER) 5 | 6 | description <<~EOS 7 | It tries to convert ruby code to use keyword arguments. 8 | 9 | ```ruby 10 | CSV.generate(options) do |csv| 11 | end 12 | ``` 13 | => 14 | ```ruby 15 | CSV.generate(**options) do |csv| 16 | end 17 | ``` 18 | EOS 19 | 20 | if_ruby '2.7' 21 | 22 | # CSV.generate(options) do |csv| 23 | # end 24 | # => 25 | # CSV.generate(**options) do |csv| 26 | # end 27 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 28 | with_node node_type: 'call_node', 29 | receiver: 'CSV', 30 | name: 'generate', 31 | arguments: { node_type: 'arguments_node', arguments: { size: 1, '0': { node_type: { in: %w[local_variable_read_node instance_variable_read_node call_node] } } } } do 32 | insert '**', to: 'arguments.arguments.0', at: 'beginning' 33 | end 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /lib/ruby/use_symbol_to_proc.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'ruby', 'use_symbol_to_proc' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | It uses &: (short for symbol to proc) 8 | 9 | ```ruby 10 | (1..100).each { |i| i.to_s } 11 | (1..100).map { |i| i.to_s } 12 | ``` 13 | 14 | => 15 | 16 | ```ruby 17 | (1..100).each(&:to_s) 18 | (1..100).map(&:to_s) 19 | ``` 20 | EOS 21 | 22 | within_files Synvert::ALL_RUBY_FILES + Synvert::ALL_RAKE_FILES do 23 | # (1..100).each { |i| i.to_s } 24 | # => 25 | # (1..100).each(&:to_s) 26 | # 27 | # (1..100).map { |i| i.to_s } 28 | # => 29 | # (1..100).map(&:to_s) 30 | find_node '.block[caller=.send[message in (each map)]] 31 | [arguments.size=1] 32 | [body.size=1] 33 | [body.first=.send[arguments.size=0]] 34 | [body.first.receiver="{{arguments.first}}"]' do 35 | replace_with '{{caller}}(&:{{body.first.message}})' 36 | end 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /lib/shoulda/fix_2_6_deprecations.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'shoulda', 'fix_2_6_deprecations' do 4 | configure(parser: Synvert::PARSER_PARSER) 5 | 6 | description <<~EOS 7 | It fixes shoulda 2.6 deprecations. 8 | 9 | ```ruby 10 | should ensure_inclusion_of(:age).in_range(0..100) 11 | should ensure_exclusion_of(:age).in_range(0..100) 12 | ``` 13 | 14 | => 15 | 16 | ```ruby 17 | should validate_inclusion_of(:age).in_range(0..100) 18 | should validate_exclusion_of(:age).in_range(0..100) 19 | ``` 20 | EOS 21 | 22 | if_gem 'shoulda-matchers', '> 2.6.2' 23 | 24 | within_files Synvert::RAILS_MODEL_TEST_FILES do 25 | # should ensure_inclusion_of(:age).in_range(0..100) 26 | # => 27 | # should validate_inclusion_of(:age).in_range(0..100) 28 | find_node '.send[message=ensure_inclusion_of][arguments.size=1]' do 29 | replace :message, with: 'validate_inclusion_of' 30 | end 31 | 32 | # should ensure_exclusion_of(:age).in_range(0..100) 33 | # => 34 | # should validate_exclusion_of(:age).in_range(0..100) 35 | find_node '.send[message=ensure_exclusion_of][arguments.size=1]' do 36 | replace :message, with: 'validate_exclusion_of' 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /lib/shoulda/use_new_syntax.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Synvert::Rewriter.new 'shoulda', 'use_new_syntax' do 4 | description 'It uses shoulda new syntax and fix deprecations.' 5 | 6 | add_snippet 'shoulda', 'use_matcher_syntax' 7 | add_snippet 'shoulda', 'fix_1_5_deprecations' 8 | add_snippet 'shoulda', 'fix_2_6_deprecations' 9 | end 10 | -------------------------------------------------------------------------------- /spec/bullet/rename_whitelist_to_safelist_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Rename bullet whitelist to safelist' do 6 | let(:rewriter_name) { 'bullet/rename_whitelist_to_safelist' } 7 | 8 | let(:test_content) { <<-EOS } 9 | Bullet.add_whitelist(type: :n_plus_one_query, class_name: 'Klass', association: :department) 10 | Bullet.delete_whitelist(type: :n_plus_one_query, class_name: 'Klass', association: :team) 11 | Bullet.get_whitelist_associations(:n_plus_one_query, 'Klass') 12 | Bullet.reset_whitelist 13 | Bullet.clear_whitelist 14 | EOS 15 | 16 | let(:test_rewritten_content) { <<-EOS } 17 | Bullet.add_safelist(type: :n_plus_one_query, class_name: 'Klass', association: :department) 18 | Bullet.delete_safelist(type: :n_plus_one_query, class_name: 'Klass', association: :team) 19 | Bullet.get_safelist_associations(:n_plus_one_query, 'Klass') 20 | Bullet.reset_safelist 21 | Bullet.clear_safelist 22 | EOS 23 | 24 | include_examples 'convertable' 25 | end 26 | -------------------------------------------------------------------------------- /spec/debug_me/remove_debug_me_calls_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Ruby removes debug_me calls' do 6 | let(:rewriter_name) { 'debug_me/remove_debug_me_calls' } 7 | let(:test_content) { <<~EOS } 8 | def test 9 | debug_me 10 | debug_me('== HERE ==') 11 | debug_me{} 12 | debug_me{[ :hello, 'world' ]} 13 | debug_me(tag: 'ERROR', levels: 5){[ :hello, :world ]} 14 | # 15 | DebugMe.debug_me 16 | DebugMe.debug_me('== HERE ==') 17 | DebugMe.debug_me{} 18 | DebugMe.debug_me{[ :hello, 'world' ]} 19 | DebugMe.debug_me(tag: 'ERROR', levels: 5){[ :hello, :world ]} 20 | end 21 | EOS 22 | 23 | let(:test_rewritten_content) { <<~EOS } 24 | def test 25 | # 26 | end 27 | EOS 28 | 29 | include_examples 'convertable' 30 | end 31 | -------------------------------------------------------------------------------- /spec/factory_bot/use_string_as_class_name_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Use string as class name' do 6 | let(:rewriter_name) { 'factory_bot/use_string_as_class_name' } 7 | let(:fake_file_path) { 'spec/factories/post.rb' } 8 | let(:test_content) { <<~EOS } 9 | FactoryBot.define do 10 | factory :admin, class: User do 11 | name { 'Admin' } 12 | end 13 | end 14 | EOS 15 | 16 | let(:test_rewritten_content) { <<~EOS } 17 | FactoryBot.define do 18 | factory :admin, class: 'User' do 19 | name { 'Admin' } 20 | end 21 | end 22 | EOS 23 | 24 | include_examples 'convertable' 25 | end 26 | -------------------------------------------------------------------------------- /spec/helpers/set_rails_load_defaults_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | require 'helpers/set_rails_load_defaults' 5 | 6 | RSpec.describe 'rails/set_load_defaults helper', fakefs: true do 7 | it 'sets config.load_defaults' do 8 | rewriter = 9 | Synvert::Rewriter.new 'test', 'set_rails_load_defaults_helper' do 10 | call_helper 'rails/set_load_defaults', rails_version: '6.0' 11 | end 12 | 13 | file_path = 'config/application.rb' 14 | FileUtils.mkdir_p(File.dirname(file_path)) 15 | File.write(file_path, <<~EOF) 16 | module Synvert 17 | class Application < Rails::Application 18 | config.load_defaults 5.2 19 | end 20 | end 21 | EOF 22 | 23 | rewriter.process 24 | 25 | expect(File.read(file_path)).to eq <<~EOF 26 | module Synvert 27 | class Application < Rails::Application 28 | config.load_defaults 6.0 29 | end 30 | end 31 | EOF 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /spec/minitest/assert_empty_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts assert_empty' do 6 | let(:rewriter_name) { 'minitest/assert_empty' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert(object.empty?) 10 | EOS 11 | 12 | let(:test_rewritten_content) { <<~EOS } 13 | assert_empty(object) 14 | EOS 15 | 16 | include_examples 'convertable' 17 | end 18 | -------------------------------------------------------------------------------- /spec/minitest/assert_equal_arguments_order_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts assert_equal arguments order' do 6 | let(:rewriter_name) { 'minitest/assert_equal_arguments_order' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert_equal(actual, "rubocop-minitest") 10 | EOS 11 | 12 | let(:test_rewritten_content) { <<~EOS } 13 | assert_equal("rubocop-minitest", actual) 14 | EOS 15 | 16 | include_examples 'convertable' 17 | end 18 | -------------------------------------------------------------------------------- /spec/minitest/assert_includes_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts assert_includes' do 6 | let(:rewriter_name) { 'minitest/assert_includes' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert(collection.include?(object)) 10 | EOS 11 | 12 | let(:test_rewritten_content) { <<~EOS } 13 | assert_includes(collection, object) 14 | EOS 15 | 16 | include_examples 'convertable' 17 | end 18 | -------------------------------------------------------------------------------- /spec/minitest/assert_instance_of_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts assert_instance_of' do 6 | let(:rewriter_name) { 'minitest/assert_instance_of' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert('rubocop-minitest'.instance_of?(String)) 10 | EOS 11 | 12 | let(:test_rewritten_content) { <<~EOS } 13 | assert_instance_of(String, 'rubocop-minitest') 14 | EOS 15 | 16 | include_examples 'convertable' 17 | end 18 | -------------------------------------------------------------------------------- /spec/minitest/assert_kind_of_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts assert_kind_of' do 6 | let(:rewriter_name) { 'minitest/assert_kind_of' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert('rubocop-minitest'.kind_of?(String)) 10 | EOS 11 | 12 | let(:test_rewritten_content) { <<~EOS } 13 | assert_kind_of(String, 'rubocop-minitest') 14 | EOS 15 | 16 | include_examples 'convertable' 17 | end 18 | -------------------------------------------------------------------------------- /spec/minitest/assert_match_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts assert_match' do 6 | let(:rewriter_name) { 'minitest/assert_match' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert(pattern.match?(object)) 10 | EOS 11 | 12 | let(:test_rewritten_content) { <<~EOS } 13 | assert_match(pattern, object) 14 | EOS 15 | 16 | include_examples 'convertable' 17 | end 18 | -------------------------------------------------------------------------------- /spec/minitest/assert_nil_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts assert_nil' do 6 | let(:rewriter_name) { 'minitest/assert_nil' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert_equal(nil, actual) 10 | EOS 11 | 12 | let(:test_rewritten_content) { <<~EOS } 13 | assert_nil(actual) 14 | EOS 15 | 16 | include_examples 'convertable' 17 | end 18 | -------------------------------------------------------------------------------- /spec/minitest/assert_operator_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts assert_operator' do 6 | let(:rewriter_name) { 'minitest/assert_operator' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert(expected < actual) 10 | EOS 11 | 12 | let(:test_rewritten_content) { <<~EOS } 13 | assert_operator(expected, :<, actual) 14 | EOS 15 | 16 | include_examples 'convertable' 17 | end 18 | -------------------------------------------------------------------------------- /spec/minitest/assert_path_exists_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts assert_path_exists' do 6 | let(:rewriter_name) { 'minitest/assert_path_exists' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert(File.exist?(path)) 10 | EOS 11 | 12 | let(:test_rewritten_content) { <<~EOS } 13 | assert_path_exists(path) 14 | EOS 15 | 16 | include_examples 'convertable' 17 | end 18 | -------------------------------------------------------------------------------- /spec/minitest/assert_predicate_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts assert_predicate' do 6 | let(:rewriter_name) { 'minitest/assert_predicate' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert expected.zero? 10 | assert_equal 0, expected 11 | EOS 12 | 13 | let(:test_rewritten_content) { <<~EOS } 14 | assert_predicate expected, :zero? 15 | assert_predicate expected, :zero? 16 | EOS 17 | 18 | include_examples 'convertable' 19 | end 20 | -------------------------------------------------------------------------------- /spec/minitest/assert_respond_to_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts assert_respond_to' do 6 | let(:rewriter_name) { 'minitest/assert_respond_to' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert(object.respond_to?(some_method)) 10 | EOS 11 | 12 | let(:test_rewritten_content) { <<~EOS } 13 | assert_respond_to(object, some_method) 14 | EOS 15 | 16 | include_examples 'convertable' 17 | end 18 | -------------------------------------------------------------------------------- /spec/minitest/assert_same_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts assert_same' do 6 | let(:rewriter_name) { 'minitest/assert_same' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert(expected.equal?(actual)) 10 | EOS 11 | 12 | let(:test_rewritten_content) { <<~EOS } 13 | assert_same(expected, actual) 14 | EOS 15 | 16 | include_examples 'convertable' 17 | end 18 | -------------------------------------------------------------------------------- /spec/minitest/assert_silent_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts assert_silent' do 6 | let(:rewriter_name) { 'minitest/assert_silent' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert_output('', '') { puts object.do_something } 10 | EOS 11 | 12 | let(:test_rewritten_content) { <<~EOS } 13 | assert_silent { puts object.do_something } 14 | EOS 15 | 16 | include_examples 'convertable' 17 | end 18 | -------------------------------------------------------------------------------- /spec/minitest/assert_truthy_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts assert_truthy' do 6 | let(:rewriter_name) { 'minitest/assert_truthy' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert_equal(true, actual) 10 | EOS 11 | 12 | let(:test_rewritten_content) { <<~EOS } 13 | assert(actual) 14 | EOS 15 | 16 | include_examples 'convertable' 17 | end 18 | -------------------------------------------------------------------------------- /spec/minitest/hooks_super_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts hooks_super' do 6 | let(:rewriter_name) { 'minitest/hooks_super' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | class TestMeme < Minitest::Test 10 | include MyHelper 11 | 12 | def setup 13 | do_something 14 | end 15 | 16 | def teardown 17 | clean_something 18 | end 19 | end 20 | EOS 21 | 22 | let(:test_rewritten_content) { <<~EOS } 23 | class TestMeme < Minitest::Test 24 | include MyHelper 25 | 26 | def setup 27 | super 28 | do_something 29 | end 30 | 31 | def teardown 32 | clean_something 33 | super 34 | end 35 | end 36 | EOS 37 | 38 | include_examples 'convertable' 39 | end 40 | -------------------------------------------------------------------------------- /spec/minitest/refute_empty_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts refute_empty' do 6 | let(:rewriter_name) { 'minitest/refute_empty' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert(!object.empty?) 10 | refute(object.empty?) 11 | EOS 12 | 13 | let(:test_rewritten_content) { <<~EOS } 14 | refute_empty(object) 15 | refute_empty(object) 16 | EOS 17 | 18 | include_examples 'convertable' 19 | end 20 | -------------------------------------------------------------------------------- /spec/minitest/refute_equal_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts refute_equal' do 6 | let(:rewriter_name) { 'minitest/refute_equal' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert("rubocop-minitest" != actual) 10 | assert(!"rubocop-minitest" == actual) 11 | EOS 12 | 13 | let(:test_rewritten_content) { <<~EOS } 14 | refute_equal("rubocop-minitest", actual) 15 | refute_equal("rubocop-minitest", actual) 16 | EOS 17 | 18 | include_examples 'convertable' 19 | end 20 | -------------------------------------------------------------------------------- /spec/minitest/refute_false_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts refute_false' do 6 | let(:rewriter_name) { 'minitest/refute_false' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert_equal(false, actual) 10 | assert(!something) 11 | EOS 12 | 13 | let(:test_rewritten_content) { <<~EOS } 14 | refute(actual) 15 | refute(something) 16 | EOS 17 | 18 | include_examples 'convertable' 19 | end 20 | -------------------------------------------------------------------------------- /spec/minitest/refute_includes_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts refute_includes' do 6 | let(:rewriter_name) { 'minitest/refute_includes' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | refute(collection.include?(object)) 10 | assert(!collection.include?(object)) 11 | EOS 12 | 13 | let(:test_rewritten_content) { <<~EOS } 14 | refute_includes(collection, object) 15 | refute_includes(collection, object) 16 | EOS 17 | 18 | include_examples 'convertable' 19 | end 20 | -------------------------------------------------------------------------------- /spec/minitest/refute_instance_of_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts refute_instance_of' do 6 | let(:rewriter_name) { 'minitest/refute_instance_of' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert(!'rubocop-minitest'.instance_of?(String)) 10 | refute('rubocop-minitest'.instance_of?(String)) 11 | EOS 12 | 13 | let(:test_rewritten_content) { <<~EOS } 14 | refute_instance_of(String, 'rubocop-minitest') 15 | refute_instance_of(String, 'rubocop-minitest') 16 | EOS 17 | 18 | include_examples 'convertable' 19 | end 20 | -------------------------------------------------------------------------------- /spec/minitest/refute_kind_of_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts refute_kind_of' do 6 | let(:rewriter_name) { 'minitest/refute_kind_of' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert(!'rubocop-minitest'.kind_of?(String)) 10 | refute('rubocop-minitest'.kind_of?(String)) 11 | EOS 12 | 13 | let(:test_rewritten_content) { <<~EOS } 14 | refute_kind_of(String, 'rubocop-minitest') 15 | refute_kind_of(String, 'rubocop-minitest') 16 | EOS 17 | 18 | include_examples 'convertable' 19 | end 20 | -------------------------------------------------------------------------------- /spec/minitest/refute_match_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts refute_match' do 6 | let(:rewriter_name) { 'minitest/refute_match' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert(!pattern.match?(object)) 10 | refute(pattern.match?(object)) 11 | EOS 12 | 13 | let(:test_rewritten_content) { <<~EOS } 14 | refute_match(pattern, object) 15 | refute_match(pattern, object) 16 | EOS 17 | 18 | include_examples 'convertable' 19 | end 20 | -------------------------------------------------------------------------------- /spec/minitest/refute_nil_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts refute_nil' do 6 | let(:rewriter_name) { 'minitest/refute_nil' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert(!actual.nil?) 10 | refute(actual.nil?) 11 | EOS 12 | 13 | let(:test_rewritten_content) { <<~EOS } 14 | refute_nil(actual) 15 | refute_nil(actual) 16 | EOS 17 | 18 | include_examples 'convertable' 19 | end 20 | -------------------------------------------------------------------------------- /spec/minitest/refute_operator_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts refute_operator' do 6 | let(:rewriter_name) { 'minitest/refute_operator' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert(!(expected > actual)) 10 | refute(expected > actual) 11 | EOS 12 | 13 | let(:test_rewritten_content) { <<~EOS } 14 | refute_operator(expected, :>, actual) 15 | refute_operator(expected, :>, actual) 16 | EOS 17 | 18 | include_examples 'convertable' 19 | end 20 | -------------------------------------------------------------------------------- /spec/minitest/refute_path_exists_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts refute_path_exists' do 6 | let(:rewriter_name) { 'minitest/refute_path_exists' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert(!File.exist?(path)) 10 | refute(File.exist?(path)) 11 | EOS 12 | 13 | let(:test_rewritten_content) { <<~EOS } 14 | refute_path_exists(path) 15 | refute_path_exists(path) 16 | EOS 17 | 18 | include_examples 'convertable' 19 | end 20 | -------------------------------------------------------------------------------- /spec/minitest/refute_predicate_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts refute_predicate' do 6 | let(:rewriter_name) { 'minitest/refute_predicate' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert(!expected.zero?) 10 | refute(expected.zero?) 11 | EOS 12 | 13 | let(:test_rewritten_content) { <<~EOS } 14 | refute_predicate(expected, :zero?) 15 | refute_predicate(expected, :zero?) 16 | EOS 17 | 18 | include_examples 'convertable' 19 | end 20 | -------------------------------------------------------------------------------- /spec/minitest/refute_respond_to_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts refute_respond_to' do 6 | let(:rewriter_name) { 'minitest/refute_respond_to' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | assert(!object.respond_to?(some_method)) 10 | refute(object.respond_to?(some_method)) 11 | EOS 12 | 13 | let(:test_rewritten_content) { <<~EOS } 14 | refute_respond_to(object, some_method) 15 | refute_respond_to(object, some_method) 16 | EOS 17 | 18 | include_examples 'convertable' 19 | end 20 | -------------------------------------------------------------------------------- /spec/minitest/refute_same_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Minitest converts refute_same' do 6 | let(:rewriter_name) { 'minitest/refute_same' } 7 | let(:fake_file_path) { 'test/units/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | refute(expected.equal?(actual)) 10 | assert(!expected.equal?(actual)) 11 | EOS 12 | 13 | let(:test_rewritten_content) { <<~EOS } 14 | refute_same(expected, actual) 15 | refute_same(expected, actual) 16 | EOS 17 | 18 | include_examples 'convertable' 19 | end 20 | -------------------------------------------------------------------------------- /spec/rails/active_record_association_call_use_keyword_arguments_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'ActiveRecord assocation call to use keyword arguments' do 6 | let(:rewriter_name) { 'rails/active_record_association_call_use_keyword_arguments' } 7 | let(:fake_file_path) { 'app/models/user.rb' } 8 | let(:test_content) { <<~EOS } 9 | class User < ApplicationRecord 10 | has_many :posts, { dependent: :destroy } 11 | end 12 | EOS 13 | 14 | let(:test_rewritten_content) { <<~EOS } 15 | class User < ApplicationRecord 16 | has_many :posts, dependent: :destroy 17 | end 18 | EOS 19 | 20 | include_examples 'convertable' 21 | end 22 | -------------------------------------------------------------------------------- /spec/rails/add_active_record_migration_rails_version_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Add ActiveRecord::Migration rails version' do 6 | let(:rewriter_name) { 'rails/add_active_record_migration_rails_version' } 7 | let(:fake_file_path) { 'db/migrate/20180101000000_create_users.rb' } 8 | let(:test_content) { <<~EOS } 9 | class CreateUsers < ActiveRecord::Migration 10 | end 11 | EOS 12 | 13 | let(:test_rewritten_content) { <<~EOS } 14 | class CreateUsers < ActiveRecord::Migration[4.2] 15 | end 16 | EOS 17 | 18 | include_examples 'convertable' 19 | end 20 | -------------------------------------------------------------------------------- /spec/rails/add_application_job_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Add ApplicationJob' do 6 | let(:rewriter_name) { 'rails/add_application_job' } 7 | 8 | context 'add application_job' do 9 | let(:fake_file_path) { 'app/jobs/application_job.rb' } 10 | let(:test_content) { nil } 11 | let(:test_rewritten_content) { <<~EOS } 12 | class ApplicationJob < ActiveJob::Base 13 | end 14 | EOS 15 | 16 | include_examples 'convertable' 17 | end 18 | 19 | context 'rename ActiveJob::Base' do 20 | let(:fake_file_path) { 'app/jobs/user_job.rb' } 21 | let(:test_content) { <<~EOS } 22 | class UserJob < ActiveJob::Base 23 | end 24 | EOS 25 | 26 | let(:test_rewritten_content) { <<~EOS } 27 | class UserJob < ApplicationJob 28 | end 29 | EOS 30 | 31 | include_examples 'convertable' 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /spec/rails/add_application_mailer_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Add ApplicationMailer' do 6 | let(:rewriter_name) { 'rails/add_application_mailer' } 7 | 8 | context 'add application_job' do 9 | let(:fake_file_path) { 'app/mailers/application_mailer.rb' } 10 | let(:test_content) { nil } 11 | let(:test_rewritten_content) { <<~EOS } 12 | class ApplicationMailer < ActionMailer::Base 13 | end 14 | EOS 15 | 16 | include_examples 'convertable' 17 | end 18 | 19 | context 'rename ActionMailer::Base' do 20 | let(:fake_file_path) { 'app/mailers/post_mailer.rb' } 21 | let(:test_content) { <<~EOS } 22 | class PostMailer < ActionMailer::Base 23 | end 24 | EOS 25 | 26 | let(:test_rewritten_content) { <<~EOS } 27 | class PostMailer < ApplicationMailer 28 | end 29 | EOS 30 | 31 | include_examples 'convertable' 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /spec/rails/add_application_record_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Add ApplicationRecord' do 6 | let(:rewriter_name) { 'rails/add_application_record' } 7 | 8 | context 'add application_record' do 9 | let(:fake_file_path) { 'app/models/application_record.rb' } 10 | let(:test_content) { nil } 11 | let(:test_rewritten_content) { <<~EOS } 12 | class ApplicationRecord < ActiveRecord::Base 13 | self.abstract_class = true 14 | end 15 | EOS 16 | 17 | include_examples 'convertable' 18 | end 19 | 20 | context 'rename ActiveRecord::Base' do 21 | let(:fake_file_path) { 'app/models/post.rb' } 22 | let(:test_content) { <<~EOS } 23 | class Post < ActiveRecord::Base 24 | end 25 | EOS 26 | 27 | let(:test_rewritten_content) { <<~EOS } 28 | class Post < ApplicationRecord 29 | end 30 | EOS 31 | 32 | include_examples 'convertable' 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /spec/rails/application_secrets_use_symbol_keys_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'application.secrets uses symbol keys' do 6 | let(:rewriter_name) { 'rails/application_secrets_use_symbol_keys' } 7 | let(:fake_file_path) { 'app/models/post.rb' } 8 | let(:test_content) { <<~EOS } 9 | class Post < ApplicationRecord 10 | def configs 11 | Rails.application.config.secrets["smtp_settings"]["address"] 12 | end 13 | end 14 | EOS 15 | let(:test_rewritten_content) { <<~EOS } 16 | class Post < ApplicationRecord 17 | def configs 18 | Rails.application.config.secrets[:smtp_settings][:address] 19 | end 20 | end 21 | EOS 22 | 23 | include_examples 'convertable' 24 | end 25 | -------------------------------------------------------------------------------- /spec/rails/convert_configs_2_3_to_3_0_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert rails configs from 2.3 to 3.0' do 6 | let(:rewriter_name) { 'rails/convert_configs_2_3_to_3_0' } 7 | let(:application_controller_content) { <<~EOS } 8 | class ApplicationController < ActionController::Base 9 | filter_parameter_logging :password, :password_confirmation 10 | end 11 | EOS 12 | 13 | let(:application_content) { <<~EOS } 14 | module Synvert 15 | class Application < Rails::Application 16 | end 17 | end 18 | EOS 19 | 20 | let(:application_controller_rewritten_content) { <<~EOS } 21 | class ApplicationController < ActionController::Base 22 | end 23 | EOS 24 | 25 | let(:application_rewritten_content) { <<~EOS } 26 | module Synvert 27 | class Application < Rails::Application 28 | config.filter_parameters += [:password, :password_confirmation] 29 | end 30 | end 31 | EOS 32 | 33 | let(:fake_file_paths) { %w[config/application.rb app/controllers/application_controller.rb] } 34 | let(:test_contents) { [application_content, application_controller_content] } 35 | let(:test_rewritten_contents) { [application_rewritten_content, application_controller_rewritten_content] } 36 | 37 | include_examples 'convertable with multiple files' 38 | end 39 | -------------------------------------------------------------------------------- /spec/rails/convert_configs_3_1_to_3_2_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert rails configs from 3.1 to 3.2' do 6 | let(:rewriter_name) { 'rails/convert_configs_3_1_to_3_2' } 7 | let(:development_content) { <<~EOS } 8 | Synvert::Application.configure do 9 | end 10 | EOS 11 | 12 | let(:development_rewritten_content) { <<~EOS } 13 | Synvert::Application.configure do 14 | config.active_record.mass_assignment_sanitizer = :strict 15 | config.active_record.auto_explain_threshold_in_seconds = 0.5 16 | end 17 | EOS 18 | 19 | let(:test_content) { <<~EOS } 20 | Synvert::Application.configure do 21 | end 22 | EOS 23 | 24 | let(:test_rewritten_content) { <<~EOS } 25 | Synvert::Application.configure do 26 | config.active_record.mass_assignment_sanitizer = :strict 27 | end 28 | EOS 29 | 30 | let(:fake_file_paths) { %w[config/environments/development.rb config/environments/test.rb] } 31 | let(:test_contents) { [development_content, test_content] } 32 | let(:test_rewritten_contents) { [development_rewritten_content, test_rewritten_content] } 33 | 34 | include_examples 'convertable with multiple files' 35 | end 36 | -------------------------------------------------------------------------------- /spec/rails/convert_configs_4_1_to_4_2_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert rails configs from 4.1 to 4.2' do 6 | let(:rewriter_name) { 'rails/convert_configs_4_1_to_4_2' } 7 | let(:application_content) { <<~EOS } 8 | module Synvert 9 | class Application < Rails::Application 10 | end 11 | end 12 | EOS 13 | 14 | let(:application_rewritten_content) { <<~EOS } 15 | module Synvert 16 | class Application < Rails::Application 17 | config.active_record.raise_in_transactional_callbacks = true 18 | end 19 | end 20 | EOS 21 | 22 | let(:production_content) { <<~EOS } 23 | module Synvert 24 | class Application < Rails::Application 25 | config.serve_static_assets = false 26 | end 27 | end 28 | EOS 29 | 30 | let(:production_rewritten_content) { <<~EOS } 31 | module Synvert 32 | class Application < Rails::Application 33 | config.serve_static_files = false 34 | end 35 | end 36 | EOS 37 | 38 | let(:fake_file_paths) { %w[config/application.rb config/environments/production.rb] } 39 | let(:test_contents) { [application_content, production_content] } 40 | let(:test_rewritten_contents) { [application_rewritten_content, production_rewritten_content] } 41 | 42 | include_examples 'convertable with multiple files' 43 | end 44 | -------------------------------------------------------------------------------- /spec/rails/convert_configs_5_0_to_5_1_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert rails configs from 5.0 to 5.1' do 6 | let(:rewriter_name) { 'rails/convert_configs_5_0_to_5_1' } 7 | let(:fake_file_path) { 'config/application.rb' } 8 | let(:test_content) { <<~EOS } 9 | module Synvert 10 | class Application < Rails::Application 11 | config.load_defaults 5.0 12 | end 13 | end 14 | EOS 15 | let(:test_rewritten_content) { <<~EOS } 16 | module Synvert 17 | class Application < Rails::Application 18 | config.load_defaults 5.1 19 | end 20 | end 21 | EOS 22 | before { load_helpers(%w[helpers/set_rails_load_defaults]) } 23 | 24 | include_examples 'convertable' 25 | end 26 | -------------------------------------------------------------------------------- /spec/rails/convert_configs_5_1_to_5_2_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert rails configs from 5.1 to 5.2' do 6 | let(:rewriter_name) { 'rails/convert_configs_5_1_to_5_2' } 7 | let(:fake_file_path) { 'config/application.rb' } 8 | let(:test_content) { <<~EOS } 9 | module Synvert 10 | class Application < Rails::Application 11 | config.load_defaults 5.1 12 | config.cache_store = :dalli_store, 'cache-1.example.com', 'cache-2.example.com' 13 | end 14 | end 15 | EOS 16 | let(:test_rewritten_content) { <<~EOS } 17 | module Synvert 18 | class Application < Rails::Application 19 | config.load_defaults 5.2 20 | config.cache_store = :mem_cache_store, 'cache-1.example.com', 'cache-2.example.com' 21 | end 22 | end 23 | EOS 24 | before { load_helpers(%w[helpers/set_rails_load_defaults]) } 25 | 26 | include_examples 'convertable' 27 | end 28 | -------------------------------------------------------------------------------- /spec/rails/convert_configs_5_2_to_6_0_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert rails configs from 5.2 to 6.0' do 6 | let(:rewriter_name) { 'rails/convert_configs_5_2_to_6_0' } 7 | let(:fake_file_path) { 'config/application.rb' } 8 | let(:test_content) { <<~EOS } 9 | module Synvert 10 | class Application < Rails::Application 11 | config.load_defaults 5.2 12 | end 13 | end 14 | EOS 15 | let(:test_rewritten_content) { <<~EOS } 16 | module Synvert 17 | class Application < Rails::Application 18 | config.load_defaults 6.0 19 | end 20 | end 21 | EOS 22 | before { load_helpers(%w[helpers/set_rails_load_defaults.rb]) } 23 | 24 | include_examples 'convertable' 25 | end 26 | -------------------------------------------------------------------------------- /spec/rails/convert_configs_6_0_to_6_1_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert rails configs from 6.0 to 6.1' do 6 | let(:rewriter_name) { 'rails/convert_configs_6_0_to_6_1' } 7 | let(:fake_file_path) { 'config/application.rb' } 8 | let(:test_content) { <<~EOS } 9 | module Synvert 10 | class Application < Rails::Application 11 | config.load_defaults 6.0 12 | end 13 | end 14 | EOS 15 | let(:test_rewritten_content) { <<~EOS } 16 | module Synvert 17 | class Application < Rails::Application 18 | config.load_defaults 6.1 19 | end 20 | end 21 | EOS 22 | before { load_helpers(%w[helpers/set_rails_load_defaults.rb]) } 23 | 24 | include_examples 'convertable' 25 | end 26 | -------------------------------------------------------------------------------- /spec/rails/convert_configs_6_1_to_7_0_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert rails configs from 6.1 to 7.0' do 6 | let(:rewriter_name) { 'rails/convert_configs_6_1_to_7_0' } 7 | let(:fake_file_path) { 'config/application.rb' } 8 | let(:test_content) { <<~EOS } 9 | module Synvert 10 | class Application < Rails::Application 11 | config.load_defaults 6.1 12 | config.autoloader = :zeitwerk 13 | end 14 | end 15 | EOS 16 | let(:test_rewritten_content) { <<~EOS } 17 | module Synvert 18 | class Application < Rails::Application 19 | config.load_defaults 7.0 20 | end 21 | end 22 | EOS 23 | before { load_helpers(%w[helpers/set_rails_load_defaults.rb]) } 24 | 25 | include_examples 'convertable' 26 | end 27 | -------------------------------------------------------------------------------- /spec/rails/convert_constants_3_1_to_3_2_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert rails constants from 3.1 to 3.2' do 6 | let(:rewriter_name) { 'rails/convert_constants_3_1_to_3_2' } 7 | let(:fake_file_path) { 'app/controllers/application_controller.rb' } 8 | let(:test_content) { <<~EOS } 9 | class ApplicationController < ActionController::Base 10 | rescue_from ActionController::UnknownAction, :with => :render_404 11 | end 12 | EOS 13 | 14 | let(:test_rewritten_content) { <<~EOS } 15 | class ApplicationController < ActionController::Base 16 | rescue_from AbstractController::ActionNotFound, :with => :render_404 17 | end 18 | EOS 19 | 20 | include_examples 'convertable' 21 | end 22 | -------------------------------------------------------------------------------- /spec/rails/convert_constants_3_2_to_4_0_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert rails constants from 3.2 to 4.0' do 6 | let(:rewriter_name) { 'rails/convert_constants_3_2_to_4_0' } 7 | let(:fake_file_path) { 'test/unit/post_test.rb' } 8 | let(:test_content) { <<~EOS } 9 | require "test_helper" 10 | 11 | class PostTest < ActiveRecord::TestCase 12 | def constants 13 | [ActionController::Integration, ActionController::IntegrationTest, ActionController::PerformanceTest, ActionController::AbstractRequest, 14 | ActionController::Request, ActionController::AbstractResponse, ActionController::Response, ActionController::Routing] 15 | end 16 | end 17 | EOS 18 | 19 | let(:test_rewritten_content) { <<~EOS } 20 | require "test_helper" 21 | 22 | class PostTest < ActiveSupport::TestCase 23 | def constants 24 | [ActionDispatch::Integration, ActionDispatch::IntegrationTest, ActionDispatch::PerformanceTest, ActionDispatch::Request, 25 | ActionDispatch::Request, ActionDispatch::Response, ActionDispatch::Response, ActionDispatch::Routing] 26 | end 27 | end 28 | EOS 29 | 30 | include_examples 'convertable' 31 | end 32 | -------------------------------------------------------------------------------- /spec/rails/convert_constants_4_2_to_5_0_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert rails constants from 4.2 to 5.0' do 6 | let(:rewriter_name) { 'rails/convert_constants_4_2_to_5_0' } 7 | let(:fake_file_paths) { 'app/controllers/posts_controller.rb' } 8 | let(:test_content) { <<~EOS } 9 | class PostsController < ApplicationController 10 | def test_load_error 11 | rescue MissingSourceFile 12 | end 13 | end 14 | EOS 15 | let(:test_rewritten_content) { <<~EOS } 16 | class PostsController < ApplicationController 17 | def test_load_error 18 | rescue LoadError 19 | end 20 | end 21 | EOS 22 | 23 | include_examples 'convertable' 24 | end 25 | -------------------------------------------------------------------------------- /spec/rails/convert_constants_5_0_to_5_1_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert rails constants from 5.0 to 5.1' do 6 | let(:rewriter_name) { 'rails/convert_constants_5_0_to_5_1' } 7 | let(:fake_file_path) { 'app/models/post.rb' } 8 | let(:test_content) { <<~EOS } 9 | class Post < ApplicationRecord 10 | def configs 11 | rgb = HashWithIndifferentAccess.new 12 | rgb[:black] = "#000000" 13 | rgb = ActiveSupport::HashWithIndifferentAccess.new 14 | rgb[:white] = "#FFFFFF" 15 | end 16 | end 17 | EOS 18 | let(:test_rewritten_content) { <<~EOS } 19 | class Post < ApplicationRecord 20 | def configs 21 | rgb = ActiveSupport::HashWithIndifferentAccess.new 22 | rgb[:black] = "#000000" 23 | rgb = ActiveSupport::HashWithIndifferentAccess.new 24 | rgb[:white] = "#FFFFFF" 25 | end 26 | end 27 | EOS 28 | 29 | include_examples 'convertable' 30 | end 31 | -------------------------------------------------------------------------------- /spec/rails/convert_controller_filter_to_action_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert controller filter to action' do 6 | let(:rewriter_name) { 'rails/convert_controller_filter_to_action' } 7 | let(:fake_file_path) { 'app/controllers/posts_controller.rb' } 8 | let(:test_content) { <<~EOS } 9 | class PostsController < ApplicationController 10 | skip_filter :authorize 11 | before_filter :load_post 12 | after_filter :track_post 13 | around_filter :log_post 14 | end 15 | EOS 16 | 17 | let(:test_rewritten_content) { <<~EOS } 18 | class PostsController < ApplicationController 19 | skip_action_callback :authorize 20 | before_action :load_post 21 | after_action :track_post 22 | around_action :log_post 23 | end 24 | EOS 25 | 26 | include_examples 'convertable' 27 | end 28 | -------------------------------------------------------------------------------- /spec/rails/convert_env_to_request_env_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert env to request.env' do 6 | let(:rewriter_name) { 'rails/convert_env_to_request_env' } 7 | let(:fake_file_path) { 'app/controllers/posts_controller.rb' } 8 | let(:test_content) { <<~EOS } 9 | class OmniauthCallbacksController < ApplicationController 10 | def create 11 | env['omniauth.auth'] 12 | end 13 | end 14 | EOS 15 | 16 | let(:test_rewritten_content) { <<~EOS } 17 | class OmniauthCallbacksController < ApplicationController 18 | def create 19 | request.env['omniauth.auth'] 20 | end 21 | end 22 | EOS 23 | 24 | include_examples 'convertable' 25 | end 26 | -------------------------------------------------------------------------------- /spec/rails/convert_head_response_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert head response' do 6 | let(:rewriter_name) { 'rails/convert_head_response' } 7 | let(:fake_file_path) { 'app/controllers/posts_controller.rb' } 8 | let(:test_content) { <<~EOS } 9 | class PostsController < ApplicationController 10 | rescue_from BadGateway do 11 | head status: 502 12 | end 13 | 14 | def ok 15 | render nothing: true 16 | end 17 | 18 | def created 19 | render nothing: true, status: :created 20 | end 21 | 22 | def redirect 23 | head location: "/foo" 24 | end 25 | end 26 | EOS 27 | 28 | let(:test_rewritten_content) { <<~EOS } 29 | class PostsController < ApplicationController 30 | rescue_from BadGateway do 31 | head 502 32 | end 33 | 34 | def ok 35 | head :ok 36 | end 37 | 38 | def created 39 | head :created 40 | end 41 | 42 | def redirect 43 | head :ok, location: "/foo" 44 | end 45 | end 46 | EOS 47 | 48 | include_examples 'convertable' 49 | end 50 | -------------------------------------------------------------------------------- /spec/rails/convert_model_errors_add_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert model errors.add' do 6 | let(:rewriter_name) { 'rails/convert_model_errors_add' } 7 | let(:fake_file_path) { 'app/models/post.rb' } 8 | let(:test_content) { <<~EOS } 9 | class Post < ApplicationRecord 10 | def validate_author 11 | errors[:base] = "author not present" unless author 12 | self.errors[:base] = "author not present" unless author 13 | end 14 | end 15 | EOS 16 | 17 | let(:test_rewritten_content) { <<~EOS } 18 | class Post < ApplicationRecord 19 | def validate_author 20 | errors.add(:base, "author not present") unless author 21 | self.errors.add(:base, "author not present") unless author 22 | end 23 | end 24 | EOS 25 | 26 | include_examples 'convertable' 27 | end 28 | -------------------------------------------------------------------------------- /spec/rails/convert_model_lambda_scope_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert model lambda scope' do 6 | let(:rewriter_name) { 'rails/convert_model_lambda_scope' } 7 | let(:fake_file_path) { 'app/models/post.rb' } 8 | let(:test_content) { <<~EOS } 9 | class Post < ActiveRecord::Base 10 | scope :active, where(active: true) 11 | scope :published, Proc.new { where(published: true) } 12 | scope :by_user, proc { |user_id| where(user_id: user_id) } 13 | 14 | default_scope order("updated_at DESC") 15 | default_scope { order("created_at DESC") } 16 | end 17 | EOS 18 | 19 | let(:test_rewritten_content) { <<~EOS } 20 | class Post < ActiveRecord::Base 21 | scope :active, -> { where(active: true) } 22 | scope :published, -> { where(published: true) } 23 | scope :by_user, ->(user_id) { where(user_id: user_id) } 24 | 25 | default_scope -> { order("updated_at DESC") } 26 | default_scope -> { order("created_at DESC") } 27 | end 28 | EOS 29 | 30 | include_examples 'convertable' 31 | end 32 | -------------------------------------------------------------------------------- /spec/rails/convert_models_3_2_to_4_0_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert rails models from 3.2 to 4.0' do 6 | let(:rewriter_name) { 'rails/convert_models_3_2_to_4_0' } 7 | let(:fake_file_path) { 'app/models/post.rb' } 8 | let(:test_content) { <<~EOS } 9 | class Post < ActiveRecord::Base 10 | has_many :comments, dependent: :restrict 11 | 12 | def serialized_attrs 13 | self.serialized_attributes 14 | end 15 | end 16 | EOS 17 | 18 | let(:test_rewritten_content) { <<~EOS } 19 | class Post < ActiveRecord::Base 20 | has_many :comments, dependent: :restrict_with_exception 21 | 22 | def serialized_attrs 23 | self.class.serialized_attributes 24 | end 25 | end 26 | EOS 27 | 28 | include_examples 'convertable' 29 | end 30 | -------------------------------------------------------------------------------- /spec/rails/convert_rails_env_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert RAILS_ENV to Rails.env' do 6 | let(:rewriter_name) { 'rails/convert_rails_env' } 7 | let(:fake_file_path) { 'config/initializers/constant.rb' } 8 | let(:test_content) { <<~EOS } 9 | RAILS_ENV 10 | RAILS_ENV == 'test' 11 | 'development' == RAILS_ENV 12 | RAILS_ENV != 'test' 13 | 'development' != RAILS_ENV 14 | EOS 15 | 16 | let(:test_rewritten_content) { <<~EOS } 17 | Rails.env 18 | Rails.env.test? 19 | Rails.env.development? 20 | !Rails.env.test? 21 | !Rails.env.development? 22 | EOS 23 | 24 | include_examples 'convertable' 25 | end 26 | -------------------------------------------------------------------------------- /spec/rails/convert_rails_logger_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Upgrade RAILS_DEFAULT_LOGGER to Rails.logger' do 6 | let(:rewriter_name) { 'rails/convert_rails_logger' } 7 | let(:fake_file_path) { 'config/initializers/constant.rb' } 8 | let(:test_content) { <<~EOS } 9 | RAILS_DEFAULT_LOGGER 10 | EOS 11 | 12 | let(:test_rewritten_content) { <<~EOS } 13 | Rails.logger 14 | EOS 15 | 16 | include_examples 'convertable' 17 | end 18 | -------------------------------------------------------------------------------- /spec/rails/convert_rails_root_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert RAILS_ROOT to Rails.root' do 6 | let(:rewriter_name) { 'rails/convert_rails_root' } 7 | let(:fake_file_path) { 'config/initializers/constant.rb' } 8 | let(:test_content) { <<~EOS } 9 | RAILS_ROOT 10 | File.join(RAILS_ROOT, "config", "database.yml") 11 | RAILS_ROOT + "/config/database.yml" 12 | "\#{RAILS_ROOT}/config/database.yml" 13 | File.exists?(RAILS_ROOT + "/config/database.yml") 14 | EOS 15 | 16 | let(:test_rewritten_content) { <<~EOS } 17 | Rails.root 18 | Rails.root.join("config", "database.yml") 19 | Rails.root.join("config/database.yml") 20 | Rails.root.join("config/database.yml") 21 | Rails.root.join("config/database.yml").exist? 22 | EOS 23 | 24 | include_examples 'convertable' 25 | end 26 | -------------------------------------------------------------------------------- /spec/rails/convert_render_text_to_render_plain_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert render :text to render :plain' do 6 | let(:rewriter_name) { 'rails/convert_render_text_to_render_plain' } 7 | let(:fake_file_path) { 'app/controllers/posts_controller.rb' } 8 | let(:test_content) { <<~EOS } 9 | class PostsController < ApplicationController 10 | def foo 11 | render :text => 'OK' 12 | end 13 | 14 | def bar 15 | render text: 'Not OK', status: 403 16 | end 17 | end 18 | EOS 19 | 20 | let(:test_rewritten_content) { <<~EOS } 21 | class PostsController < ApplicationController 22 | def foo 23 | render :plain => 'OK' 24 | end 25 | 26 | def bar 27 | render plain: 'Not OK', status: 403 28 | end 29 | end 30 | EOS 31 | 32 | include_examples 'convertable' 33 | end 34 | -------------------------------------------------------------------------------- /spec/rails/convert_routes_3_2_to_4_0_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert rails routes from 3.2 to 4.0' do 6 | let(:rewriter_name) { 'rails/convert_routes_3_2_to_4_0' } 7 | let(:fake_file_path) { 'config/routes.rb' } 8 | let(:test_content) { <<~EOS } 9 | Synvert::Application.routes.draw do 10 | get Rack::Utils.escape('こんにちは'), controller: 'welcome', action: 'index' 11 | match '/' => 'root#index' 12 | match 'new', to: 'episodes#new' 13 | end 14 | EOS 15 | let(:test_rewritten_content) { <<~EOS } 16 | Synvert::Application.routes.draw do 17 | get 'こんにちは', controller: 'welcome', action: 'index' 18 | get '/' => 'root#index' 19 | get 'new', to: 'episodes#new' 20 | end 21 | EOS 22 | 23 | include_examples 'convertable' 24 | end 25 | -------------------------------------------------------------------------------- /spec/rails/convert_update_attributes_to_update_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert update_attributes to update' do 6 | let(:rewriter_name) { 'rails/convert_update_attributes_to_update' } 7 | let(:fake_file_path) { 'app/model/user.rb' } 8 | let(:test_content) { <<~EOS } 9 | class User < ActiveRecord::Base 10 | def test 11 | update_attributes(title: 'new') 12 | self.update_attributes!(title: 'new') 13 | self.update(title: 'new') 14 | 15 | role&.update_attributes(admin: false) 16 | end 17 | end 18 | EOS 19 | 20 | let(:test_rewritten_content) { <<~EOS } 21 | class User < ActiveRecord::Base 22 | def test 23 | update(title: 'new') 24 | self.update!(title: 'new') 25 | self.update(title: 'new') 26 | 27 | role&.update(admin: false) 28 | end 29 | end 30 | EOS 31 | 32 | include_examples 'convertable' 33 | end 34 | -------------------------------------------------------------------------------- /spec/rails/convert_views_2_3_to_3_0_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert rails views from 2.3 to 3.0' do 6 | let(:rewriter_name) { 'rails/convert_views_2_3_to_3_0' } 7 | let(:fake_file_path) { 'app/views/posts/_form.html.erb' } 8 | let(:test_content) { <<~EOS } 9 | <%= h user.login %> 10 | <%= post.title %> 11 | 12 | <% form_for post, :html => {:id => 'post-form'} do |post_form| %> 13 | <% fields_for :author, post.author do |author_form| %> 14 | <% end %> 15 | <% end %> 16 | EOS 17 | 18 | let(:test_rewritten_content) { <<~EOS } 19 | <%= user.login %> 20 | <%= post.title %> 21 | 22 | <%= form_for post, :html => {:id => 'post-form'} do |post_form| %> 23 | <%= fields_for :author, post.author do |author_form| %> 24 | <% end %> 25 | <% end %> 26 | EOS 27 | 28 | include_examples 'convertable' 29 | end 30 | -------------------------------------------------------------------------------- /spec/rails/convert_views_3_2_to_4_0_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert rails views from 3.2 to 4.0' do 6 | let(:rewriter_name) { 'rails/convert_views_3_2_to_4_0' } 7 | let(:fake_file_path) { 'app/views/posts/index.html.erb' } 8 | let(:test_content) { <<~EOS } 9 | <% @posts.each do |post| %> 10 | <%= link_to "delete", post_url(post), remote: true, confirm: "Are you sure to delete a post" %> 11 | <%= link_to "delete", post_url(post), remote: true, data: { foo: "bar" }, confirm: "Are you sure to delete a post" %> 12 | <% end %> 13 | EOS 14 | 15 | let(:test_rewritten_content) { <<~EOS } 16 | <% @posts.each do |post| %> 17 | <%= link_to "delete", post_url(post), remote: true, data: { confirm: "Are you sure to delete a post" } %> 18 | <%= link_to "delete", post_url(post), remote: true, data: { foo: "bar", confirm: "Are you sure to delete a post" } %> 19 | <% end %> 20 | EOS 21 | 22 | include_examples 'convertable' 23 | end 24 | -------------------------------------------------------------------------------- /spec/rails/deprecate_active_record_migration_check_pending_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Deprecate ActiveRecord::Migration.check_pending!' do 6 | let(:rewriter_name) { 'rails/deprecate_active_record_migration_check_pending' } 7 | let(:fake_file_path) { 'test/test_helper.rb' } 8 | let(:test_content) { 'ActiveRecord::Migration.check_pending!' } 9 | let(:test_rewritten_content) { '' } 10 | 11 | include_examples 'convertable' 12 | end 13 | -------------------------------------------------------------------------------- /spec/rails/deprecate_errors_as_hash_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Deprecate errors as hash' do 6 | let(:rewriter_name) { 'rails/deprecate_errors_as_hash' } 7 | let(:fake_file_path) { 'app/controllers/posts_controller.rb' } 8 | let(:test_content) { <<~EOS } 9 | class PostsController < ApplicationController 10 | def create 11 | @post = Post.create(post_params) 12 | @post.errors[:title] << 'is not interesting enough.' 13 | @post.errors.messages.delete(:comments) 14 | render json: { fields: @post.errors.keys, errors: @post.errors.values } 15 | end 16 | end 17 | EOS 18 | 19 | let(:test_rewritten_content) { <<~EOS } 20 | class PostsController < ApplicationController 21 | def create 22 | @post = Post.create(post_params) 23 | @post.errors.add(:title, 'is not interesting enough.') 24 | @post.errors.delete(:comments) 25 | render json: { fields: @post.errors.map(&:attribute_names), errors: @post.errors.map(&:message) } 26 | end 27 | end 28 | EOS 29 | 30 | include_examples 'convertable' 31 | end 32 | -------------------------------------------------------------------------------- /spec/rails/deprecate_multi_json_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Replace multi_json with json' do 6 | let(:rewriter_name) { 'rails/deprecate_multi_json' } 7 | let(:fake_file_path) { 'app/models/post.rb' } 8 | let(:test_content) { <<~EOS } 9 | class Post < ActiveRecord::Base 10 | def test 11 | json = MultiJson.dump self 12 | MultiJson.load json 13 | end 14 | end 15 | EOS 16 | let(:test_rewritten_content) { <<~EOS } 17 | class Post < ActiveRecord::Base 18 | def test 19 | json = self.to_json 20 | JSON.parse json 21 | end 22 | end 23 | EOS 24 | 25 | include_examples 'convertable' 26 | end 27 | -------------------------------------------------------------------------------- /spec/rails/explicitly-render-with-formats_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Explicitly render with formats' do 6 | let(:rewriter_name) { 'rails/explicitly-render-with-formats' } 7 | let(:fake_file_path) { 'app/controllers/foo_controller.rb' } 8 | let(:test_content) { "render template: 'index.json'" } 9 | let(:test_rewritten_content) { "render template: 'index', formats: [:json]" } 10 | 11 | include_examples 'convertable' 12 | end 13 | -------------------------------------------------------------------------------- /spec/rails/fix_model_3_2_deprecations_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Fix rails model 3.2 deprecations' do 6 | let(:rewriter_name) { 'rails/fix_model_3_2_deprecations' } 7 | let(:fake_file_path) { 'app/models/project.rb' } 8 | let(:test_content) { <<~EOS } 9 | class Project < ActiveRecord::Base 10 | set_table_name "project" 11 | end 12 | EOS 13 | 14 | let(:test_rewritten_content) { <<~EOS } 15 | class Project < ActiveRecord::Base 16 | self.table_name = "project" 17 | end 18 | EOS 19 | 20 | include_examples 'convertable' 21 | end 22 | -------------------------------------------------------------------------------- /spec/rails/migrate-ujs-to-turbo_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Convert Foo to Bar' do 6 | let(:rewriter_name) { 'rails/migrate-ujs-to-turbo' } 7 | let(:fake_file_path) { 'app/views/posts/index.html.erb' } 8 | let(:test_content) { <<~EOS } 9 | <%= link_to "Destroy", post_path(post), method: :delete %> 10 | <%= link_to "Destroy", post_path(post), method: :delete, data: { confirm: 'Are you sure?' } %> 11 | <%= submit_tag "Create", data: { disable_with: "Submitting..." } %> 12 | EOS 13 | let(:test_rewritten_content) { <<~EOS } 14 | <%= link_to "Destroy", post_path(post), data: { turbo_method: :delete } %> 15 | <%= link_to "Destroy", post_path(post), data: { turbo_method: :delete, turbo_confirm: 'Are you sure?' } %> 16 | <%= submit_tag "Create", data: { turbo_submits_with: "Submitting..." } %> 17 | EOS 18 | 19 | include_examples 'convertable' 20 | end 21 | -------------------------------------------------------------------------------- /spec/rails/new_enum_syntax_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'New enum syntax' do 6 | let(:rewriter_name) { 'rails/new_enum_syntax' } 7 | let(:fake_file_path) { 'app/models/post.rb' } 8 | let(:test_content) { <<~EOS } 9 | class Post < ActiveRecord::Base 10 | enum status: [ :draft, :published, :archived ], _prefix: true, _scopes: false 11 | enum category: [ :free, :premium ], _suffix: true, _default: :free 12 | 13 | enum status: { draft: 0, published: 1, archived: 2 }, prefix: true, scopes: false 14 | enum category: { free: 0, premium: 1 }, suffix: true, default: :free 15 | end 16 | EOS 17 | 18 | let(:test_rewritten_content) { <<~EOS } 19 | class Post < ActiveRecord::Base 20 | enum :status, [ :draft, :published, :archived ], prefix: true, scopes: false 21 | enum :category, [ :free, :premium ], suffix: true, default: :free 22 | 23 | enum :status, { draft: 0, published: 1, archived: 2 }, prefix: true, scopes: false 24 | enum :category, { free: 0, premium: 1 }, suffix: true, default: :free 25 | end 26 | EOS 27 | 28 | include_examples 'convertable' 29 | end 30 | -------------------------------------------------------------------------------- /spec/rails/prefer_nor_conditions_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Prefer NOR conditions' do 6 | let(:rewriter_name) { 'rails/prefer_nor_conditions' } 7 | let(:fake_file_path) { 'app/models/user.rb' } 8 | let(:test_content) { <<~EOS } 9 | where.not(first_name: nil, last_name: nil, email: nil) 10 | EOS 11 | 12 | let(:test_rewritten_content) { <<~EOS } 13 | where.not(first_name: nil).where.not(last_name: nil).where.not(email: nil) 14 | EOS 15 | 16 | include_examples 'convertable' 17 | end 18 | -------------------------------------------------------------------------------- /spec/rails/remove_active_support_dependencies_private_api_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Remove ActiveSupport::Dependencies private api' do 6 | let(:rewriter_name) { 'rails/remove_active_support_dependencies_private_api' } 7 | let(:fake_file_path) { 'app/models/user.rb' } 8 | let(:test_content) { <<~EOS } 9 | ActiveSupport::Dependencies.constantize("User") 10 | ActiveSupport::Dependencies.safe_constantize("User") 11 | EOS 12 | 13 | let(:test_rewritten_content) { <<~EOS } 14 | "User".constantize 15 | "User".safe_constantize 16 | EOS 17 | 18 | include_examples 'convertable' 19 | end 20 | -------------------------------------------------------------------------------- /spec/rails/rename_errors_keys_to_attribute_names_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Rename errors.keys to error.attribute_names' do 6 | let(:rewriter_name) { 'rails/rename_errors_keys_to_attribute_names' } 7 | let(:fake_file_path) { 'app/models/user.rb' } 8 | let(:test_content) { <<~EOS } 9 | user.errors.keys.include?(:name) 10 | EOS 11 | 12 | let(:test_rewritten_content) { <<~EOS } 13 | user.errors.attribute_names.include?(:name) 14 | EOS 15 | 16 | include_examples 'convertable' 17 | end 18 | -------------------------------------------------------------------------------- /spec/rails/update_active_storage_variant_argument_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Update ActiveStorage variant argument' do 6 | let(:rewriter_name) { 'rails/update_active_storage_variant_argument' } 7 | let(:fake_file_path) { 'app/models/item.rb' } 8 | let(:test_content) { <<~EOS } 9 | class Item < ApplicationRecord 10 | def resize 11 | image.variant(resize: "100x") 12 | image.variant(crop: "1920x1080+0+0") 13 | image.variant(resize_and_pad: [300, 300]) 14 | image.variant(monochrome: true) 15 | end 16 | end 17 | EOS 18 | 19 | let(:test_rewritten_content) { <<~EOS } 20 | class Item < ApplicationRecord 21 | def resize 22 | image.variant(resize_to_limit: [100, nil]) 23 | image.variant(crop: [0, 0, 1920, 1080]) 24 | image.variant(resize_and_pad: [300, 300, background: [255]]) 25 | image.variant(colourspace: "b-w") 26 | end 27 | end 28 | EOS 29 | 30 | include_examples 'convertable' 31 | end 32 | -------------------------------------------------------------------------------- /spec/rails/use_active_storage_image_processing_macros_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Uses ActiveStorage image_processing macros' do 6 | let(:rewriter_name) { 'rails/use_active_storage_image_processing_macros' } 7 | let(:fake_file_path) { 'app/models/item.rb' } 8 | let(:test_content) { <<~EOS } 9 | class Item < ApplicationRecord 10 | def resize 11 | video.preview(resize: "100x100") 12 | video.preview(resize: "100x100>") 13 | video.preview(resize: "100x100^") 14 | end 15 | end 16 | EOS 17 | 18 | let(:test_rewritten_content) { <<~EOS } 19 | class Item < ApplicationRecord 20 | def resize 21 | video.preview(resize_to_fit: [100, 100]) 22 | video.preview(resize_to_limit: [100, 100]) 23 | video.preview(resize_to_fill: [100, 100]) 24 | end 25 | end 26 | EOS 27 | 28 | include_examples 'convertable' 29 | end 30 | -------------------------------------------------------------------------------- /spec/rails/use_migrations_instance_methods_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Uses instance methods in migrations' do 6 | let(:rewriter_name) { 'rails/use_migrations_instance_methods' } 7 | let(:fake_file_path) { 'db/migrate/20140831000000_create_posts.rb' } 8 | let(:test_content) { <<~EOS } 9 | class CreatePosts < ActiveRecord::Migration 10 | def self.up 11 | create_table :posts do |t| 12 | t.string :name 13 | end 14 | add_index :posts, :name 15 | end 16 | def self.down 17 | drop_table :posts 18 | end 19 | end 20 | EOS 21 | 22 | let(:test_rewritten_content) { <<~EOS } 23 | class CreatePosts < ActiveRecord::Migration 24 | def up 25 | create_table :posts do |t| 26 | t.string :name 27 | end 28 | add_index :posts, :name 29 | end 30 | def down 31 | drop_table :posts 32 | end 33 | end 34 | EOS 35 | 36 | include_examples 'convertable' 37 | end 38 | -------------------------------------------------------------------------------- /spec/redis/deprecate_calling_redis_inside_multi_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Deprecate calling redis inside Redis#multi' do 6 | let(:rewriter_name) { 'redis/deprecate_calling_redis_inside_multi' } 7 | let(:fake_file_path) { 'app/models/user.rb' } 8 | let(:test_content) { <<~EOS } 9 | redis.multi do 10 | redis.get("key") 11 | end 12 | 13 | redis.multi do |transaction| 14 | transaction.get("key") 15 | end 16 | EOS 17 | let(:test_rewritten_content) { <<~EOS } 18 | redis.multi do |transaction| 19 | transaction.get("key") 20 | end 21 | 22 | redis.multi do |transaction| 23 | transaction.get("key") 24 | end 25 | EOS 26 | 27 | include_examples 'convertable' 28 | end 29 | -------------------------------------------------------------------------------- /spec/redis/deprecate_calling_redis_inside_pipelined_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Deprecate calling redis inside Redis#pipelined' do 6 | let(:rewriter_name) { 'redis/deprecate_calling_redis_inside_pipelined' } 7 | let(:fake_file_path) { 'app/models/user.rb' } 8 | let(:test_content) { <<~EOS } 9 | redis.pipelined do 10 | redis.get("key") 11 | end 12 | 13 | redis.pipelined do |pipeline| 14 | pipeline.get("key") 15 | end 16 | EOS 17 | let(:test_rewritten_content) { <<~EOS } 18 | redis.pipelined do |pipeline| 19 | pipeline.get("key") 20 | end 21 | 22 | redis.pipelined do |pipeline| 23 | pipeline.get("key") 24 | end 25 | EOS 26 | 27 | include_examples 'convertable' 28 | end 29 | -------------------------------------------------------------------------------- /spec/rspec/be_close_to_be_within_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'RSpec converts be_close to be_within' do 6 | let(:rewriter_name) { 'rspec/be_close_to_be_within' } 7 | let(:fake_file_path) { 'spec/models/post_spec.rb' } 8 | let(:test_content) { <<~EOS } 9 | describe Post do 10 | it 'test' do 11 | expect(1.0 / 3.0).to be_close(0.333, 0.001) 12 | end 13 | end 14 | EOS 15 | 16 | let(:test_rewritten_content) { <<~EOS } 17 | describe Post do 18 | it 'test' do 19 | expect(1.0 / 3.0).to be_within(0.001).of(0.333) 20 | end 21 | end 22 | EOS 23 | 24 | include_examples 'convertable' 25 | end 26 | -------------------------------------------------------------------------------- /spec/rspec/block_to_expect_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'RSpec converts block to expect' do 6 | let(:rewriter_name) { 'rspec/block_to_expect' } 7 | let(:fake_file_path) { 'spec/models/post_spec.rb' } 8 | let(:test_content) { <<~EOS } 9 | describe Post do 10 | it 'test' do 11 | lambda { do_something }.should raise_error 12 | proc { do_something }.should raise_error 13 | -> { do_something }.should raise_error 14 | end 15 | end 16 | EOS 17 | 18 | let(:test_rewritten_content) { <<~EOS } 19 | describe Post do 20 | it 'test' do 21 | expect { do_something }.to raise_error 22 | expect { do_something }.to raise_error 23 | expect { do_something }.to raise_error 24 | end 25 | end 26 | EOS 27 | 28 | include_examples 'convertable' 29 | end 30 | -------------------------------------------------------------------------------- /spec/rspec/boolean_matcher_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'RSpec converts boolean matcher' do 6 | let(:rewriter_name) { 'rspec/boolean_matcher' } 7 | let(:fake_file_path) { 'spec/models/post_spec.rb' } 8 | let(:test_content) { <<~EOS } 9 | describe Post do 10 | it 'case' do 11 | expect(obj).to be_true 12 | expect(obj).to be_false 13 | end 14 | end 15 | EOS 16 | 17 | let(:test_rewritten_content) { <<~EOS } 18 | describe Post do 19 | it 'case' do 20 | expect(obj).to be_truthy 21 | expect(obj).to be_falsey 22 | end 23 | end 24 | EOS 25 | 26 | include_examples 'convertable' 27 | end 28 | -------------------------------------------------------------------------------- /spec/rspec/collection_matcher_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'RSpec converts collection matcher' do 6 | let(:rewriter_name) { 'rspec/collection_matcher' } 7 | let(:fake_file_path) { 'spec/models/post_spec.rb' } 8 | let(:test_content) { <<~EOS } 9 | describe Post do 10 | it 'test' do 11 | expect(collection).to have(3).items 12 | expect(collection).to have_exactly(3).items 13 | expect(collection).to have_at_least(3).items 14 | expect(collection).to have_at_most(3).items 15 | 16 | expect(team).to have(3).players 17 | end 18 | end 19 | EOS 20 | 21 | let(:test_rewritten_content) { <<~EOS } 22 | describe Post do 23 | it 'test' do 24 | expect(collection.size).to eq 3 25 | expect(collection.size).to eq 3 26 | expect(collection.size).to be >= 3 27 | expect(collection.size).to be <= 3 28 | 29 | expect(team.players.size).to eq 3 30 | end 31 | end 32 | EOS 33 | 34 | include_examples 'convertable' 35 | end 36 | -------------------------------------------------------------------------------- /spec/rspec/custom_matcher_new_syntax_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Use RSpec custom matcher new syntax' do 6 | let(:rewriter_name) { 'rspec/custom_matcher_new_syntax' } 7 | let(:fake_file_path) { 'spec/models/post_spec.rb' } 8 | let(:test_content) { <<~EOS } 9 | RSpec::Matchers.define :be_awesome do 10 | match_for_should { } 11 | match_for_should_not { } 12 | failure_message_for_should { } 13 | failure_message_for_should_not { } 14 | end 15 | EOS 16 | 17 | let(:test_rewritten_content) { <<~EOS } 18 | RSpec::Matchers.define :be_awesome do 19 | match { } 20 | match_when_negated { } 21 | failure_message { } 22 | failure_message_when_negated { } 23 | end 24 | EOS 25 | 26 | include_examples 'convertable' 27 | end 28 | -------------------------------------------------------------------------------- /spec/rspec/its_to_it_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'RSpec converts its to it' do 6 | let(:rewriter_name) { 'rspec/its_to_it' } 7 | let(:fake_file_path) { 'spec/models/post_spec.rb' } 8 | let(:test_content) { <<~EOS } 9 | describe Post do 10 | describe 'example' do 11 | subject { { foo: 1, bar: 2 } } 12 | its(:size) { should == 2 } 13 | its([:foo]) { should == 1 } 14 | its('keys.first') { should == :foo } 15 | end 16 | end 17 | EOS 18 | 19 | let(:test_rewritten_content) { <<~EOS } 20 | describe Post do 21 | describe 'example' do 22 | subject { { foo: 1, bar: 2 } } 23 | describe '#size' do 24 | subject { super().size } 25 | it { should == 2 } 26 | end 27 | describe '[:foo]' do 28 | subject { super()[:foo] } 29 | it { should == 1 } 30 | end 31 | describe '#keys' do 32 | subject { super().keys } 33 | describe '#first' do 34 | subject { super().first } 35 | it { should == :foo } 36 | end 37 | end 38 | end 39 | end 40 | EOS 41 | 42 | include_examples 'convertable' 43 | end 44 | -------------------------------------------------------------------------------- /spec/rspec/message_expectation_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'RSpec converts message expectation' do 6 | let(:rewriter_name) { 'rspec/message_expectation' } 7 | let(:fake_file_path) { 'spec/models/post_spec.rb' } 8 | let(:test_content) { <<~EOS } 9 | describe Post do 10 | it 'test' do 11 | obj.should_receive(:message) 12 | Klass.any_instance.should_receive(:message) 13 | obj.should_not_receive(:message) 14 | Klass.any_instance.should_not_receive(:message) 15 | 16 | expect(obj).to receive(:message).and_return { 1 } 17 | 18 | expect(obj).to receive(:message).and_return 19 | end 20 | end 21 | EOS 22 | 23 | let(:test_rewritten_content) { <<~EOS } 24 | describe Post do 25 | it 'test' do 26 | expect(obj).to receive(:message) 27 | expect_any_instance_of(Klass).to receive(:message) 28 | expect(obj).not_to receive(:message) 29 | expect_any_instance_of(Klass).not_to receive(:message) 30 | 31 | expect(obj).to receive(:message) { 1 } 32 | 33 | expect(obj).to receive(:message) 34 | end 35 | end 36 | EOS 37 | 38 | include_examples 'convertable' 39 | end 40 | -------------------------------------------------------------------------------- /spec/rspec/negative_error_expectation_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'RSpec converts negative error expectation' do 6 | let(:rewriter_name) { 'rspec/negative_error_expectation' } 7 | let(:fake_file_path) { 'spec/models/post_spec.rb' } 8 | let(:test_content) { <<~EOS } 9 | describe Post do 10 | it 'test' do 11 | expect { do_something }.not_to raise_error(SomeErrorClass) 12 | expect { do_something }.not_to raise_error('message') 13 | expect { do_something }.not_to raise_error(SomeErrorClass, 'message') 14 | end 15 | end 16 | EOS 17 | 18 | let(:test_rewritten_content) { <<~EOS } 19 | describe Post do 20 | it 'test' do 21 | expect { do_something }.not_to raise_error 22 | expect { do_something }.not_to raise_error 23 | expect { do_something }.not_to raise_error 24 | end 25 | end 26 | EOS 27 | 28 | include_examples 'convertable' 29 | end 30 | -------------------------------------------------------------------------------- /spec/rspec/new_hook_scope_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'RSpec uses new hook scope' do 6 | let(:rewriter_name) { 'rspec/new_hook_scope' } 7 | 8 | context 'unit test' do 9 | let(:fake_file_path) { 'spec/models/post_spec.rb' } 10 | let(:test_content) { <<~EOS } 11 | describe 'example' do 12 | before { do_something } 13 | before(:each) { do_something } 14 | before(:all) { do_something } 15 | end 16 | EOS 17 | 18 | let(:test_rewritten_content) { <<~EOS } 19 | describe 'example' do 20 | before { do_something } 21 | before(:example) { do_something } 22 | before(:context) { do_something } 23 | end 24 | EOS 25 | 26 | include_examples 'convertable' 27 | end 28 | 29 | context 'spec_helper' do 30 | let(:fake_file_path) { 'spec/spec_helper.rb' } 31 | let(:test_content) { <<~EOS } 32 | RSpec.configure do |config| 33 | config.before(:suite) { do_something } 34 | end 35 | EOS 36 | 37 | let(:test_rewritten_content) { <<~EOS } 38 | RSpec.configure do |config| 39 | config.before(:suite) { do_something } 40 | end 41 | EOS 42 | 43 | include_examples 'convertable' 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /spec/rspec/one_liner_expectation_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'RSpec converts one liner expectation' do 6 | let(:rewriter_name) { 'rspec/one_liner_expectation' } 7 | let(:fake_file_path) { 'spec/models/post_spec.rb' } 8 | let(:test_content) { <<~EOS } 9 | describe Post do 10 | it { should matcher } 11 | it { should_not matcher } 12 | 13 | it { should have(3).items } 14 | it { should have_at_least(3).players } 15 | end 16 | EOS 17 | 18 | let(:test_rewritten_content) { <<~EOS } 19 | describe Post do 20 | it { is_expected.to matcher } 21 | it { is_expected.not_to matcher } 22 | 23 | it 'has 3 items' do 24 | expect(subject.size).to eq(3) 25 | end 26 | it 'has at least 3 players' do 27 | expect(subject.players.size).to be >= 3 28 | end 29 | end 30 | EOS 31 | 32 | include_examples 'convertable' 33 | end 34 | -------------------------------------------------------------------------------- /spec/rspec/pending_to_skip_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'RSpec converts pending to skip' do 6 | let(:rewriter_name) { 'rspec/pending_to_skip' } 7 | let(:fake_file_path) { 'spec/models/post_spec.rb' } 8 | let(:test_content) { <<~EOS } 9 | describe 'example' do 10 | it 'is skipped', :pending => true do 11 | do_something_possibly_fail 12 | end 13 | 14 | pending 'is skipped' do 15 | do_something_possibly_fail 16 | end 17 | 18 | it 'is skipped' do 19 | pending 20 | do_something_possibly_fail 21 | end 22 | 23 | it 'is run and expected to fail' do 24 | pending do 25 | do_something_surely_fail 26 | end 27 | end 28 | end 29 | EOS 30 | 31 | let(:test_rewritten_content) { <<~EOS } 32 | describe 'example' do 33 | it 'is skipped', :skip => true do 34 | do_something_possibly_fail 35 | end 36 | 37 | skip 'is skipped' do 38 | do_something_possibly_fail 39 | end 40 | 41 | it 'is skipped' do 42 | skip 43 | do_something_possibly_fail 44 | end 45 | 46 | it 'is run and expected to fail' do 47 | skip 48 | do_something_surely_fail 49 | end 50 | end 51 | EOS 52 | 53 | include_examples 'convertable' 54 | end 55 | -------------------------------------------------------------------------------- /spec/rspec/should_to_expect_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'RSpec converts should to expect' do 6 | let(:rewriter_name) { 'rspec/should_to_expect' } 7 | let(:fake_file_path) { 'spec/models/post_spec.rb' } 8 | let(:test_content) { <<~EOS } 9 | describe Post do 10 | it 'test' do 11 | obj.should matcher 12 | obj.should_not matcher 13 | 14 | 1.should == 1 15 | 1.should < 2 16 | Integer.should === 1 17 | 'string'.should =~ /^str/ 18 | [1, 2, 3].should =~ [2, 1, 3] 19 | end 20 | end 21 | EOS 22 | 23 | let(:test_rewritten_content) { <<~EOS } 24 | describe Post do 25 | it 'test' do 26 | expect(obj).to matcher 27 | expect(obj).not_to matcher 28 | 29 | expect(1).to eq 1 30 | expect(1).to be < 2 31 | expect(Integer).to be === 1 32 | expect('string').to match /^str/ 33 | expect([1, 2, 3]).to match_array [2, 1, 3] 34 | end 35 | end 36 | EOS 37 | 38 | include_examples 'convertable' 39 | end 40 | -------------------------------------------------------------------------------- /spec/rspec/stub_and_mock_to_double_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'RSpec converts stub and mock to double' do 6 | let(:rewriter_name) { 'rspec/stub_and_mock_to_double' } 7 | let(:fake_file_path) { 'spec/models/post_spec.rb' } 8 | let(:test_content) { <<~EOS } 9 | describe Post do 10 | it 'test' do 11 | stub('something') 12 | mock('something') 13 | end 14 | end 15 | EOS 16 | 17 | let(:test_rewritten_content) { <<~EOS } 18 | describe Post do 19 | it 'test' do 20 | double('something') 21 | double('something') 22 | end 23 | end 24 | EOS 25 | 26 | include_examples 'convertable' 27 | end 28 | -------------------------------------------------------------------------------- /spec/ruby/block_to_yield_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Ruby converts block to yield' do 6 | let(:rewriter_name) { 'ruby/block_to_yield' } 7 | let(:test_content) { <<~EOS } 8 | def test(&block) 9 | block.call 10 | end 11 | 12 | def test(foo, bar, &block) 13 | block.call(foo, bar) 14 | end 15 | EOS 16 | 17 | let(:test_rewritten_content) { <<~EOS } 18 | def test 19 | yield 20 | end 21 | 22 | def test(foo, bar) 23 | yield(foo, bar) 24 | end 25 | EOS 26 | 27 | include_examples 'convertable' 28 | end 29 | -------------------------------------------------------------------------------- /spec/ruby/deprecate_big_decimal_new_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Deprecate BigDecimal.new' do 6 | let(:rewriter_name) { 'ruby/deprecate_big_decimal_new' } 7 | let(:fake_file_path) { 'foobar.rb' } 8 | let(:test_content) { 'BigDecimal.new("1.1")' } 9 | let(:test_rewritten_content) { 'BigDecimal("1.1")' } 10 | 11 | include_examples 'convertable' 12 | end 13 | -------------------------------------------------------------------------------- /spec/ruby/deprecate_dir_exists.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Deprecate Dir.exists?' do 6 | let(:rewriter_name) { 'ruby/deprecate_dir_exists' } 7 | let(:fake_file_path) { 'foobar.rb' } 8 | let(:test_content) { 'Dir.exists?(path)' } 9 | let(:test_rewritten_content) { 'Dir.exist?(path)' } 10 | 11 | include_examples 'convertable' 12 | end 13 | -------------------------------------------------------------------------------- /spec/ruby/deprecate_file_exists.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Deprecate File.exists?' do 6 | let(:rewriter_name) { 'ruby/deprecate_file_exists' } 7 | let(:fake_file_path) { 'foobar.rb' } 8 | let(:test_content) { 'File.exists?(path)' } 9 | let(:test_rewritten_content) { 'File.exist?(path)' } 10 | 11 | include_examples 'convertable' 12 | end 13 | -------------------------------------------------------------------------------- /spec/ruby/deprecate_fixnum_and_bignum.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Deprecate Fixnum and Bignum' do 6 | let(:rewriter_name) { 'ruby/deprecate_fixnum_and_bignum' } 7 | let(:fake_file_path) { 'foobar.rb' } 8 | let(:test_content) { <<~EOS } 9 | Fixnum 10 | Bignum 11 | EOS 12 | let(:test_rewritten_content) { <<~EOS } 13 | Integer 14 | Integer 15 | EOS 16 | 17 | include_examples 'convertable' 18 | end 19 | -------------------------------------------------------------------------------- /spec/ruby/frozen_string_literal_comment_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Ruby adds frozen_string_literal: true' do 6 | context 'frozen_string_literal: true does not exist' do 7 | let(:rewriter_name) { 'ruby/frozen_string_literal_comment' } 8 | let(:test_content) { "'hello world'" } 9 | let(:test_rewritten_content) { "# frozen_string_literal: true\n\n'hello world'" } 10 | 11 | include_examples 'convertable' 12 | end 13 | 14 | context 'frozen_string_literal: true exists' do 15 | let(:rewriter_name) { 'ruby/frozen_string_literal_comment' } 16 | let(:test_content) { "# frozen_string_literal: true\n\n'hello world'" } 17 | let(:test_rewritten_content) { "# frozen_string_literal: true\n\n'hello world'" } 18 | 19 | include_examples 'convertable' 20 | end 21 | end 22 | -------------------------------------------------------------------------------- /spec/ruby/gsub_to_tr_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Ruby converts gsub to tr' do 6 | let(:rewriter_name) { 'ruby/gsub_to_tr' } 7 | let(:test_content) { "'slug from title'.gsub(' ', '_')" } 8 | let(:test_rewritten_content) { "'slug from title'.tr(' ', '_')" } 9 | 10 | include_examples 'convertable' 11 | end 12 | -------------------------------------------------------------------------------- /spec/ruby/kernel_open_to_uri_open_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Ruby converts Kernel#open to URI.open' do 6 | context 'convert' do 7 | let(:rewriter_name) { 'ruby/kernel_open_to_uri_open' } 8 | let(:test_content) { "open('http://test.com')" } 9 | let(:test_rewritten_content) { "URI.open('http://test.com')" } 10 | 11 | include_examples 'convertable' 12 | end 13 | 14 | context 'not convert when define open method' do 15 | let(:rewriter_name) { 'ruby/kernel_open_to_uri_open' } 16 | let(:test_content) {<<~EOS} 17 | def open(url) 18 | end 19 | 20 | open('http://test.com') 21 | EOS 22 | let(:test_rewritten_content) {<<~EOS} 23 | def open(url) 24 | end 25 | 26 | open('http://test.com') 27 | EOS 28 | 29 | include_examples 'convertable' 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /spec/ruby/keys_each_to_each_key_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Ruby .keys.each to .each_key' do 6 | let(:rewriter_name) { 'ruby/keys_each_to_each_key' } 7 | let(:test_content) { <<~EOS } 8 | params.keys.each do |param| 9 | end 10 | EOS 11 | 12 | let(:test_rewritten_content) { <<~EOS } 13 | params.each_key do |param| 14 | end 15 | EOS 16 | 17 | include_examples 'convertable' 18 | end 19 | -------------------------------------------------------------------------------- /spec/ruby/map_and_flatten_to_flat_map_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Ruby converts map_and_flatten_to_flat_map' do 6 | let(:rewriter_name) { 'ruby/map_and_flatten_to_flat_map' } 7 | let(:test_content) { <<~EOS } 8 | enum.map do 9 | # do something 10 | end.flatten 11 | EOS 12 | 13 | let(:test_rewritten_content) { <<~EOS } 14 | enum.flat_map do 15 | # do something 16 | end 17 | EOS 18 | 19 | include_examples 'convertable' 20 | end 21 | -------------------------------------------------------------------------------- /spec/ruby/merge_to_square_brackets_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Ruby converts merge or merge! to []' do 6 | let(:rewriter_name) { 'ruby/merge_to_square_brackets' } 7 | let(:test_content) { <<~EOS } 8 | enum.inject({}) do |h, e| 9 | h.merge(e => e) 10 | end 11 | 12 | enum.inject({}) { |h, e| h.merge!(e => e) } 13 | 14 | enum.each_with_object({}) do |e, h| 15 | h.merge(e => e) 16 | end 17 | 18 | enum.each_with_object({}) { |e, h| h.merge!(e => e) } 19 | 20 | params.merge!(:a => 'b') 21 | params.merge!(a: 'b') 22 | EOS 23 | 24 | let(:test_rewritten_content) { <<~EOS } 25 | enum.inject({}) do |h, e| 26 | h[e] = e 27 | h 28 | end 29 | 30 | enum.inject({}) { |h, e| h[e] = e; h } 31 | 32 | enum.each_with_object({}) do |e, h| 33 | h[e] = e 34 | end 35 | 36 | enum.each_with_object({}) { |e, h| h[e] = e } 37 | 38 | params[:a] = 'b' 39 | params[:a] = 'b' 40 | EOS 41 | 42 | include_examples 'convertable' 43 | end 44 | -------------------------------------------------------------------------------- /spec/ruby/nested_class_definition_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'nested class definition' do 6 | let(:rewriter_name) { 'ruby/nested_class_definition' } 7 | 8 | context 'single module' do 9 | let(:test_content) { <<~EOS } 10 | class Foo::Bar < Base 11 | def test; end 12 | end 13 | EOS 14 | 15 | let(:test_rewritten_content) { <<~EOS } 16 | module Foo 17 | class Bar < Base 18 | def test; end 19 | end 20 | end 21 | EOS 22 | 23 | include_examples 'convertable' 24 | end 25 | 26 | context 'nested modules' do 27 | let(:test_content) { <<~EOS } 28 | class Foo::Bar::FooBar < Base 29 | def test; end 30 | end 31 | EOS 32 | 33 | let(:test_rewritten_content) { <<~EOS } 34 | module Foo 35 | module Bar 36 | class FooBar < Base 37 | def test; end 38 | end 39 | end 40 | end 41 | EOS 42 | 43 | include_examples 'convertable' 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /spec/ruby/new_1_9_hash_syntax_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Uses ruby 1.9 new hash synax' do 6 | let(:rewriter_name) { 'ruby/new_1_9_hash_syntax' } 7 | 8 | let(:test_content) { <<~EOS } 9 | { :foo => 'bar', 'foo' => 'bar' } 10 | { :key1 => 'value1', :key2 => 'value2' } 11 | { foo_key: 'foo_value', bar_key: 42 } 12 | { :'foo-key' => 'foo_value', :'bar-key' => 42 } 13 | EOS 14 | 15 | let(:test_rewritten_content) { <<~EOS } 16 | { foo: 'bar', 'foo' => 'bar' } 17 | { key1: 'value1', key2: 'value2' } 18 | { foo_key: 'foo_value', bar_key: 42 } 19 | { :'foo-key' => 'foo_value', :'bar-key' => 42 } 20 | EOS 21 | 22 | include_examples 'convertable' 23 | end 24 | -------------------------------------------------------------------------------- /spec/ruby/new_2_2_hash_syntax_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Uses ruby 2.2 new hash synax' do 6 | let(:rewriter_name) { 'ruby/new_2_2_hash_syntax' } 7 | let(:test_content) { <<~'EOS' } 8 | { :foo => 'bar', 'foo' => 'bar' } 9 | { :key1 => 'value1', :key2 => 'value2' } 10 | { foo_key: 'foo_value', bar_key: 42, "baz-key" => true } 11 | { :"foo-#{key}" => 'foo_value', :"bar-key" => 42, :"a\tb" => false, :"c'd" => nil } 12 | { "foo-#{key}": 'foo_value', 'bar-key': 42, "a\tb": false, "c'd": nil } 13 | EOS 14 | 15 | let(:test_rewritten_content) { <<~'EOS' } 16 | { foo: 'bar', 'foo' => 'bar' } 17 | { key1: 'value1', key2: 'value2' } 18 | { foo_key: 'foo_value', bar_key: 42, "baz-key" => true } 19 | { "foo-#{key}": 'foo_value', 'bar-key': 42, "a\tb": false, "c'd": nil } 20 | { "foo-#{key}": 'foo_value', 'bar-key': 42, "a\tb": false, "c'd": nil } 21 | EOS 22 | 23 | before do 24 | load_sub_snippets(%w[ruby/new_1_9_hash_syntax]) 25 | end 26 | 27 | include_examples 'convertable' 28 | end 29 | -------------------------------------------------------------------------------- /spec/ruby/new_lambda_syntax_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Ruby uses new -> synax' do 6 | let(:rewriter_name) { 'ruby/new_lambda_syntax' } 7 | let(:test_content) { <<~EOS } 8 | lambda { test } 9 | lambda { |a, b, c| a + b + c } 10 | EOS 11 | 12 | let(:test_rewritten_content) { <<~EOS } 13 | -> { test } 14 | ->(a, b, c) { a + b + c } 15 | EOS 16 | 17 | include_examples 'convertable' 18 | end 19 | -------------------------------------------------------------------------------- /spec/ruby/new_safe_navigation_operator_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Ruby uses new safe navigation operator', 6 | skip: Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.3.0') do 7 | let(:rewriter_name) { 'ruby/new_safe_navigation_operator' } 8 | 9 | context 'with arguments' do 10 | let(:test_content) { <<~EOS } 11 | u = User.find(id) 12 | u.try!(:profile).try!(:thumbnails).try!(:large, 100, format: 'jpg') 13 | u.try!('profile').try!('thumbnails').try!('large', 100, format: 'jpg') 14 | u.try(:profile).try(:thumbnails).try(:large, 100, format: 'jpg') 15 | u.try('profile').try('thumbnails').try('large', 100, format: 'jpg') 16 | EOS 17 | 18 | let(:test_rewritten_content) { <<~EOS } 19 | u = User.find(id) 20 | u&.profile&.thumbnails&.large(100, format: 'jpg') 21 | u&.profile&.thumbnails&.large(100, format: 'jpg') 22 | u&.profile&.thumbnails&.large(100, format: 'jpg') 23 | u&.profile&.thumbnails&.large(100, format: 'jpg') 24 | EOS 25 | 26 | include_examples 'convertable' 27 | end 28 | 29 | context 'without arguments' do 30 | let(:test_content) { 'u.try! {|u| do_something(u.profile) }' } 31 | let(:test_rewritten_content) { 'u.try! {|u| do_something(u.profile) }' } 32 | 33 | include_examples 'convertable' 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /spec/ruby/numbered_parameters_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7') 6 | RSpec.describe 'Ruby numbered parameters' do 7 | let(:rewriter_name) { 'ruby/numbered_parameters' } 8 | let(:test_content) { <<~EOS } 9 | squared_numbers = (1...10).map { |num| num ** 2 } 10 | 11 | city_populations.each { |city, population| puts "Population of \#{city} is \#{population}" } 12 | EOS 13 | 14 | let(:test_rewritten_content) { <<~EOS } 15 | squared_numbers = (1...10).map { _1 ** 2 } 16 | 17 | city_populations.each { puts "Population of \#{_1} is \#{_2}" } 18 | EOS 19 | 20 | include_examples 'convertable' 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /spec/ruby/parallel_assignment_to_sequential_assignment_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Ruby converts parallel assignment to sequential assignment' do 6 | let(:rewriter_name) { 'ruby/parallel_assignment_to_sequential_assignment' } 7 | let(:test_content) { <<~EOS } 8 | a, b = 1, 2 9 | a, b = params 10 | EOS 11 | 12 | let(:test_rewritten_content) { <<~EOS } 13 | a = 1 14 | b = 2 15 | a, b = params 16 | EOS 17 | 18 | include_examples 'convertable' 19 | end 20 | -------------------------------------------------------------------------------- /spec/ruby/prefer_dig_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Prefer dig' do 6 | let(:rewriter_name) { 'ruby/prefer_dig' } 7 | let(:fake_file_path) { 'foobar.rb' } 8 | let(:test_content) { <<~EOS } 9 | def action 10 | params[:foo] && params[:foo][:bar] 11 | 12 | params[one] && params[one][two] && params[one][two][three] 13 | 14 | params[one] && params[one][two] && params[one][two][three] && params[one][two][three][four] 15 | end 16 | EOS 17 | let(:test_rewritten_content) { <<~EOS } 18 | def action 19 | params.dig(:foo, :bar) 20 | 21 | params.dig(one, two, three) 22 | 23 | params.dig(one, two, three, four) 24 | end 25 | EOS 26 | 27 | include_examples 'convertable' 28 | end 29 | -------------------------------------------------------------------------------- /spec/ruby/prefer_nil_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Ruby prefer nil?' do 6 | let(:rewriter_name) { 'ruby/prefer_nil' } 7 | let(:fake_file_path) { 'foobar.rb' } 8 | let(:test_content) { <<~EOS } 9 | def test 10 | value1 == nil 11 | value2 != nil 12 | end 13 | EOS 14 | let(:test_rewritten_content) { <<~EOS } 15 | def test 16 | value1.nil? 17 | !value2.nil? 18 | end 19 | EOS 20 | 21 | include_examples 'convertable' 22 | end 23 | -------------------------------------------------------------------------------- /spec/ruby/remove_debug_code_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Ruby removes debug code' do 6 | let(:rewriter_name) { 'ruby/remove_debug_code' } 7 | let(:test_content) { <<~EOS } 8 | def test 9 | puts 'hello world' 10 | p 'debug' 11 | end 12 | EOS 13 | 14 | let(:test_rewritten_content) { <<~EOS } 15 | def test 16 | end 17 | EOS 18 | 19 | include_examples 'convertable' 20 | end 21 | -------------------------------------------------------------------------------- /spec/ruby/shorthand_hash_syntax_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Uses ruby 3.1 shorthand hash synax' do 6 | let(:rewriter_name) { 'ruby/shorthand_hash_syntax' } 7 | 8 | context 'new hash syntax' do 9 | let(:test_content) { <<~EOS } 10 | { a: a, b: b, c: c, d: d + 4 } 11 | some_method(a: a, b: b, c: c, d: d + 4) 12 | some_method(:a => a, :b => b, :c => c, :d => d + 4) 13 | some_method a: a, b: b, c: c, d: d + 4 14 | EOS 15 | 16 | let(:test_rewritten_content) { <<~EOS } 17 | { a:, b:, c:, d: d + 4 } 18 | some_method(a:, b:, c:, d: d + 4) 19 | some_method(:a => a, :b => b, :c => c, :d => d + 4) 20 | some_method a: a, b: b, c: c, d: d + 4 21 | EOS 22 | 23 | include_examples 'convertable' 24 | end 25 | 26 | context 'hash rocket syntax' do 27 | let(:test_content) { <<~EOS } 28 | { :a => a, :b => b, :c => c, :d => d + 4 } 29 | EOS 30 | 31 | let(:test_rewritten_content) { <<~EOS } 32 | { :a => a, :b => b, :c => c, :d => d + 4 } 33 | EOS 34 | 35 | include_examples 'convertable' 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /spec/ruby/uri_escape_to_uri_default_parser_escape_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Uses URI::DEFAULT_PARSER.escape instead of URI.escape' do 6 | let(:rewriter_name) { 'ruby/uri_escape_to_uri_default_parser_escape' } 7 | 8 | let(:test_content) { <<~EOS } 9 | URI.escape(url) 10 | URI.encode(url) 11 | EOS 12 | 13 | let(:test_rewritten_content) { <<~EOS } 14 | URI::DEFAULT_PARSER.escape(url) 15 | URI::DEFAULT_PARSER.escape(url) 16 | EOS 17 | 18 | include_examples 'convertable' 19 | end 20 | -------------------------------------------------------------------------------- /spec/ruby/use_it_keyword_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.4') 6 | RSpec.describe 'Ruby uses it keyword' do 7 | let(:rewriter_name) { 'ruby/use_it_keyword' } 8 | let(:test_content) { <<~EOS } 9 | squared_numbers = (1...10).map { |num| num ** 2 } 10 | squared_numbers = (1...10).map { _1 ** 2 } 11 | 12 | city_populations.each { |city, population| puts "Population of \#{city} is \#{population}" } 13 | city_populations.each { puts "Population of \#{_1} is \#{_2}" } 14 | EOS 15 | 16 | let(:test_rewritten_content) { <<~EOS } 17 | squared_numbers = (1...10).map { it ** 2 } 18 | squared_numbers = (1...10).map { it ** 2 } 19 | 20 | city_populations.each { |city, population| puts "Population of \#{city} is \#{population}" } 21 | city_populations.each { puts "Population of \#{_1} is \#{_2}" } 22 | EOS 23 | 24 | include_examples 'convertable' 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /spec/ruby/use_keyword_arguments_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Ruby uses keyword arguments' do 6 | let(:rewriter_name) { 'ruby/use_keyword_arguments' } 7 | let(:test_content) { <<~EOS } 8 | CSV.generate(options) do |csv| 9 | end 10 | EOS 11 | 12 | let(:test_rewritten_content) { <<~EOS } 13 | CSV.generate(**options) do |csv| 14 | end 15 | EOS 16 | 17 | include_examples 'convertable' 18 | end 19 | -------------------------------------------------------------------------------- /spec/ruby/use_symbol_to_proc_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Ruby uses symbol to proc' do 6 | let(:rewriter_name) { 'ruby/use_symbol_to_proc' } 7 | let(:test_content) { <<~EOS } 8 | (1..100).map { |i| i.to_s } 9 | enum.map { |e| e[:object_id] } 10 | (1..100).each { |i| i.to_s } 11 | EOS 12 | 13 | let(:test_rewritten_content) { <<~EOS } 14 | (1..100).map(&:to_s) 15 | enum.map { |e| e[:object_id] } 16 | (1..100).each(&:to_s) 17 | EOS 18 | 19 | include_examples 'convertable' 20 | end 21 | -------------------------------------------------------------------------------- /spec/shoulda/fix_2_6_deprecations_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe 'Fix shoulda 2.6 deprecations' do 6 | let(:rewriter_name) { 'shoulda/fix_2_6_deprecations' } 7 | 8 | context 'unit test methods' do 9 | let(:fake_file_path) { 'test/unit/post_test.rb' } 10 | let(:test_content) { <<~EOS } 11 | class PostTest < ActiveSupport::TestCase 12 | should ensure_inclusion_of(:age).in_range(0..100) 13 | 14 | should ensure_exclusion_of(:age).in_range(30..60) 15 | end 16 | EOS 17 | 18 | let(:test_rewritten_content) { <<~EOS } 19 | class PostTest < ActiveSupport::TestCase 20 | should validate_inclusion_of(:age).in_range(0..100) 21 | 22 | should validate_exclusion_of(:age).in_range(30..60) 23 | end 24 | EOS 25 | 26 | include_examples 'convertable' 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) 4 | 5 | require 'pp' # rubocop:disable Lint/RedundantRequireStatement 6 | require 'synvert/core' 7 | require 'fakefs/spec_helpers' 8 | 9 | Dir[File.expand_path(File.join(File.dirname(__FILE__), 'support/*.rb'))].each { |f| require f } 10 | 11 | RSpec.configure do |config| 12 | config.include FakeFS::SpecHelpers, fakefs: true 13 | 14 | config.run_all_when_everything_filtered = true 15 | config.filter_run :focus 16 | 17 | config.order = 'random' 18 | 19 | config.before do 20 | allow_any_instance_of(Synvert::Core::Rewriter::GemSpec).to receive(:match?).and_return(true) 21 | end 22 | config.expose_dsl_globally = false 23 | end 24 | -------------------------------------------------------------------------------- /spec/support/warnable.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | shared_examples 'warnable' do 4 | # it needs to define rewriter_name, fake_file_path (optional), test_content, warnings 5 | let!(:rewriter) { load_snippet(rewriter_name) } 6 | 7 | describe 'with fakefs', fakefs: true do 8 | it 'converts' do 9 | file_paths.each_with_index do |file_path, index| 10 | FileUtils.mkdir_p(File.dirname(file_path)) 11 | File.write(file_path, test_contents[index]) 12 | end 13 | rewriter.process 14 | expect(rewriter.warnings.map(&:message)).to eq(warnings) 15 | end 16 | end 17 | end 18 | --------------------------------------------------------------------------------