Sha256: 5f346cb3a043be9888067424a5854fa3c61ec729dbd9d225156ee4346914eb39

Contents?: true

Size: 1.21 KB

Versions: 6894

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # This cop 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 < Cop
        include RangeHelp

        MSG = 'Use `sort` instead of `sort_by { |%<var>s| %<var>s }`.'.freeze

        def_node_matcher :redundant_sort_by, <<-PATTERN
          (block $(send _ :sort_by) (args (arg $_x)) (lvar _x))
        PATTERN

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

            add_offense(node,
                        location: range,
                        message: format(MSG, var: var_name))
          end
        end

        def autocorrect(node)
          send, = *node
          ->(corrector) { corrector.replace(sort_by_range(send, node), 'sort') }
        end

        private

        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

6,894 entries across 6,888 versions & 27 rubygems

Version Path
ory-client-0.0.1.alpha58 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/redundant_sort_by.rb
ory-client-0.0.1.alpha57 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/redundant_sort_by.rb
ory-client-0.0.1.alpha56 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/redundant_sort_by.rb
ory-client-0.0.1.alpha55 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/redundant_sort_by.rb
ory-client-0.0.1.alpha54 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/redundant_sort_by.rb
ory-client-0.0.1.alpha53 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/redundant_sort_by.rb
ory-client-0.0.1.alpha52 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/redundant_sort_by.rb
ory-client-0.0.1.alpha51 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/redundant_sort_by.rb
ory-client-0.0.1.alpha50 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/redundant_sort_by.rb
ory-client-0.0.1.alpha49 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/redundant_sort_by.rb
ory-client-0.0.1.alpha48 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/redundant_sort_by.rb
ory-client-0.0.1.alpha47 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/redundant_sort_by.rb
ory-client-0.0.1.alpha46 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/redundant_sort_by.rb
ory-client-0.0.1.alpha45 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/redundant_sort_by.rb
ory-client-0.0.1.alpha44 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/redundant_sort_by.rb
ory-client-0.0.1.alpha43 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/redundant_sort_by.rb
ory-client-0.0.1.alpha42 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/redundant_sort_by.rb
ory-client-0.0.1.alpha41 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/redundant_sort_by.rb
ory-client-0.0.1.alpha40 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/redundant_sort_by.rb
ory-client-0.0.1.alpha39 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/redundant_sort_by.rb