Sha256: 4e00e099ad2c242c4a1be7cc3b4735b599984e57ec11d45189fac01fa5ab9f61

Contents?: true

Size: 1.66 KB

Versions: 14

Compression:

Stored size: 1.66 KB

Contents

# encoding: utf-8
# 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

14 entries across 14 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/predicate_name.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/predicate_name.rb
rubocop-0.43.0 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.42.0 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.41.2 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.41.1 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.41.0 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.40.0 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.39.0 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.38.0 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.37.2 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.37.1 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.37.0 lib/rubocop/cop/style/predicate_name.rb
rubocop-0.36.0 lib/rubocop/cop/style/predicate_name.rb