From 64d596466eefad0ad794f6a6c682afe9e8816adc Mon Sep 17 00:00:00 2001 From: Brian Hawley Date: Wed, 6 Jul 2022 10:28:28 -0700 Subject: [PATCH] Add missing deprecated `errors` methods Fixes for Rails/DeprecatedActiveModelErrorsMethods: - The `values`, `to_h`, and `to_xml` methods are deprecated too. --- .../fix_deprecated_errors_missing_methods.md | 1 + .../deprecated_active_model_errors_methods.rb | 9 +- ...ecated_active_model_errors_methods_spec.rb | 114 ++++++++++++++++++ 3 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 changelog/fix_deprecated_errors_missing_methods.md diff --git a/changelog/fix_deprecated_errors_missing_methods.md b/changelog/fix_deprecated_errors_missing_methods.md new file mode 100644 index 0000000000..3259128e89 --- /dev/null +++ b/changelog/fix_deprecated_errors_missing_methods.md @@ -0,0 +1 @@ +* [#742](https://github.com/rubocop/rubocop-rails/pull/742): Rails/DeprecatedActiveModelErrorsMethods was missing the deprecated `values`, `to_h`, and `to_xml` methods. ([@BrianHawley][]) diff --git a/lib/rubocop/cop/rails/deprecated_active_model_errors_methods.rb b/lib/rubocop/cop/rails/deprecated_active_model_errors_methods.rb index f115174158..2207789da3 100644 --- a/lib/rubocop/cop/rails/deprecated_active_model_errors_methods.rb +++ b/lib/rubocop/cop/rails/deprecated_active_model_errors_methods.rb @@ -38,6 +38,7 @@ class DeprecatedActiveModelErrorsMethods < Base MSG = 'Avoid manipulating ActiveModel errors as hash directly.' AUTOCORECTABLE_METHODS = %i[<< clear keys].freeze + INCOMPATIBLE_METHODS = %i[keys values to_h to_xml].freeze MANIPULATIVE_METHODS = Set[ *%i[ @@ -55,7 +56,7 @@ class DeprecatedActiveModelErrorsMethods < Base { #root_manipulation? #root_assignment? - #errors_keys? + #errors_deprecated? #messages_details_manipulation? #messages_details_assignment? } @@ -77,10 +78,10 @@ class DeprecatedActiveModelErrorsMethods < Base ...) PATTERN - def_node_matcher :errors_keys?, <<~PATTERN + def_node_matcher :errors_deprecated?, <<~PATTERN (send (send #receiver_matcher :errors) - :keys) + {:keys :values :to_h :to_xml}) PATTERN def_node_matcher :messages_details_manipulation?, <<~PATTERN @@ -106,7 +107,7 @@ class DeprecatedActiveModelErrorsMethods < Base def on_send(node) any_manipulation?(node) do - next if node.method?(:keys) && target_rails_version <= 6.0 + next if target_rails_version <= 6.0 && INCOMPATIBLE_METHODS.include?(node.method_name) add_offense(node) do |corrector| next unless AUTOCORECTABLE_METHODS.include?(node.method_name) diff --git a/spec/rubocop/cop/rails/deprecated_active_model_errors_methods_spec.rb b/spec/rubocop/cop/rails/deprecated_active_model_errors_methods_spec.rb index 1ccd7f1e31..b51cca9fa1 100644 --- a/spec/rubocop/cop/rails/deprecated_active_model_errors_methods_spec.rb +++ b/spec/rubocop/cop/rails/deprecated_active_model_errors_methods_spec.rb @@ -64,6 +64,39 @@ RUBY end end + + context 'when using `values` method' do + it 'registers an offense' do + expect_offense(<<~RUBY, file_path) + user.errors.values + ^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. + RUBY + + expect_no_corrections + end + end + + context 'when using `to_h` method' do + it 'registers an offense' do + expect_offense(<<~RUBY, file_path) + user.errors.to_h + ^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. + RUBY + + expect_no_corrections + end + end + + context 'when using `to_xml` method' do + it 'registers an offense' do + expect_offense(<<~RUBY, file_path) + user.errors.to_xml + ^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. + RUBY + + expect_no_corrections + end + end end context 'Rails <= 6.0', :rails60 do @@ -81,6 +114,30 @@ RUBY end end + + context 'when using `values` method' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY, file_path) + user.errors.values + RUBY + end + end + + context 'when using `to_h` method' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY, file_path) + user.errors.to_h + RUBY + end + end + + context 'when using `to_xml` method' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY, file_path) + user.errors.to_xml + RUBY + end + end end end @@ -238,6 +295,39 @@ def expect_no_corrections_if_model_file(file_path) RUBY end end + + context 'when using `values` method' do + it 'registers an offense' do + expect_offense_if_model_file(<<~RUBY, file_path) + errors.values + ^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. + RUBY + + expect_no_corrections_if_model_file(file_path) + end + end + + context 'when using `to_h` method' do + it 'registers an offense' do + expect_offense_if_model_file(<<~RUBY, file_path) + errors.to_h + ^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. + RUBY + + expect_no_corrections_if_model_file(file_path) + end + end + + context 'when using `to_xml` method' do + it 'registers an offense' do + expect_offense_if_model_file(<<~RUBY, file_path) + errors.to_xml + ^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. + RUBY + + expect_no_corrections_if_model_file(file_path) + end + end end context 'Rails <= 6.0', :rails60 do @@ -248,6 +338,30 @@ def expect_no_corrections_if_model_file(file_path) RUBY end end + + context 'when using `values` method' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY, file_path) + user.errors.values + RUBY + end + end + + context 'when using `to_h` method' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY, file_path) + user.errors.to_h + RUBY + end + end + + context 'when using `to_xml` method' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY, file_path) + user.errors.to_xml + RUBY + end + end end context 'when calling non-manipulative methods' do