Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No correction for errors.details[:n] << v #741

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#741](https://github.com/rubocop/rubocop-rails/pull/741): Fix a bad autocorrection for `errors.details[:name] << value` in Rails/DeprecatedActiveModelErrorsMethods. ([@BrianHawley][])
11 changes: 9 additions & 2 deletions lib/rubocop/cop/rails/deprecated_active_model_errors_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class DeprecatedActiveModelErrorsMethods < Base
extend AutoCorrector

MSG = 'Avoid manipulating ActiveModel errors as hash directly.'
AUTOCORECTABLE_METHODS = %i[<< clear keys].freeze
AUTOCORRECTABLE_METHODS = %i[<< clear keys].freeze

MANIPULATIVE_METHODS = Set[
*%i[
Expand Down Expand Up @@ -109,7 +109,7 @@ def on_send(node)
next if node.method?(:keys) && target_rails_version <= 6.0

add_offense(node) do |corrector|
next unless AUTOCORECTABLE_METHODS.include?(node.method_name)
next if skip_autocorrect?(node)

autocorrect(corrector, node)
end
Expand All @@ -118,6 +118,13 @@ def on_send(node)

private

def skip_autocorrect?(node)
return true unless AUTOCORRECTABLE_METHODS.include?(node.method_name)
return false unless (receiver = node.receiver.receiver)

receiver.send_type? && receiver.method?(:details) && node.method?(:<<)
end

def autocorrect(corrector, node)
receiver = node.receiver

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@
user.errors.details[:name] << {}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
RUBY

expect_no_corrections
end

context 'when assigning' do
Expand Down Expand Up @@ -318,6 +320,8 @@ def expect_no_corrections_if_model_file(file_path)
errors.details[:name] << {}
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
RUBY

expect_no_corrections_if_model_file(file_path)
end

context 'when assigning' do
Expand Down