Module: RuboCop::Cop::UncommunicativeName
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Defined in: | lib/rubocop/cop/mixin/uncommunicative_name.rb |
Overview
Common functionality shared by Uncommunicative cops
Constant Summary
-
CASE_MSG =
# File 'lib/rubocop/cop/mixin/uncommunicative_name.rb', line 7'Only use lowercase characters for %<name_type>s.'
-
FORBIDDEN_MSG =
# File 'lib/rubocop/cop/mixin/uncommunicative_name.rb', line 10'Do not use %<name>s as a name for a %<name_type>s.'
-
LENGTH_MSG =
# File 'lib/rubocop/cop/mixin/uncommunicative_name.rb', line 9'%<name_type>s must be at least %<min>s characters long.'
-
NUM_MSG =
# File 'lib/rubocop/cop/mixin/uncommunicative_name.rb', line 8'Do not end %<name_type>s with a number.'
Instance Method Summary
- #check(node, args)
- #allow_nums private
- #allowed_names private
- #arg_range(arg, length) private
- #case_offense(node, range) private
- #ends_with_num?(name) ⇒ Boolean private
- #forbidden_names private
- #forbidden_offense(node, range, name) private
- #issue_offenses(node, range, name) private
- #length_offense(node, range) private
- #long_enough?(name) ⇒ Boolean private
- #min_length private
- #name_type(node) private
- #num_offense(node, range) private
- #uppercase?(name) ⇒ Boolean private
Instance Method Details
#allow_nums (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/uncommunicative_name.rb', line 95
def allow_nums cop_config['AllowNamesEndingInNumbers'] end
#allowed_names (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/uncommunicative_name.rb', line 87
def allowed_names cop_config['AllowedNames'] end
#arg_range(arg, length) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/uncommunicative_name.rb', line 78
def arg_range(arg, length) begin_pos = arg.source_range.begin_pos Parser::Source::Range.new(processed_source.buffer, begin_pos, begin_pos + length) end
#case_offense(node, range) (private)
[ GitHub ]#check(node, args)
[ GitHub ]# File 'lib/rubocop/cop/mixin/uncommunicative_name.rb', line 12
def check(node, args) args.each do |arg| # Argument names might be "_" or prefixed with "_" to indicate they # are unused. Trim away this prefix and only analyse the basename. name_child = arg.children.first next if name_child.nil? full_name = name_child.to_s next if full_name == '_' name = full_name.gsub(/\A(_+)/, '') next if allowed_names.include?(name) length = full_name.size length += 1 if arg.restarg_type? length += 2 if arg.kwrestarg_type? range = arg_range(arg, length) issue_offenses(node, range, name) end end
#ends_with_num?(name) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/mixin/uncommunicative_name.rb', line 64
def ends_with_num?(name) /\d/.match?(name[-1]) end
#forbidden_names (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/uncommunicative_name.rb', line 91
def forbidden_names cop_config['ForbiddenNames'] end
#forbidden_offense(node, range, name) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/uncommunicative_name.rb', line 83
def forbidden_offense(node, range, name) add_offense(range, message: format(FORBIDDEN_MSG, name: name, name_type: name_type(node))) end
#issue_offenses(node, range, name) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/uncommunicative_name.rb', line 36
def issue_offenses(node, range, name) forbidden_offense(node, range, name) if forbidden_names.include?(name) case_offense(node, range) if uppercase?(name) length_offense(node, range) unless long_enough?(name) return if allow_nums num_offense(node, range) if ends_with_num?(name) end
#length_offense(node, range) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/uncommunicative_name.rb', line 68
def length_offense(node, range) = format(LENGTH_MSG, name_type: name_type(node).capitalize, min: min_length) add_offense(range, message: ) end
#long_enough?(name) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/mixin/uncommunicative_name.rb', line 74
def long_enough?(name) name.size >= min_length end
#min_length (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/uncommunicative_name.rb', line 99
def min_length cop_config['MinNameLength'] end
#name_type(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/uncommunicative_name.rb', line 53
def name_type(node) @name_type ||= case node.type when :block then 'block parameter' when :def, :defs then 'method parameter' end end
#num_offense(node, range) (private)
[ GitHub ]
#uppercase?(name) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/mixin/uncommunicative_name.rb', line 49
def uppercase?(name) /[[:upper:]]/.match?(name) end