Sha256: c626f47f66f10232a19ecfe59b50f34b8f149998de5c949480980ea905dab68a

Contents?: true

Size: 1.22 KB

Versions: 84

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # Identifies places where `sort { |a, b| b <=> a }`
      # can be replaced by a faster `sort.reverse`.
      #
      # @example
      #   # bad
      #   array.sort { |a, b| b <=> a }
      #
      #   # good
      #   array.sort.reverse
      #
      class SortReverse < Base
        include SortBlock
        extend AutoCorrector

        MSG = 'Use `sort.reverse` instead.'

        def on_block(node)
          sort_with_block?(node) do |send, var_a, var_b, body|
            replaceable_body?(body, var_b, var_a) do
              register_offense(send, node)
            end
          end
        end

        def on_numblock(node)
          sort_with_numblock?(node) do |send, arg_count, body|
            next unless arg_count == 2

            replaceable_body?(body, :_2, :_1) do
              register_offense(send, node)
            end
          end
        end

        private

        def register_offense(send, node)
          range = sort_range(send, node)

          add_offense(range) do |corrector|
            replacement = 'sort.reverse'

            corrector.replace(range, replacement)
          end
        end
      end
    end
  end
end

Version data entries

84 entries across 84 versions & 4 rubygems

Version Path
harbr-0.1.90 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/sort_reverse.rb
harbr-0.1.89 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/sort_reverse.rb
harbr-0.1.88 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/sort_reverse.rb
harbr-0.1.87 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/sort_reverse.rb
harbr-0.1.86 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/sort_reverse.rb
harbr-0.1.85 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/sort_reverse.rb
harbr-0.1.84 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/sort_reverse.rb
harbr-0.1.83 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/sort_reverse.rb
harbr-0.1.82 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/sort_reverse.rb
harbr-0.1.81 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/sort_reverse.rb
harbr-0.1.80 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/sort_reverse.rb
harbr-0.1.79 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/sort_reverse.rb
harbr-0.1.78 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/sort_reverse.rb
harbr-0.1.77 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/sort_reverse.rb
harbr-0.1.76 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/sort_reverse.rb
harbr-0.1.75 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/sort_reverse.rb
harbr-0.1.74 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/sort_reverse.rb
harbr-0.1.73 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/sort_reverse.rb
harbr-0.1.72 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/sort_reverse.rb
harbr-0.1.71 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/sort_reverse.rb