Sha256: f55f47ca6e8c216a2eba99bc29307ad6d6e791e63cca4fc147a62cf105235234

Contents?: true

Size: 1.75 KB

Versions: 14

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # 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
      class SafeNavigationChain < Cop
        include NilMethods

        MSG = 'Do not chain ordinary method call' \
              ' after safe navigation operator.'

        def_node_matcher :bad_method?, <<-PATTERN
        {
          (send $(csend ...) $_ ...)
          (send $(block (csend ...) ...) $_ ...)
        }
        PATTERN

        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

        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
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/lint/safe_navigation_chain.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/lint/safe_navigation_chain.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/lint/safe_navigation_chain.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/lint/safe_navigation_chain.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/lint/safe_navigation_chain.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/lint/safe_navigation_chain.rb
zuora_connect_ui-0.8.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/lint/safe_navigation_chain.rb
zuora_connect_ui-0.8.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/lint/safe_navigation_chain.rb
zuora_connect_ui-0.7.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/lint/safe_navigation_chain.rb
zuora_connect_ui-0.7.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/lint/safe_navigation_chain.rb
rubocop-0.72.0 lib/rubocop/cop/lint/safe_navigation_chain.rb
rubocop-0.71.0 lib/rubocop/cop/lint/safe_navigation_chain.rb
rubocop-0.70.0 lib/rubocop/cop/lint/safe_navigation_chain.rb
rubocop-0.69.0 lib/rubocop/cop/lint/safe_navigation_chain.rb