Sha256: 339512ba50f2a4842a597c99ecb51443f66b70dc17398614c10e90c13d3d6225

Contents?: true

Size: 1022 Bytes

Versions: 1

Compression:

Stored size: 1022 Bytes

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop makes sure that predicates are named properly.
      #
      # @example
      #   # bad
      #   def is_even?(value) ...
      #
      #   # good
      #   def even?(value)
      #
      #   # bad
      #   def has_value? ...
      #
      #   # good
      #   def value? ...
      class PredicateName < Cop
        include CheckMethods

        private

        def check(node, method_name, _args, _body)
          prefix_blacklist.each do |prefix|
            next unless method_name.to_s.start_with?(prefix)
            add_offense(node, :name, message(method_name.to_s, prefix))
          end
        end

        def message(method_name, prefix)
          new_name = method_name.sub(prefix, '')
          new_name << '?' unless method_name.end_with?('?')
          "Rename `#{method_name}` to `#{new_name}`."
        end

        def prefix_blacklist
          cop_config['NamePrefixBlacklist']
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-0.22.0 lib/rubocop/cop/style/predicate_name.rb