Sha256: 168279dc5addf0c275d64a70a175e5fc1c6affbb240246045cf974136fa6eb13

Contents?: true

Size: 1.73 KB

Versions: 14

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for potential uses of `Enumerable#minmax`.
      #
      # @example
      #
      #   # bad
      #   bar = [foo.min, foo.max]
      #   return foo.min, foo.max
      #
      #   # good
      #   bar = foo.minmax
      #   return foo.minmax
      class MinMax < Cop
        MSG = 'Use `%<receiver>s.minmax` instead of `%<offender>s`.'

        def on_array(node)
          min_max_candidate(node) do |receiver|
            offender = offending_range(node)

            add_offense(node, location: offender,
                              message: message(offender, receiver))
          end
        end
        alias on_return on_array

        def autocorrect(node)
          receiver = node.children.first.receiver

          lambda do |corrector|
            corrector.replace(offending_range(node),
                              "#{receiver.source}.minmax")
          end
        end

        private

        def_node_matcher :min_max_candidate, <<-PATTERN
          ({array return} (send [$_receiver !nil?] :min) (send [$_receiver !nil?] :max))
        PATTERN

        def message(offender, receiver)
          format(MSG, offender: offender.source,
                      receiver: receiver.source)
        end

        def offending_range(node)
          case node.type
          when :return
            argument_range(node)
          else
            node.loc.expression
          end
        end

        def argument_range(node)
          first_argument_range = node.children.first.loc.expression
          last_argument_range  = node.children.last.loc.expression

          first_argument_range.join(last_argument_range)
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/min_max.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/min_max.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/min_max.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/min_max.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/min_max.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/min_max.rb
zuora_connect_ui-0.8.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/min_max.rb
zuora_connect_ui-0.8.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/min_max.rb
zuora_connect_ui-0.7.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/min_max.rb
zuora_connect_ui-0.7.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/min_max.rb
rubocop-0.72.0 lib/rubocop/cop/style/min_max.rb
rubocop-0.71.0 lib/rubocop/cop/style/min_max.rb
rubocop-0.70.0 lib/rubocop/cop/style/min_max.rb
rubocop-0.69.0 lib/rubocop/cop/style/min_max.rb