Sha256: 44de48c935225bdaf902ba03eb7d05d90a0caa909c2c2e7fe801cdd3a22ae078

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

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|
            if method_name.to_s.start_with?(prefix)
              add_offense(node, :name,
                          message(method_name.to_s, prefix))
            end
          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

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-0.19.1 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.19.0 lib/rubocop/cop/style/predicate_name.rb