Skip to content

Commit

Permalink
[Fix rubocop#1179] accept method calls on local variables in Rails/Re…
Browse files Browse the repository at this point in the history
…flectionClassName
  • Loading branch information
exterm committed Nov 10, 2023
1 parent dc6cebc commit a95f27c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/reflection_class_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ReflectionClassName < Base

def on_send(node)
association_with_reflection(node) do |reflection_class_name|
return if reflection_class_name.value.send_type? && reflection_class_name.value.receiver.nil?
return if reflection_class_name.value.send_type? && !reflection_class_name.value.receiver&.const_type?
return if reflection_class_name.value.lvar_type? && str_assigned?(reflection_class_name)

add_offense(reflection_class_name.source_range) do |corrector|
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/rails/reflection_class_name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@
RUBY
end

it 'does not register and offense when parameter values is a method call on an object in a variable' do
expect_no_offenses(<<~RUBY)
has_many :accounts, class_name: the_class.name
RUBY
end

context 'Ruby >= 3.1', :ruby31 do
it 'registers an offense when shorthand syntax value is a local variable assigned a constant' do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit a95f27c

Please sign in to comment.