From 6175e0c695f3b62b75cd19c718faad8480a1e316 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 27 Aug 2022 00:32:23 +0900 Subject: [PATCH] [Fix #757] Fix a false positive for `Rails/ReflectionClassName` Fixes #757. This PR fixes a false positive for `Rails/ReflectionClassName` when using Ruby 3.1's hash shorthand syntax. --- ...a_false_positive_for_rails_reflection_class_name.md | 1 + lib/rubocop/cop/rails/reflection_class_name.rb | 2 ++ spec/rubocop/cop/rails/reflection_class_name_spec.rb | 10 ++++++++++ 3 files changed, 13 insertions(+) create mode 100644 changelog/fix_a_false_positive_for_rails_reflection_class_name.md diff --git a/changelog/fix_a_false_positive_for_rails_reflection_class_name.md b/changelog/fix_a_false_positive_for_rails_reflection_class_name.md new file mode 100644 index 0000000000..9bbd3a3649 --- /dev/null +++ b/changelog/fix_a_false_positive_for_rails_reflection_class_name.md @@ -0,0 +1 @@ +* [#757](https://github.com/rubocop/rubocop-rails/issues/757): Fix a false positive for `Rails/ReflectionClassName` when using Ruby 3.1's hash shorthand syntax. ([@koic][]) diff --git a/lib/rubocop/cop/rails/reflection_class_name.rb b/lib/rubocop/cop/rails/reflection_class_name.rb index 2de89b84ac..ebf114ec7f 100644 --- a/lib/rubocop/cop/rails/reflection_class_name.rb +++ b/lib/rubocop/cop/rails/reflection_class_name.rb @@ -34,6 +34,8 @@ class ReflectionClassName < Base def on_send(node) association_with_reflection(node) do |reflection_class_name| + next if reflection_class_name.value_omission? + add_offense(reflection_class_name.loc.expression) end end diff --git a/spec/rubocop/cop/rails/reflection_class_name_spec.rb b/spec/rubocop/cop/rails/reflection_class_name_spec.rb index 51caf666f7..1e6f207b95 100644 --- a/spec/rubocop/cop/rails/reflection_class_name_spec.rb +++ b/spec/rubocop/cop/rails/reflection_class_name_spec.rb @@ -80,4 +80,14 @@ belongs_to :account, class_name: :Account RUBY end + + context 'Ruby >= 3.1', :ruby31 do + it 'does not register an offense when using shorthand syntax' do + expect_no_offenses(<<~RUBY) + class_name = 'Account' + + has_many :accounts, class_name: + RUBY + end + end end