Skip to content

Commit f44d616

Browse files
authored
Merge pull request #741 from BrianHawley/fix_rails_deprecated_active_model_errors_methods_20220706_2
No correction for `errors.details[:n] << v`
2 parents 77ab1b1 + d0a12a6 commit f44d616

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#741](https://github.com/rubocop/rubocop-rails/pull/741): Fix a bad autocorrection for `errors.details[:name] << value` in Rails/DeprecatedActiveModelErrorsMethods. ([@BrianHawley][])

lib/rubocop/cop/rails/deprecated_active_model_errors_methods.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class DeprecatedActiveModelErrorsMethods < Base
3737
extend AutoCorrector
3838

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

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

111111
add_offense(node) do |corrector|
112-
next unless AUTOCORECTABLE_METHODS.include?(node.method_name)
112+
next if skip_autocorrect?(node)
113113

114114
autocorrect(corrector, node)
115115
end
@@ -118,6 +118,13 @@ def on_send(node)
118118

119119
private
120120

121+
def skip_autocorrect?(node)
122+
return true unless AUTOCORRECTABLE_METHODS.include?(node.method_name)
123+
return false unless (receiver = node.receiver.receiver)
124+
125+
receiver.send_type? && receiver.method?(:details) && node.method?(:<<)
126+
end
127+
121128
def autocorrect(corrector, node)
122129
receiver = node.receiver
123130

spec/rubocop/cop/rails/deprecated_active_model_errors_methods_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@
135135
user.errors.details[:name] << {}
136136
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
137137
RUBY
138+
139+
expect_no_corrections
138140
end
139141

140142
context 'when assigning' do
@@ -318,6 +320,8 @@ def expect_no_corrections_if_model_file(file_path)
318320
errors.details[:name] << {}
319321
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
320322
RUBY
323+
324+
expect_no_corrections_if_model_file(file_path)
321325
end
322326

323327
context 'when assigning' do

0 commit comments

Comments
 (0)