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.
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_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
#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: (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: (method_name, expected_name(method_name.to_s, prefix)) ) end end end |
#validate_config ⇒ Object
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 |