module RuboCop::Cop::UncommunicativeName

Common functionality shared by Uncommunicative cops

Constants

CASE_MSG
FORBIDDEN_MSG
LENGTH_MSG
NUM_MSG

Public Instance Methods

check(node, args) click to toggle source
# File lib/rubocop/cop/mixin/uncommunicative_name.rb, line 14
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.
    full_name = arg.children.first.to_s
    next if full_name == '_'

    name = full_name.gsub(/\A([_]+)/, '')
    next if (arg.restarg_type? || arg.kwrestarg_type?) && name.empty?
    next if allowed_names.include?(name)

    range = arg_range(arg, name.size)
    issue_offenses(node, range, name)
  end
end

Private Instance Methods

allow_nums() click to toggle source
# File lib/rubocop/cop/mixin/uncommunicative_name.rb, line 102
def allow_nums
  cop_config['AllowNamesEndingInNumbers']
end
allowed_names() click to toggle source
# File lib/rubocop/cop/mixin/uncommunicative_name.rb, line 94
def allowed_names
  cop_config['AllowedNames']
end
arg_range(arg, length) click to toggle source
# File lib/rubocop/cop/mixin/uncommunicative_name.rb, line 79
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) click to toggle source
# File lib/rubocop/cop/mixin/uncommunicative_name.rb, line 41
def case_offense(node, range)
  add_offense(node, location: range,
                    message: format(CASE_MSG, name_type: name_type(node)))
end
ends_with_num?(name) click to toggle source
# File lib/rubocop/cop/mixin/uncommunicative_name.rb, line 64
def ends_with_num?(name)
  name[-1] =~ /\d/
end
forbidden_names() click to toggle source
# File lib/rubocop/cop/mixin/uncommunicative_name.rb, line 98
def forbidden_names
  cop_config['ForbiddenNames']
end
forbidden_offense(node, range, name) click to toggle source
# File lib/rubocop/cop/mixin/uncommunicative_name.rb, line 86
def forbidden_offense(node, range, name)
  add_offense(
    node,
    location: range,
    message: format(FORBIDDEN_MSG, name: name, name_type: name_type(node))
  )
end
issue_offenses(node, range, name) click to toggle source
# File lib/rubocop/cop/mixin/uncommunicative_name.rb, line 32
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) click to toggle source
# File lib/rubocop/cop/mixin/uncommunicative_name.rb, line 68
def length_offense(node, range)
  add_offense(node, location: range,
                    message: format(LENGTH_MSG,
                                    name_type: name_type(node).capitalize,
                                    min: min_length))
end
long_enough?(name) click to toggle source
# File lib/rubocop/cop/mixin/uncommunicative_name.rb, line 75
def long_enough?(name)
  name.size >= min_length
end
min_length() click to toggle source
# File lib/rubocop/cop/mixin/uncommunicative_name.rb, line 106
def min_length
  cop_config['MinNameLength']
end
name_type(node) click to toggle source
# File lib/rubocop/cop/mixin/uncommunicative_name.rb, line 50
def name_type(node)
  @name_type ||= begin
    case node.type
    when :block then 'block parameter'
    when :def, :defs then 'method parameter'
    end
  end
end
num_offense(node, range) click to toggle source
# File lib/rubocop/cop/mixin/uncommunicative_name.rb, line 59
def num_offense(node, range)
  add_offense(node, location: range,
                    message: format(NUM_MSG, name_type: name_type(node)))
end
uppercase?(name) click to toggle source
# File lib/rubocop/cop/mixin/uncommunicative_name.rb, line 46
def uppercase?(name)
  name =~ /[[:upper:]]/
end