class RuboCop::Cop::Lint::SafeNavigationChain
The safe navigation operator returns nil if the receiver is nil. If you chain an ordinary method call after a safe navigation operator, it raises NoMethodError. We should use a safe navigation operator after a safe navigation operator. This cop checks for the problem outlined above.
@example
# bad x&.foo.bar x&.foo + bar x&.foo[bar]
@example
# good x&.foo&.bar x&.foo || bar
Constants
- MSG
Public Instance Methods
on_send(node)
click to toggle source
# File lib/rubocop/cop/lint/safe_navigation_chain.rb, line 39 def on_send(node) bad_method?(node) do |safe_nav, method| return if nil_methods.include?(method) method_chain = method_chain(node) location = Parser::Source::Range.new(node.loc.expression.source_buffer, safe_nav.loc.expression.end_pos, method_chain.loc.expression.end_pos) add_offense(node, location: location) end end
Private Instance Methods
method_chain(node)
click to toggle source
# File lib/rubocop/cop/lint/safe_navigation_chain.rb, line 54 def method_chain(node) chain = node while chain.send_type? chain = chain.parent if chain.parent && %i[send csend].include?(chain.parent.type) break end chain end