Class: RuboCop::Cop::Naming::PredicateName
- Includes:
- AllowedMethods
- Defined in:
- lib/rubocop/cop/naming/predicate_name.rb
Overview
Checks that predicate methods 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 defined in the NamePrefix
configuration.
You can change what prefixes are considered by changing this option.
Any method name that starts with one of these prefixes is required by
the cop to end with a ?
. Other methods can be allowed by adding to
the AllowedMethods
configuration.
Note
|
The is_a? method is allowed by default.
|
If ForbiddenPrefixes
is set, methods that start with the configured
prefixes will not be allowed and will be removed by autocorrection.
In other words, if ForbiddenPrefixes
is empty, a method named is_foo
will register an offense only due to the lack of question mark (and will be
autocorrected to is_foo?
). If ForbiddenPrefixes
contains is_
,
is_foo
will register an offense both because the ? is missing and because of
the is_
prefix, and will be corrected to foo?
.
Note
|
ForbiddenPrefixes is only applied to prefixes in NamePrefix ;
a prefix in the former but not the latter will not be considered by
this cop.
|
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
Methods inherited from Base
#active_support_extensions_enabled?, #add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #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, #ready, #relevant_file?, 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
63 64 65 66 67 |
# File 'lib/rubocop/cop/naming/predicate_name.rb', line 63 def_node_matcher :dynamic_method_define, <<~PATTERN (send nil? #method_definition_macros (sym $_) ...) PATTERN |
#on_def(node) ⇒ Object Also known as: on_defs
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rubocop/cop/naming/predicate_name.rb', line 82 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
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rubocop/cop/naming/predicate_name.rb', line 69 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.source_range, message: (method_name, expected_name(method_name.to_s, prefix)) ) end end end |