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.

When ‘UseSorbetSigs` set to true (optional), the cop will only report offenses if the method has a Sorbet `sig` with a return type of `T::Boolean`. Dynamic methods are not supported with this configuration.

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

UseSorbetSigs: false (default)

# bad
sig { returns(String) }
def is_this_thing_on
  "yes"
end

# good - Sorbet signature is not evaluated
sig { returns(String) }
def is_this_thing_on?
  "yes"
end

UseSorbetSigs: true

# bad
sig { returns(T::Boolean) }
def odd(value)
end

# good
sig { returns(T::Boolean) }
def odd?(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_gem_version, #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

[View source]

107
108
109
110
111
# File 'lib/rubocop/cop/naming/predicate_name.rb', line 107

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

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

[View source]

126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rubocop/cop/naming/predicate_name.rb', line 126

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

    next if allowed_method_name?(method_name, prefix)
    next if use_sorbet_sigs? && !sorbet_sig?(node, return_type: 'T::Boolean')

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

#on_send(node) ⇒ Object

[View source]

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

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

[View source]

141
142
143
144
145
146
147
148
149
# File 'lib/rubocop/cop/naming/predicate_name.rb', line 141

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