Sha256: e9839ff5cbc662ce4c59c65d56638f9624be06bb68221e50b0d1c338e31a5729

Contents?: true

Size: 1.64 KB

Versions: 167

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # 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 < Base
        extend AutoCorrector

        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(offender, message: message(offender, receiver)) do |corrector|
              receiver = node.children.first.receiver

              corrector.replace(offending_range(node), "#{receiver.source}.minmax")
            end
          end
        end
        alias on_return on_array

        private

        # @!method min_max_candidate(node)
        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.source_range
          end
        end

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

          first_argument_range.join(last_argument_range)
        end
      end
    end
  end
end

Version data entries

167 entries across 166 versions & 18 rubygems

Version Path
rubocop-1.74.0 lib/rubocop/cop/style/min_max.rb
rubocop-1.73.2 lib/rubocop/cop/style/min_max.rb
siteimprove_api_client-1.0.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.73.1/lib/rubocop/cop/style/min_max.rb
rubocop-1.73.1 lib/rubocop/cop/style/min_max.rb
rubocop-1.73.0 lib/rubocop/cop/style/min_max.rb
rubocop-1.72.2 lib/rubocop/cop/style/min_max.rb
rubocop-1.72.1 lib/rubocop/cop/style/min_max.rb
rubocop-1.72.0 lib/rubocop/cop/style/min_max.rb
rubocop-1.71.2 lib/rubocop/cop/style/min_max.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/rubocop-1.71.1/lib/rubocop/cop/style/min_max.rb
rubocop-1.71.1 lib/rubocop/cop/style/min_max.rb
rubocop-1.71.0 lib/rubocop/cop/style/min_max.rb
rubocop-1.70.0 lib/rubocop/cop/style/min_max.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/min_max.rb
rubocop-1.69.2 lib/rubocop/cop/style/min_max.rb
rubocop-1.69.1 lib/rubocop/cop/style/min_max.rb
rubocop-1.69.0 lib/rubocop/cop/style/min_max.rb
rubocop-1.68.0 lib/rubocop/cop/style/min_max.rb
rubocop-1.67.0 lib/rubocop/cop/style/min_max.rb
rubocop-1.66.1 lib/rubocop/cop/style/min_max.rb