Class: RuboCop::Cop::Naming::PredicateName
- 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.
Constant Summary
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #dynamic_method_define(node) ⇒ Object
- #on_def(node) ⇒ Object (also: #on_defs)
- #on_send(node) ⇒ Object
- #validate_config ⇒ Object
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
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
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
permalink #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 |
permalink #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: (method_name, expected_name(method_name, prefix)) ) end end |
permalink #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: (method_name, expected_name(method_name.to_s, prefix)) ) end end end |
permalink #validate_config ⇒ Object
[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 |