Skip to content

Commit

Permalink
Merge pull request #772 from koic/fix_a_false_positive_for_rails_top_…
Browse files Browse the repository at this point in the history
…level_hash_with_indifferent_access

Fix a false positive for `Rails/TopLevelHashWithIndifferentAccess`
  • Loading branch information
koic committed Sep 15, 2022
2 parents 6b5424e + f89c1d3 commit 9aa85f1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
@@ -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][])
Expand Up @@ -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)
Expand Down
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 9aa85f1

Please sign in to comment.