Module: RuboCop::Cop::ConfigurableFormatting
- Includes:
- ConfigurableEnforcedStyle
- Included in:
- ConfigurableNaming, ConfigurableNumbering
- Defined in:
- lib/rubocop/cop/mixin/configurable_formatting.rb
Overview
Shared functionality between mixins that enforce naming conventions
Instance Method Summary collapse
- #check_name(node, name, name_range) ⇒ Object
-
#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.
- #report_opposing_styles(node, name) ⇒ Object
- #valid_name?(node, name, given_style = style) ⇒ Boolean
Methods included from ConfigurableEnforcedStyle
#alternative_style, #alternative_styles, #ambiguous_style_detected, #correct_style_detected, #detected_style, #detected_style=, #no_acceptable_style!, #no_acceptable_style?, #opposite_style_detected, #style, #style_detected, #style_parameter_name, #supported_styles, #unexpected_style_detected
Instance Method Details
#check_name(node, name, name_range) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# 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(node, location: name_range, message: (style)) do report_opposing_styles(node, name) end 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.
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rubocop/cop/mixin/configurable_formatting.rb', line 34 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) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/rubocop/cop/mixin/configurable_formatting.rb', line 19 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) ⇒ Boolean
27 28 29 30 |
# File 'lib/rubocop/cop/mixin/configurable_formatting.rb', line 27 def valid_name?(node, name, given_style = style) name.match(self.class::FORMATS.fetch(given_style)) || class_emitter_method?(node, name) end |