module RuboCop::Cop::ConfigurableFormatting

Shared functionality between mixins that enforce naming conventions

Public Instance Methods

check_name(node, name, name_range) click to toggle source
# File lib/rubocop/cop/mixin/configurable_formatting.rb, line 8
def check_name(node, name, name_range)
  return if operator?(name)

  if valid_name?(node, name)
    correct_style_detected
  else
    add_offense(node, location: name_range, message: message(style)) do
      report_opposing_styles(node, name)
    end
  end
end
class_emitter_method?(node, name) click to toggle source

A class emitter method is a singleton method in a class/module, where the method has the same name as a class defined in the class/module.

# File lib/rubocop/cop/mixin/configurable_formatting.rb, line 35
def class_emitter_method?(node, name)
  return false unless node.parent && node.defs_type?
  # a class emitter method may be defined inside `def self.included`,
  # `def self.extended`, etc.
  node = node.parent while node.parent.defs_type?

  node.parent.each_child_node(:class).any? do |c|
    c.loc.name.is?(name.to_s)
  end
end
report_opposing_styles(node, name) click to toggle source
# File lib/rubocop/cop/mixin/configurable_formatting.rb, line 20
def report_opposing_styles(node, name)
  alternative_styles.each do |alternative|
    if valid_name?(node, name, alternative)
      return unexpected_style_detected(alternative)
    end
  end
end
valid_name?(node, name, given_style = style) click to toggle source
# File lib/rubocop/cop/mixin/configurable_formatting.rb, line 28
def valid_name?(node, name, given_style = style)
  name.match(self.class::FORMATS.fetch(given_style)) ||
    class_emitter_method?(node, name)
end