Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a false positive for Rails/TopLevelHashWithIndifferentAccess #772

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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