Sha256: b19a42bc1e53cd959e96412e43bf9d2312892d1ae2d40ef3ee2ee667c5e6a683

Contents?: true

Size: 1.88 KB

Versions: 33

Compression:

Stored size: 1.88 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 < Base
        include ConfigurableEnforcedStyle
        extend AutoCorrector

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

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

        RESTRICT_ON_SEND = OFFENDING_SELECTORS.values.flatten.freeze

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

          message = message(node.method_name)

          add_offense(node.loc.selector, message: message) do |corrector|
            corrector.replace(node.loc.selector, proper_method_name(node.loc.selector.source))
          end
        end
        alias on_csend on_send

        private

        def message(method_name)
          format(MSG, prefer: proper_method_name(method_name), current: 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

33 entries across 33 versions & 2 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/preferred_hash_methods.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/preferred_hash_methods.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/preferred_hash_methods.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/preferred_hash_methods.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/preferred_hash_methods.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-1.12.1 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-1.12.0 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-1.11.0 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-1.10.0 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-1.9.1 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-1.9.0 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-1.8.1 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-1.8.0 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-1.7.0 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-1.6.1 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-1.6.0 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-1.5.2 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-1.5.1 lib/rubocop/cop/style/preferred_hash_methods.rb
rubocop-1.5.0 lib/rubocop/cop/style/preferred_hash_methods.rb