From f89c1d3040b87ecf1cf42459422282ef8cd2f240 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 13 Sep 2022 12:30:34 +0900 Subject: [PATCH] Fix a false positive for `Rails/TopLevelHashWithIndifferentAccess` This PR fixes a false positive for `Rails/TopLevelHashWithIndifferentAccess` when using `HashWithIndifferentAccess` under namespace module. --- ..._top_level_hash_with_indifferent_access.md | 1 + .../top_level_hash_with_indifferent_access.rb | 1 + ...level_hash_with_indifferent_access_spec.rb | 24 +++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 changelog/fix_a_false_positive_for_rails_top_level_hash_with_indifferent_access.md diff --git a/changelog/fix_a_false_positive_for_rails_top_level_hash_with_indifferent_access.md b/changelog/fix_a_false_positive_for_rails_top_level_hash_with_indifferent_access.md new file mode 100644 index 0000000000..46a58bc2e0 --- /dev/null +++ b/changelog/fix_a_false_positive_for_rails_top_level_hash_with_indifferent_access.md @@ -0,0 +1 @@ +* [#772](https://github.com/rubocop/rubocop-rails/pull/772): Fix a false positive for `Rails/TopLevelHashWithIndifferentAccess` when using `HashWithIndifferentAccess` under namespace module. ([@koic][]) diff --git a/lib/rubocop/cop/rails/top_level_hash_with_indifferent_access.rb b/lib/rubocop/cop/rails/top_level_hash_with_indifferent_access.rb index 2d5ca04db6..4e950e490a 100644 --- a/lib/rubocop/cop/rails/top_level_hash_with_indifferent_access.rb +++ b/lib/rubocop/cop/rails/top_level_hash_with_indifferent_access.rb @@ -31,6 +31,7 @@ class TopLevelHashWithIndifferentAccess < Base # @param [RuboCop::AST::ConstNode] node def on_const(node) return unless top_level_hash_with_indifferent_access?(node) + return if node.parent&.class_type? && node.parent.ancestors.any?(&:module_type?) add_offense(node) do |corrector| autocorrect(corrector, node) diff --git a/spec/rubocop/cop/rails/top_level_hash_with_indifferent_access_spec.rb b/spec/rubocop/cop/rails/top_level_hash_with_indifferent_access_spec.rb index d335a7d68a..753db83a5c 100644 --- a/spec/rubocop/cop/rails/top_level_hash_with_indifferent_access_spec.rb +++ b/spec/rubocop/cop/rails/top_level_hash_with_indifferent_access_spec.rb @@ -27,6 +27,19 @@ end end + context 'with top-level `HashWithIndifferentAccess` without method call' do + it 'registers and corrects an offense' do + expect_offense(<<~RUBY) + HashWithIndifferentAccess + ^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid top-level `HashWithIndifferentAccess`. + RUBY + + expect_correction(<<~RUBY) + ActiveSupport::HashWithIndifferentAccess + RUBY + end + end + context 'with ActiveSupport::HashWithIndifferentAccess' do it 'does not register an offense' do expect_no_offenses(<<~RUBY) @@ -35,6 +48,17 @@ end end + context 'with `HashWithIndifferentAccess` under the namespace' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY) + module CoreExt + class HashWithIndifferentAccess + end + end + RUBY + end + end + context 'with ActiveSupport::HashWithIndifferentAccess on Rails 5.0', :rails50 do it 'does not register an offense' do expect_no_offenses(<<~RUBY)