123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::Cop::ConfigurableFormatting

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Defined in: lib/rubocop/cop/mixin/configurable_formatting.rb

Overview

Shared functionality between mixins that enforce naming conventions

Constant Summary

ConfigurableEnforcedStyle - Included

SYMBOL_TO_STRING_CACHE

Instance Attribute Summary

Instance Method Summary

Instance Method Details

#check_name(node, name, name_range)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/configurable_formatting.rb', line 9

def check_name(node, name, name_range)
  if valid_name?(node, name)
    correct_style_detected
  else
    add_offense(name_range, message: message(style)) { report_opposing_styles(node, name) }
  end
end

#class_emitter_method?(node, name) ⇒ Boolean

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.

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/configurable_formatting.rb', line 30

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? { |c| c.loc.name.is?(name.to_s) }
end

#report_opposing_styles(node, name)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/configurable_formatting.rb', line 17

def report_opposing_styles(node, name)
  alternative_styles.each do |alternative|
    return unexpected_style_detected(alternative) if valid_name?(node, name, alternative)
  end
  unrecognized_style_detected
end

#valid_name?(node, name, given_style = style) ⇒ Boolean

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/configurable_formatting.rb', line 24

def valid_name?(node, name, given_style = style)
  name.match?(self.class::FORMATS.fetch(given_style)) || class_emitter_method?(node, name)
end