Class: RuboCop::Cop::Ezcater::StyleDig
- Inherits:
-
RuboCop::Cop
- Object
- RuboCop::Cop
- RuboCop::Cop::Ezcater::StyleDig
- Extended by:
- TargetRubyVersion
- Defined in:
- lib/rubocop/cop/ezcater/style_dig.rb
Overview
Use ‘dig` for deeply nested access.
Constant Summary collapse
- MSG =
"Use `dig` for nested access."
Instance Method Summary collapse
Instance Method Details
#autocorrect(node) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rubocop/cop/ezcater/style_dig.rb', line 39 def autocorrect(node) access_node = node source_args = [access_node.first_argument.source] while access_node?(access_node.children.first) access_node = access_node.children.first source_args << access_node.first_argument.source end root_node = access_node.children.first lambda do |corrector| range = Parser::Source::Range.new(node.source_range.source_buffer, root_node.source_range.end_pos, node.source_range.end_pos) corrector.replace(range, ".dig(#{source_args.reverse.join(', ')})") end end |
#on_send(node) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/rubocop/cop/ezcater/style_dig.rb', line 30 def on_send(node) 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) end |