Sha256: 7873d519e0617bb574dbd2673e95c5c24272d5f51c3503d4617d69dce4b5beef

Contents?: true

Size: 1.65 KB

Versions: 36

Compression:

Stored size: 1.65 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.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

36 entries across 32 versions & 4 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/min_max.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/min_max.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/min_max.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/style/min_max.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/min_max.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/style/min_max.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/style/min_max.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/min_max.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/style/min_max.rb
zilla-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.46.0/lib/rubocop/cop/style/min_max.rb
rubocop-1.46.0 lib/rubocop/cop/style/min_max.rb
rubocop-1.45.1 lib/rubocop/cop/style/min_max.rb
rubocop-1.45.0 lib/rubocop/cop/style/min_max.rb
rubocop-1.44.1 lib/rubocop/cop/style/min_max.rb
rubocop-1.44.0 lib/rubocop/cop/style/min_max.rb
rubocop-1.43.0 lib/rubocop/cop/style/min_max.rb
rubocop-1.42.0 lib/rubocop/cop/style/min_max.rb
rubocop-1.41.1 lib/rubocop/cop/style/min_max.rb
rubocop-1.41.0 lib/rubocop/cop/style/min_max.rb
rubocop-1.40.0 lib/rubocop/cop/style/min_max.rb