Sha256: f27603e1be3173acabb7de36fd3414bc0001c7ecdbe4c3cc31878e4f19a9b16b

Contents?: true

Size: 1.66 KB

Versions: 27

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # In Ruby 2.7, `Enumerable#filter_map` has been added.
      #
      # This cop identifies places where `select.map` can be replaced by `filter_map`.
      #
      # @example
      #   # bad
      #   ary.select(&:foo).map(&:bar)
      #   ary.filter(&:foo).map(&:bar)
      #
      #   # good
      #   ary.filter_map { |o| o.bar if o.foo }
      #
      class SelectMap < Base
        include RangeHelp
        extend TargetRubyVersion

        minimum_target_ruby_version 2.7

        MSG = 'Use `filter_map` instead of `%<method_name>s.map`.'
        RESTRICT_ON_SEND = %i[select filter].freeze

        def_node_matcher :bad_method?, <<~PATTERN
          (send nil? :bad_method ...)
        PATTERN

        def on_send(node)
          return if (first_argument = node.first_argument) && !first_argument.block_pass_type?
          return unless (send_node = map_method_candidate(node))
          return unless send_node.method?(:map)

          map_method = send_node.parent&.block_type? ? send_node.parent : send_node

          range = offense_range(node, map_method)
          add_offense(range, message: format(MSG, method_name: node.method_name))
        end

        private

        def map_method_candidate(node)
          return unless (parent = node.parent)

          if parent.block_type? && parent.parent&.send_type?
            parent.parent
          elsif parent.send_type?
            parent
          end
        end

        def offense_range(node, map_method)
          range_between(node.loc.selector.begin_pos, map_method.loc.expression.end_pos)
        end
      end
    end
  end
end

Version data entries

27 entries across 25 versions & 4 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-performance-1.14.3/lib/rubocop/cop/performance/select_map.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-performance-1.14.3/lib/rubocop/cop/performance/select_map.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-performance-1.14.3/lib/rubocop/cop/performance/select_map.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-performance-1.13.3/lib/rubocop/cop/performance/select_map.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-performance-1.14.3/lib/rubocop/cop/performance/select_map.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-performance-1.13.3/lib/rubocop/cop/performance/select_map.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-performance-1.14.3/lib/rubocop/cop/performance/select_map.rb
rubocop-performance-1.16.0 lib/rubocop/cop/performance/select_map.rb
rubocop-performance-1.15.2 lib/rubocop/cop/performance/select_map.rb
rubocop-performance-1.15.1 lib/rubocop/cop/performance/select_map.rb
rubocop-performance-1.15.0 lib/rubocop/cop/performance/select_map.rb
rubocop-performance-1.14.3 lib/rubocop/cop/performance/select_map.rb
rubocop-performance-1.14.2 lib/rubocop/cop/performance/select_map.rb
rubocop-performance-1.14.1 lib/rubocop/cop/performance/select_map.rb
rubocop-performance-1.14.0 lib/rubocop/cop/performance/select_map.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-performance-1.13.3/lib/rubocop/cop/performance/select_map.rb
rubocop-performance-1.13.3 lib/rubocop/cop/performance/select_map.rb
rubocop-performance-1.13.2 lib/rubocop/cop/performance/select_map.rb
rubocop-performance-1.13.1 lib/rubocop/cop/performance/select_map.rb
rubocop-performance-1.13.0 lib/rubocop/cop/performance/select_map.rb