Sha256: f724acd58d5f69518a63a89c1366d88f58a84f3e966ed2688bdac9f51b9c22f8

Contents?: true

Size: 1.49 KB

Versions: 14

Compression:

Stored size: 1.49 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 OnMethodDef

        private

        def on_method_def(node, method_name, _args, _body)
          predicate_prefices.each do |prefix|
            method_name = method_name.to_s
            next unless method_name.start_with?(prefix)
            next if method_name == expected_name(method_name, prefix)
            add_offense(
              node,
              :name,
              message(method_name, expected_name(method_name, prefix))
            )
          end
        end

        def expected_name(method_name, prefix)
          new_name = if prefix_blacklist.include?(prefix)
                       method_name.sub(prefix, '')
                     else
                       method_name.dup
                     end
          new_name << '?' unless method_name.end_with?('?')
          new_name
        end

        def message(method_name, new_name)
          "Rename `#{method_name}` to `#{new_name}`."
        end

        def prefix_blacklist
          cop_config['NamePrefixBlacklist']
        end

        def predicate_prefices
          cop_config['NamePrefix']
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
rubocop-0.34.2 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.34.1 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.34.0 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.33.0 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.32.1 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.32.0 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.31.0 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.30.1 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.30.0 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.29.1 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.29.0 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.28.0 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.27.1 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.27.0 lib/rubocop/cop/style/predicate_name.rb