Sha256: d9391c5773ec65c40e06210c4806d533b9d63260bb13e56486357ed72a36b26f

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.20.1 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.20.0 lib/rubocop/cop/style/predicate_name.rb