Class: RuboCop::Cop::Naming::PredicateName

Inherits:
Base
  • Object
show all
Includes:
AllowedMethods
Defined in:
lib/rubocop/cop/naming/predicate_name.rb

Overview

Checks that predicate method names end with a question mark and do not start with a forbidden prefix.

A method is determined to be a predicate method if its name starts with one of the prefixes listed in the ‘NamePrefix` configuration. The list defaults to `is_`, `has_`, and `have_` but may be overridden.

Predicate methods must end with a question mark.

When ‘ForbiddenPrefixes` is also set (as it is by default), predicate methods which begin with a forbidden prefix are not allowed, even if they end with a `?`. These methods should be changed to remove the prefix.

Examples:

NamePrefix: [‘is_’, ‘has_’, ‘have_’] (default)

# bad
def is_even(value)
end

# When ForbiddenPrefixes: ['is_', 'has_', 'have_'] (default)
# good
def even?(value)
end

# When ForbiddenPrefixes: []
# good
def is_even?(value)
end

NamePrefix: [‘seems_to_be_’]

# bad
def seems_to_be_even(value)
end

# When ForbiddenPrefixes: ['seems_to_be_']
# good
def even?(value)
end

# When ForbiddenPrefixes: []
# good
def seems_to_be_even?(value)
end

AllowedMethods: [‘is_a?’] (default)

# Despite starting with the `is_` prefix, this method is allowed
# good
def is_a?(value)
end

AllowedMethods: [‘is_even?’]

# good
def is_even?(value)
end

MethodDefinitionMacros: [‘define_method’, ‘define_singleton_method’] (default)

# bad
define_method(:is_even) { |value| }

# good
define_method(:even?) { |value| }

MethodDefinitionMacros: [‘def_node_matcher’]

# bad
def_node_matcher(:is_even) { |value| }

# good
def_node_matcher(:even?) { |value| }

Constant Summary

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#dynamic_method_define(node) ⇒ Object



79
80
81
82
83
# File 'lib/rubocop/cop/naming/predicate_name.rb', line 79

def_node_matcher :dynamic_method_define, <<~PATTERN
  (send nil? #method_definition_macros
    (sym $_)
    ...)
PATTERN

#on_def(node) ⇒ Object Also known as: on_defs



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rubocop/cop/naming/predicate_name.rb', line 98

def on_def(node)
  predicate_prefixes.each do |prefix|
    method_name = node.method_name.to_s

    next if allowed_method_name?(method_name, prefix)

    add_offense(
      node.loc.name,
      message: message(method_name, expected_name(method_name, prefix))
    )
  end
end

#on_send(node) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rubocop/cop/naming/predicate_name.rb', line 85

def on_send(node)
  dynamic_method_define(node) do |method_name|
    predicate_prefixes.each do |prefix|
      next if allowed_method_name?(method_name.to_s, prefix)

      add_offense(
        node.first_argument,
        message: message(method_name, expected_name(method_name.to_s, prefix))
      )
    end
  end
end

#validate_configObject



112
113
114
115
116
117
118
119
120
# File 'lib/rubocop/cop/naming/predicate_name.rb', line 112

def validate_config
  forbidden_prefixes.each do |forbidden_prefix|
    next if predicate_prefixes.include?(forbidden_prefix)

    raise ValidationError, <<~MSG.chomp
      The `Naming/PredicateName` cop is misconfigured. Prefix #{forbidden_prefix} must be included in NamePrefix because it is included in ForbiddenPrefixes.
    MSG
  end
end