Sha256: 564ce75d0fd199ab50e78bb87bf69b353d22748a1ea1bc7e567ce49109f12783

Contents?: true

Size: 1.7 KB

Versions: 143

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 $(send _ :sort_by) (args (arg $_x)) (lvar _x))
        PATTERN

        # @!method redundant_sort_by_numblock(node)
        def_node_matcher :redundant_sort_by_numblock, <<~PATTERN
          (numblock $(send _ :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

143 entries across 138 versions & 13 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/redundant_sort_by.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/redundant_sort_by.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/redundant_sort_by.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/redundant_sort_by.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/redundant_sort_by.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/redundant_sort_by.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/redundant_sort_by.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/redundant_sort_by.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/redundant_sort_by.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/redundant_sort_by.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/redundant_sort_by.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/redundant_sort_by.rb
harbr-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/redundant_sort_by.rb
harbr-0.2.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/redundant_sort_by.rb
harbr-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/redundant_sort_by.rb
harbr-0.1.99 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/redundant_sort_by.rb
harbr-0.1.98 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/redundant_sort_by.rb
harbr-0.1.97 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/redundant_sort_by.rb
harbr-0.1.96 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/redundant_sort_by.rb
harbr-0.1.95 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/redundant_sort_by.rb