Sha256: d3031189073f6de2456d0b617b0207d0c7a69207e7cb78d9fd27cd98ad75e303
Contents?: true
Size: 1.34 KB
Versions: 2
Compression:
Stored size: 1.34 KB
Contents
# frozen_string_literal: true module RuboCop module Cop module Performance # This cop 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 < Cop include SortBlock MSG = 'Use `sort.reverse` instead of `%<bad_method>s`.' def on_block(node) sort_with_block?(node) do |send, var_a, var_b, body| replaceable_body?(body, var_b, var_a) do range = sort_range(send, node) add_offense( node, location: range, message: message(var_a, var_b) ) end end end def autocorrect(node) sort_with_block?(node) do |send, _var_a, _var_b, _body| lambda do |corrector| range = sort_range(send, node) replacement = 'sort.reverse' corrector.replace(range, replacement) end end end private def message(var_a, var_b) bad_method = "sort { |#{var_a}, #{var_b}| #{var_b} <=> #{var_a} }" format(MSG, bad_method: bad_method) end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
rubocop-performance-1.7.1 | lib/rubocop/cop/performance/sort_reverse.rb |
rubocop-performance-1.7.0 | lib/rubocop/cop/performance/sort_reverse.rb |