Sha256: d0d2388ae12337ed167ee909471f3ef508ed14e83191194a2d5b3eb86cab3315

Contents?: true

Size: 1.85 KB

Versions: 26

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop (by default) checks for uses of methods Hash#has_key? and
      # Hash#has_value? where it enforces Hash#key? and Hash#value?
      # It is configurable to enforce the inverse, using `verbose` method
      # names also.
      #
      # @example EnforcedStyle: short (default)
      #  # bad
      #  Hash#has_key?
      #  Hash#has_value?
      #
      #  # good
      #  Hash#key?
      #  Hash#value?
      #
      # @example EnforcedStyle: verbose
      #  # bad
      #  Hash#key?
      #  Hash#value?
      #
      #  # good
      #  Hash#has_key?
      #  Hash#has_value?
      class PreferredHashMethods < Cop
        include ConfigurableEnforcedStyle

        MSG = 'Use `Hash#%<prefer>s` instead of `Hash#%<current>s`.'.freeze

        OFFENDING_SELECTORS = {
          short: %i[has_key? has_value?],
          verbose: %i[key? value?]
        }.freeze

        def on_send(node)
          return unless node.arguments.one? &&
                        offending_selector?(node.method_name)

          add_offense(node, location: :selector)
        end

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

        private

        def message(node)
          format(MSG,
                 prefer: proper_method_name(node.method_name),
                 current: node.method_name)
        end

        def proper_method_name(method_name)
          if style == :verbose
            "has_#{method_name}"
          else
            method_name.to_s.sub(/has_/, '')
          end
        end

        def offending_selector?(method_name)
          OFFENDING_SELECTORS[style].include?(method_name)
        end
      end
    end
  end
end

Version data entries

26 entries across 24 versions & 3 rubygems

Version Path
rubocop-0.63.1 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-0.63.0 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-0.62.0 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-0.61.1 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-0.61.0 lib/rubocop/cop/style/preferred_hash_methods.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/preferred_hash_methods.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/preferred_hash_methods.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/preferred_hash_methods.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-0.60.0 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-0.59.2 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-0.59.1 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-0.59.0 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-0.58.2 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-0.58.1 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-0.58.0 lib/rubocop/cop/style/preferred_hash_methods.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-0.57.2 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-0.57.1 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-0.57.0 lib/rubocop/cop/style/preferred_hash_methods.rb