Sha256: 51f5bed716a8b9ec86521d30611a40e5e9d1090e23e5777013bb3c5234ca6d74

Contents?: true

Size: 1.84 KB

Versions: 12

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # This cop identifies places where `sort { |a, b| a.foo <=> b.foo }`
      # can be replaced by `sort_by(&:foo)`.
      # This cop also checks `max` and `min` methods.
      #
      # @example
      #   @bad
      #   array.sort { |a, b| a.foo <=> b.foo }
      #   array.max { |a, b| a.foo <=> b.foo }
      #   array.min { |a, b| a.foo <=> b.foo }
      #
      #   @good
      #   array.sort_by(&:foo)
      #   array.sort_by { |v| v.foo }
      #   array.sort_by do |var|
      #     var.foo
      #   end
      #   array.max_by(&:foo)
      #   array.min_by(&:foo)
      class CompareWithBlock < Cop
        MSG = 'Use `%s_by(&:%s)` instead of ' \
              '`%s { |%s, %s| %s.%s <=> %s.%s }`.'.freeze

        def_node_matcher :compare?, <<-END
          (block $(send _ {:sort :min :max}) (args (arg $_a) (arg $_b)) (send (send (lvar _a) $_m) :<=> (send (lvar _b) $_m)))
        END

        def on_block(node)
          compare?(node) do |send, var_a, var_b, method|
            range = compare_range(send, node)
            compare_method = send.method_name
            add_offense(node, range,
                        format(MSG, compare_method, method,
                               compare_method, var_a, var_b,
                               var_a, method, var_b, method))
          end
        end

        def autocorrect(node)
          send, = *node

          lambda do |corrector|
            method = node.children.last.children.last.children.last
            corrector.replace(compare_range(send, node),
                              "#{send.method_name}_by(&:#{method})")
          end
        end

        private

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

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/performance/compare_with_block.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/performance/compare_with_block.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/performance/compare_with_block.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/performance/compare_with_block.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/performance/compare_with_block.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/performance/compare_with_block.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/performance/compare_with_block.rb
rubocop-0.48.1 lib/rubocop/cop/performance/compare_with_block.rb
rubocop-0.48.0 lib/rubocop/cop/performance/compare_with_block.rb
rubocop-0.47.1 lib/rubocop/cop/performance/compare_with_block.rb
rubocop-0.47.0 lib/rubocop/cop/performance/compare_with_block.rb
rubocop-0.46.0 lib/rubocop/cop/performance/compare_with_block.rb