class RuboCop::Cop::Naming::PredicateName
end
def is_a?(value)
# good
@example AllowedMethods: [‘is_a?’] (default)
end
def value?
# good
end
def has_value?
end
def has_value
# bad
end
def even?(value)
# good
end
def is_even?(value)
end
def is_even(value)
# bad
@example
this cop.
a prefix in the former but not the latter will not be considered by
NOTE: ‘ForbiddenPrefixes` is only applied to prefixes in `NamePrefix`;
the `is_` prefix, and will be corrected to `foo?`.
`is_foo` will register an offense both because the ? is missing and because of
autocorrected to `is_foo?`). If `ForbiddenPrefixes` contains `is_`,
will register an offense only due to the lack of question mark (and will be
In other words, if `ForbiddenPrefixes` is empty, a method named `is_foo`
prefixes will not be allowed and will be removed by autocorrection.
If `ForbiddenPrefixes` is set, methods that start with the configured
NOTE: The `is_a?` method is allowed by default.
the `AllowedMethods` configuration.
the cop to end with a `?`. Other methods can be allowed by adding to
Any method name that starts with one of these prefixes is required by
You can change what prefixes are considered by changing this option.
with one of the prefixes defined in the `NamePrefix` configuration.
A method is determined to be a predicate method if its name starts
do not start with a forbidden prefix.
Checks that predicate methods names end with a question mark and
def allowed_method_name?(method_name, prefix)
def allowed_method_name?(method_name, prefix) !(method_name.start_with?(prefix) && # cheap check to avoid allocating Regexp method_name.match?(/^#{prefix}[^0-9]/)) || method_name == expected_name(method_name, prefix) || method_name.end_with?('=') || # rubocop:todo InternalAffairs/MethodNameEndWith allowed_method?(method_name) end
def expected_name(method_name, prefix)
def expected_name(method_name, prefix) new_name = if forbidden_prefixes.include?(prefix) method_name.sub(prefix, '') else method_name.dup end new_name << '?' unless method_name.end_with?('?') # rubocop:todo InternalAffairs/MethodNameEndWith new_name end
def forbidden_prefixes
def forbidden_prefixes cop_config['ForbiddenPrefixes'] end
def message(method_name, new_name)
def message(method_name, new_name) "Rename `#{method_name}` to `#{new_name}`." end
def method_definition_macros(macro_name)
def method_definition_macros(macro_name) cop_config['MethodDefinitionMacros'].include?(macro_name.to_s) end
def on_def(node)
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
def on_send(node)
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: message(method_name, expected_name(method_name.to_s, prefix)) ) end end end
def predicate_prefixes
def predicate_prefixes cop_config['NamePrefix'] end