-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Rails/TopLevelHashWithIndifferentAccess
cop
- Loading branch information
Showing
5 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
changelog/new_add_rails_top_level_hash_with_indifferent_access.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#751](https://github.com/rubocop/rubocop-rails/pull/751): Add `Rails/TopLevelHashWithIndifferentAccess` cop. ([@r7kamura][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
lib/rubocop/cop/rails/top_level_hash_with_indifferent_access.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Identifies top-level `HashWithIndifferentAccess`. | ||
# This has been soft-deprecated since Rails 5.1. | ||
# | ||
# @example | ||
# # bad | ||
# HashWithIndifferentAccess.new(foo: 'bar') | ||
# | ||
# # good | ||
# ActiveSupport::HashWithIndifferentAccess.new(foo: 'bar') | ||
# | ||
class TopLevelHashWithIndifferentAccess < Base | ||
extend AutoCorrector | ||
extend TargetRailsVersion | ||
|
||
minimum_target_rails_version 5.1 | ||
|
||
MSG = 'Avoid top-level `HashWithIndifferentAccess`.' | ||
|
||
# @!method top_level_hash_with_indifferent_access?(node) | ||
# @param [RuboCop::AST::ConstNode] node | ||
# @return [Boolean] | ||
def_node_matcher :top_level_hash_with_indifferent_access?, <<~PATTERN | ||
(const {nil? cbase} :HashWithIndifferentAccess) | ||
PATTERN | ||
|
||
# @param [RuboCop::AST::ConstNode] node | ||
def on_const(node) | ||
return unless top_level_hash_with_indifferent_access?(node) | ||
|
||
add_offense(node) do |corrector| | ||
autocorrect(corrector, node) | ||
end | ||
end | ||
|
||
private | ||
|
||
def autocorrect(corrector, node) | ||
corrector.insert_before( | ||
node.location.name, | ||
'ActiveSupport::' | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
spec/rubocop/cop/rails/top_level_hash_with_indifferent_access_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::TopLevelHashWithIndifferentAccess, :config, :rails51 do | ||
context 'with top-level HashWithIndifferentAccess' do | ||
it 'registers and corrects an offense' do | ||
expect_offense(<<~RUBY) | ||
HashWithIndifferentAccess.new(foo: 'bar') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid top-level `HashWithIndifferentAccess`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
ActiveSupport::HashWithIndifferentAccess.new(foo: 'bar') | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with top-level ::HashWithIndifferentAccess' do | ||
it 'registers and corrects an offense' do | ||
expect_offense(<<~RUBY) | ||
::HashWithIndifferentAccess.new(foo: 'bar') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid top-level `HashWithIndifferentAccess`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
::ActiveSupport::HashWithIndifferentAccess.new(foo: 'bar') | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with ActiveSupport::HashWithIndifferentAccess' do | ||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY) | ||
ActiveSupport::HashWithIndifferentAccess.new(foo: 'bar') | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with ActiveSupport::HashWithIndifferentAccess on Rails 5.0', :rails50 do | ||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY) | ||
HashWithIndifferentAccess.new(foo: 'bar') | ||
RUBY | ||
end | ||
end | ||
end |