Sha256: 36ebf46135a1dae60e4e7e45eabd7902cfd1c3beac39b366af96d85bee42c98a

Contents?: true

Size: 1.86 KB

Versions: 132

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Looks for uses of `map.to_set` or `collect.to_set` that could be
      # written with just `to_set`.
      #
      # @safety
      #   This cop is unsafe, as it can produce false positives if the receiver
      #   is not an `Enumerable`.
      #
      # @example
      #   # bad
      #   something.map { |i| i * 2 }.to_set
      #
      #   # good
      #   something.to_set { |i| i * 2 }
      #
      #   # bad
      #   [1, 2, 3].collect { |i| i.to_s }.to_set
      #
      #   # good
      #   [1, 2, 3].to_set { |i| i.to_s }
      #
      class MapToSet < Base
        extend AutoCorrector
        include RangeHelp

        MSG = 'Pass a block to `to_set` instead of calling `%<method>s.to_set`.'
        RESTRICT_ON_SEND = %i[to_set].freeze

        # @!method map_to_set?(node)
        def_node_matcher :map_to_set?, <<~PATTERN
          {
            $(send ({block numblock} $(send _ {:map :collect}) ...) :to_set)
            $(send $(send _ {:map :collect} (block_pass sym)) :to_set)
          }
        PATTERN

        def on_send(node)
          return unless (to_set_node, map_node = map_to_set?(node))

          message = format(MSG, method: map_node.loc.selector.source)
          add_offense(map_node.loc.selector, message: message) do |corrector|
            # If the `to_set` call already has a block, do not autocorrect.
            next if to_set_node.block_node

            autocorrect(corrector, to_set_node, map_node)
          end
        end

        private

        def autocorrect(corrector, to_set, map)
          removal_range = range_between(to_set.loc.dot.begin_pos, to_set.loc.selector.end_pos)

          corrector.remove(range_with_surrounding_space(removal_range, side: :left))
          corrector.replace(map.loc.selector, 'to_set')
        end
      end
    end
  end
end

Version data entries

132 entries across 131 versions & 12 rubygems

Version Path
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/map_to_set.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/style/map_to_set.rb
rubocop-1.62.1 lib/rubocop/cop/style/map_to_set.rb
rubocop-1.62.0 lib/rubocop/cop/style/map_to_set.rb
rubocop-1.61.0 lib/rubocop/cop/style/map_to_set.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/style/map_to_set.rb
rubocop-1.60.2 lib/rubocop/cop/style/map_to_set.rb
study_line-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/map_to_set.rb
study_line-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/map_to_set.rb
study_line-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/map_to_set.rb
study_line-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/map_to_set.rb
study_line-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/map_to_set.rb
study_line-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/map_to_set.rb
rubocop-1.60.1 lib/rubocop/cop/style/map_to_set.rb
study_line-0.2.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/map_to_set.rb
study_line-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/map_to_set.rb
rubocop-1.60.0 lib/rubocop/cop/style/map_to_set.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/map_to_set.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/map_to_set.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/map_to_set.rb