Sha256: 773f3667143c412cf14c94d8e139c388f1108bf3307b88b74f1339f98c914454

Contents?: true

Size: 1.65 KB

Versions: 17

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

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_prefixes.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)
            next if predicate_whitelist.include?(method_name)
            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_prefixes
          cop_config['NamePrefix']
        end

        def predicate_whitelist
          cop_config['NameWhitelist']
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/predicate_name.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/predicate_name.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/predicate_name.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/predicate_name.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/predicate_name.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/predicate_name.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/predicate_name.rb
rubocop-0.49.1 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.49.0 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.48.1 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.48.0 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.47.1 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.47.0 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.46.0 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.45.0 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.44.1 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.44.0 lib/rubocop/cop/style/predicate_name.rb