Sha256: fb41eadd76fcbb3a5192f938017cf3090c3d9c2e779ea2a9f002f102fd6d9761

Contents?: true

Size: 1.69 KB

Versions: 175

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module InternalAffairs
      # Enforces the use of `node.single_line?` instead of
      # comparing `first_line` and `last_line` for equality.
      #
      # @example
      #   # bad
      #   node.loc.first_line == node.loc.last_line
      #
      #   # bad
      #   node.loc.last_line == node.loc.first_line
      #
      #   # bad
      #   node.loc.line == node.loc.last_line
      #
      #   # bad
      #   node.loc.last_line == node.loc.line
      #
      #   # bad
      #   node.first_line == node.last_line
      #
      #   # good
      #   node.single_line?
      #
      class SingleLineComparison < Base
        extend AutoCorrector

        MSG = 'Use `%<preferred>s`.'
        RESTRICT_ON_SEND = %i[== !=].freeze

        # @!method single_line_comparison(node)
        def_node_matcher :single_line_comparison, <<~PATTERN
          {
            (send (send $_receiver {:line :first_line}) {:== :!=} (send _receiver :last_line))
            (send (send $_receiver :last_line) {:== :!=} (send _receiver {:line :first_line}))
          }
        PATTERN

        def on_send(node)
          return unless (receiver = single_line_comparison(node))

          bang = node.method?(:!=) ? '!' : ''
          preferred = "#{bang}#{extract_receiver(receiver)}.single_line?"

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

        private

        def extract_receiver(node)
          node = node.receiver if node.send_type? && %i[loc source_range].include?(node.method_name)
          node.source
        end
      end
    end
  end
end

Version data entries

175 entries across 170 versions & 17 rubygems

Version Path
harbr-0.1.43 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/single_line_comparison.rb
harbr-0.1.42 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/single_line_comparison.rb
harbr-0.1.41 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/single_line_comparison.rb
harbr-0.1.39 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/single_line_comparison.rb
harbr-0.1.38 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/single_line_comparison.rb
harbr-0.1.37 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/single_line_comparison.rb
getargv-0.3.3-universal-darwin vendor/bundle/ruby/3.3.0/gems/rubocop-1.59.0/lib/rubocop/cop/internal_affairs/single_line_comparison.rb
rubocop-1.59.0 lib/rubocop/cop/internal_affairs/single_line_comparison.rb
study_line-0.1.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/single_line_comparison.rb
study_line-0.1.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/single_line_comparison.rb
study_line-0.1.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/single_line_comparison.rb
study_line-0.1.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/single_line_comparison.rb
study_line-0.1.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/single_line_comparison.rb
study_line-0.1.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/single_line_comparison.rb
rubocop-1.58.0 lib/rubocop/cop/internal_affairs/single_line_comparison.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/internal_affairs/single_line_comparison.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.52.1/lib/rubocop/cop/internal_affairs/single_line_comparison.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/single_line_comparison.rb
rubocop-1.57.2 lib/rubocop/cop/internal_affairs/single_line_comparison.rb
rubocop-1.57.1 lib/rubocop/cop/internal_affairs/single_line_comparison.rb