Sha256: f22bd15c514b1ab9c291d698c5beed7591a56e968f65b2c33c4310ee126a82e6

Contents?: true

Size: 1010 Bytes

Versions: 2

Compression:

Stored size: 1010 Bytes

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop checks for uses of the deprecated methods Hash#has_key?
      # and Hash#has_value?
      class HashMethods < Cop
        MSG = '%s is deprecated in favor of %s.'

        DEPRECATED_METHODS = [:has_key?, :has_value?]

        def on_send(node)
          _receiver, method_name, *args = *node

          if args.size == 1 && DEPRECATED_METHODS.include?(method_name)
            convention(node, :selector,
                       MSG.format(method_name,
                                  proper_method_name(method_name)))
          end
        end

        def autocorrect_action(node)
          @corrections << lambda do |corrector|
            corrector.replace(node.loc.selector,
                              proper_method_name(node.loc.selector.source))
          end
        end

        private

        def proper_method_name(method_name)
          method_name.to_s.sub(/has_/, '')
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-0.13.1 lib/rubocop/cop/style/hash_methods.rb
rubocop-0.13.0 lib/rubocop/cop/style/hash_methods.rb