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

ReflectionClassName: accept method calls on local variables #1180

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions changelog/change_accept_method_calls_on_local_variables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1179](https://github.com/rubocop/rubocop-rails/issues/1179): `Rails/ReflectionClassName`: Accept method calls on local variables. ([@exterm][])
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 an offense when parameter value is a method call on an object in a variable' do
expect_no_offenses(<<~RUBY)
has_many :accounts, class_name: some_thing.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