From a9eb6aeb4d8aaabebc93ccf56e2f346806807da4 Mon Sep 17 00:00:00 2001 From: Philip Mueller Date: Thu, 9 Nov 2023 20:18:40 -0500 Subject: [PATCH] [Fix #1179] accept method calls on local variables in Rails/ReflectionClassName --- changelog/change_accept_method_calls_on_local_variables.md | 1 + lib/rubocop/cop/rails/reflection_class_name.rb | 2 +- spec/rubocop/cop/rails/reflection_class_name_spec.rb | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 changelog/change_accept_method_calls_on_local_variables.md diff --git a/changelog/change_accept_method_calls_on_local_variables.md b/changelog/change_accept_method_calls_on_local_variables.md new file mode 100644 index 0000000000..f6afaecd35 --- /dev/null +++ b/changelog/change_accept_method_calls_on_local_variables.md @@ -0,0 +1 @@ +* [#1179](https://github.com/rubocop/rubocop-rails/issues/1179): `Rails/ReflectionClassName`: Accept method calls on local variables. ([@exterm][]) diff --git a/lib/rubocop/cop/rails/reflection_class_name.rb b/lib/rubocop/cop/rails/reflection_class_name.rb index d0afa1acb6..9f5637f8e7 100644 --- a/lib/rubocop/cop/rails/reflection_class_name.rb +++ b/lib/rubocop/cop/rails/reflection_class_name.rb @@ -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| diff --git a/spec/rubocop/cop/rails/reflection_class_name_spec.rb b/spec/rubocop/cop/rails/reflection_class_name_spec.rb index a1e41ed132..a849cf3d69 100644 --- a/spec/rubocop/cop/rails/reflection_class_name_spec.rb +++ b/spec/rubocop/cop/rails/reflection_class_name_spec.rb @@ -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: 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)