From 68372fb8a9ddc6a80b7f30b77b23735a9aaec6c6 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 31 Aug 2021 12:27:37 +0900 Subject: [PATCH] [Fix #528] Fix a false positive for `Rails/HasManyOrHasOneDependent` Fixes #528. This PR fixes a false positive for `Rails/HasManyOrHasOneDependent` when specifying `:dependent` strategy with double splat. --- CHANGELOG.md | 4 ++++ lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb | 2 ++ .../cop/rails/has_many_or_has_one_dependent_spec.rb | 8 ++++++++ 3 files changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e20bfa2a2..5611f5626e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ * [#520](https://github.com/rubocop/rubocop-rails/pull/520): Support auto-correction for `Rails/ScopeArgs`. ([@koic][]) * [#524](https://github.com/rubocop/rubocop-rails/pull/524): Add new `Rails/RedundantTravelBack` cop. ([@koic][]) +### Bug fixes + +* [#528](https://github.com/rubocop/rubocop-rails/issues/528): Fix a false positive for `Rails/HasManyOrHasOneDependent` when specifying `:dependent` strategy with double splat. ([@koic][]) + ## Changes * [#260](https://github.com/rubocop/rubocop-rails/issues/260): Change target of `Rails/ContentTag` from `content_tag` method to `tag` method. ([@tabuchi0919][]) diff --git a/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb b/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb index 47b9dd8423..3a6d5aed22 100644 --- a/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +++ b/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb @@ -114,6 +114,8 @@ def contain_valid_options_in_with_options_block?(node) end def valid_options?(options) + options = options.first.children.first.pairs if options.first.kwsplat_type? + return true unless options return true if options.any? do |o| dependent_option?(o) || present_option?(o) diff --git a/spec/rubocop/cop/rails/has_many_or_has_one_dependent_spec.rb b/spec/rubocop/cop/rails/has_many_or_has_one_dependent_spec.rb index f4191b56d4..b22e4013df 100644 --- a/spec/rubocop/cop/rails/has_many_or_has_one_dependent_spec.rb +++ b/spec/rubocop/cop/rails/has_many_or_has_one_dependent_spec.rb @@ -28,6 +28,14 @@ class Person < ApplicationRecord RUBY end + it 'does not register an offense when specifying `:dependent` strategy with double splat' do + expect_no_offenses(<<~RUBY) + class Person < ApplicationRecord + has_one :foo, **{dependent: :destroy} + end + RUBY + end + it 'does not register an offense when specifying default `dependent: nil` strategy' do expect_no_offenses(<<~RUBY) class Person < ApplicationRecord