lib/rubocop/cop/salsify/style_dig.rb in salsify_rubocop-1.59.1 vs lib/rubocop/cop/salsify/style_dig.rb in salsify_rubocop-1.68.0
- old
+ new
@@ -16,15 +16,13 @@
# # bad
# my_hash['foo']['bar']
# my_hash['foo'] && my_hash['foo']['bar']
# my_array[0][1]
- class StyleDig < Cop
- extend TargetRubyVersion
+ class StyleDig < ::RuboCop::Cop::Base
+ extend RuboCop::Cop::AutoCorrector
- minimum_target_ruby_version 2.3
-
MSG = 'Use `dig` for nested access.'
def_node_matcher :nested_access_match, <<-PATTERN
(send (send (send _receiver !:[]) :[] !{irange erange}) :[] !{irange erange})
PATTERN
@@ -33,10 +31,12 @@
return unless nested_access_match(node) && !assignment?(node)
match_node = node
# walk to outermost access node
match_node = match_node.parent while access_node?(match_node.parent)
- add_offense(match_node, location: :expression, message: MSG)
+ add_offense(match_node, message: MSG) do |corrector|
+ autocorrect(match_node)[corrector]
+ end
end
def autocorrect(node)
access_node = node
source_args = [access_node.first_argument.source]