Sha256: 34668bcd48a656a24594b62e3bd1c7fe40fb77d5aecf7212b25af388956b6dd2
Contents?: true
Size: 1.89 KB
Versions: 13
Compression:
Stored size: 1.89 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#%s` instead of `Hash#%s`.'.freeze OFFENDING_SELECTORS = { short: [:has_key?, :has_value?], verbose: [:key?, :value?] }.freeze def on_send(node) _receiver, method_name, *args = *node return unless args.size == 1 && offending_selector?(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) 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
13 entries across 13 versions & 2 rubygems