Sha256: 284f9f875d5e9e8c64e431058eaf7849386b82c0a9b546e2f7c331532a825d44

Contents?: true

Size: 1.7 KB

Versions: 37

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Identifies places where `sort_by { ... }` can be replaced by
      # `sort`.
      #
      # @example
      #   # bad
      #   array.sort_by { |x| x }
      #   array.sort_by do |var|
      #     var
      #   end
      #
      #   # good
      #   array.sort
      class RedundantSortBy < Base
        include RangeHelp
        extend AutoCorrector

        MSG_BLOCK = 'Use `sort` instead of `sort_by { |%<var>s| %<var>s }`.'
        MSG_NUMBLOCK = 'Use `sort` instead of `sort_by { _1 }`.'

        def on_block(node)
          redundant_sort_by_block(node) do |send, var_name|
            range = sort_by_range(send, node)

            add_offense(range, message: format(MSG_BLOCK, var: var_name)) do |corrector|
              corrector.replace(range, 'sort')
            end
          end
        end

        def on_numblock(node)
          redundant_sort_by_numblock(node) do |send|
            range = sort_by_range(send, node)

            add_offense(range, message: format(MSG_NUMBLOCK)) do |corrector|
              corrector.replace(range, 'sort')
            end
          end
        end

        private

        # @!method redundant_sort_by_block(node)
        def_node_matcher :redundant_sort_by_block, <<~PATTERN
          (block $(call _ :sort_by) (args (arg $_x)) (lvar _x))
        PATTERN

        # @!method redundant_sort_by_numblock(node)
        def_node_matcher :redundant_sort_by_numblock, <<~PATTERN
          (numblock $(call _ :sort_by) 1 (lvar :_1))
        PATTERN

        def sort_by_range(send, node)
          range_between(send.loc.selector.begin_pos, node.loc.end.end_pos)
        end
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 8 rubygems

Version Path
rubocop-1.70.0 lib/rubocop/cop/style/redundant_sort_by.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/redundant_sort_by.rb
rubocop-1.69.2 lib/rubocop/cop/style/redundant_sort_by.rb
rubocop-1.69.1 lib/rubocop/cop/style/redundant_sort_by.rb
rubocop-1.69.0 lib/rubocop/cop/style/redundant_sort_by.rb
rubocop-1.68.0 lib/rubocop/cop/style/redundant_sort_by.rb
rubocop-1.67.0 lib/rubocop/cop/style/redundant_sort_by.rb
rubocop-1.66.1 lib/rubocop/cop/style/redundant_sort_by.rb
rubocop-1.66.0 lib/rubocop/cop/style/redundant_sort_by.rb
rubocop-1.65.1 lib/rubocop/cop/style/redundant_sort_by.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/redundant_sort_by.rb
rubocop-1.65.0 lib/rubocop/cop/style/redundant_sort_by.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/redundant_sort_by.rb
rubocop-1.64.1 lib/rubocop/cop/style/redundant_sort_by.rb
rubocop-1.63.4 lib/rubocop/cop/style/redundant_sort_by.rb
rubocop-1.63.3 lib/rubocop/cop/style/redundant_sort_by.rb
rubocop-1.63.2 lib/rubocop/cop/style/redundant_sort_by.rb
rubocop-1.63.1 lib/rubocop/cop/style/redundant_sort_by.rb
rubocop-1.63.0 lib/rubocop/cop/style/redundant_sort_by.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/style/redundant_sort_by.rb