Sha256: 46e15dda0759aff2e2465373fffaefa259733e02f735d7022a4ec7664f68f6e1

Contents?: true

Size: 1.07 KB

Versions: 7

Compression:

Stored size: 1.07 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

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

        DEPRECATED_METHODS = [:has_key?, :has_value?].freeze

        def on_send(node)
          _receiver, method_name, *args = *node
          return unless args.size == 1 &&
                        DEPRECATED_METHODS.include?(method_name)

          add_offense(node, :selector,
                      format(MSG,
                             method_name,
                             proper_method_name(method_name)))
        end

        def autocorrect(node)
          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

7 entries across 7 versions & 1 rubygems

Version Path
rubocop-0.40.0 lib/rubocop/cop/style/deprecated_hash_methods.rb
rubocop-0.39.0 lib/rubocop/cop/style/deprecated_hash_methods.rb
rubocop-0.38.0 lib/rubocop/cop/style/deprecated_hash_methods.rb
rubocop-0.37.2 lib/rubocop/cop/style/deprecated_hash_methods.rb
rubocop-0.37.1 lib/rubocop/cop/style/deprecated_hash_methods.rb
rubocop-0.37.0 lib/rubocop/cop/style/deprecated_hash_methods.rb
rubocop-0.36.0 lib/rubocop/cop/style/deprecated_hash_methods.rb