Sha256: 3ce50b2ec847bf234762947a6b50ab238ca6856d67790505e6225029f9ae6fcd

Contents?: true

Size: 1.34 KB

Versions: 5

Compression:

Stored size: 1.34 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Style
      # This cop enforces the use of consistent method names
      # from the Enumerable module.
      #
      # Unfortunately we cannot actually know if a method is from
      # Enumerable or not (static analysis limitation), so this cop
      # can yield some false positives.
      class CollectionMethods < Cop
        include MethodPreference

        MSG = 'Prefer `%s` over `%s`.'

        def on_block(node)
          method, _args, _body = *node

          check_method_node(method)
        end

        def on_send(node)
          _receiver, _method_name, *args = *node
          return unless args.size == 1 && args.first.type == :block_pass

          check_method_node(node)
        end

        def autocorrect(node)
          lambda do |corrector|
            corrector.replace(node.loc.selector,
                              preferred_method(node.loc.selector.source))
          end
        end

        private

        def check_method_node(node)
          _receiver, method_name, *_args = *node

          return unless preferred_methods[method_name]
          add_offense(node, :selector,
                      format(MSG,
                             preferred_method(method_name),
                             method_name)
                     )
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-0.35.1 lib/rubocop/cop/style/collection_methods.rb
rubocop-0.35.0 lib/rubocop/cop/style/collection_methods.rb
rubocop-0.34.2 lib/rubocop/cop/style/collection_methods.rb
rubocop-0.34.1 lib/rubocop/cop/style/collection_methods.rb
rubocop-0.34.0 lib/rubocop/cop/style/collection_methods.rb