Sha256: b72bea55bbbae0e9049da0ba1ada1736fc7da3f5bd2d27138f50c6d8ce647f3a

Contents?: true

Size: 1.09 KB

Versions: 7

Compression:

Stored size: 1.09 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for uses of methods Hash#has_key? and Hash#has_value?
      # Prefer to use Hash#key? and Hash#value? instead
      class PreferredHashMethods < Cop
        MSG = 'Use `Hash#%s` instead of `Hash#%s`.'.freeze

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

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

          add_offense(node, :selector,
                      format(MSG,
                             proper_method_name(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 & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/preferred_hash_methods.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-0.43.0 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-0.42.0 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-0.41.2 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-0.41.1 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-0.41.0 lib/rubocop/cop/style/preferred_hash_methods.rb