Sha256: 2e177d84e80806a77d8ffb7d239a870ef662601d756a64a0a1f340ba12e074f8

Contents?: true

Size: 1.67 KB

Versions: 132

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module InternalAffairs
      # Enforces the use of `same_line?` instead of location line comparison for equality.
      #
      # @example
      #   # bad
      #   node.loc.line == node.parent.loc.line
      #
      #   # bad
      #   node.loc.first_line == node.parent.loc.first_line
      #
      #   # good
      #   same_line?(node, node.parent)
      #
      class LocationLineEqualityComparison < Base
        extend AutoCorrector

        MSG = 'Use `%<preferred>s`.'

        # @!method line_send(node)
        def_node_matcher :line_send, <<~PATTERN
          {
            (send (send _ {:loc :source_range}) {:line :first_line})
            (send _ :first_line)
          }
        PATTERN

        # @!method location_line_equality_comparison?(node)
        def_node_matcher :location_line_equality_comparison?, <<~PATTERN
          (send #line_send :== #line_send)
        PATTERN

        def on_send(node)
          return unless location_line_equality_comparison?(node)

          lhs, _op, rhs = *node

          lhs_receiver = extract_receiver(lhs)
          rhs_receiver = extract_receiver(rhs)
          preferred = "same_line?(#{lhs_receiver}, #{rhs_receiver})"

          add_offense(node, message: format(MSG, preferred: preferred)) do |corrector|
            corrector.replace(node, preferred)
          end
        end

        private

        def extract_receiver(node)
          receiver = node.receiver
          if receiver.send_type? && (receiver.method?(:loc) || receiver.method?(:source_range))
            receiver = receiver.receiver
          end
          receiver.source
        end
      end
    end
  end
end

Version data entries

132 entries across 132 versions & 13 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb
rubocop-1.68.0 lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb
rubocop-1.67.0 lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb
rubocop-1.66.1 lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb
rubocop-1.66.0 lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb
rubocop-1.65.1 lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb
rubocop-1.65.0 lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb
rubocop-1.64.1 lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb
rubocop-1.63.4 lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb
rubocop-1.63.3 lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb
rubocop-1.63.2 lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb
rubocop-1.63.1 lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb
rubocop-1.63.0 lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb
rubocop-1.62.1 lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb
rubocop-1.62.0 lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb
rubocop-1.61.0 lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb