Module: RuboCop::Cop::ConfigurableNumbering

Includes:
ConfigurableEnforcedStyle
Included in:
Style::VariableNumber
Defined in:
lib/rubocop/cop/mixin/configurable_numbering.rb

Overview

This module provides functionality for checking if numbering match the configured EnforcedStyle.

Constant Summary collapse

SNAKE_CASE =
/^@{0,2}[-_a-z]+[_\D]*(_\d)*[!?=]?$/
NORMAL_CASE =
/^@{0,2}-{0,1}_{0,1}[a-zA-Z\d]*(_[a-zA-Z]+\d*)*
[_\D]*[!?=]?$/x
NON_INTEGER =
/^@{0,2}[-_a-z]+[_\D]*[!?=]?$/

Instance Method Summary collapse

Methods included from ConfigurableEnforcedStyle

#alternative_style, #ambiguous_style_detected, #correct_style_detected, #detected_style, #detected_style=, #no_acceptable_style!, #no_acceptable_style?, #opposite_style_detected, #parameter_name, #style, #style_detected, #supported_styles, #unexpected_style_detected

Instance Method Details

#check_name(node, name, name_range) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/rubocop/cop/mixin/configurable_numbering.rb', line 15

def check_name(node, name, name_range)
  return if operator?(name)

  if valid_name?(node, name)
    correct_style_detected
  else
    add_offense(node, name_range, message(style))
  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.

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
# File 'lib/rubocop/cop/mixin/configurable_numbering.rb', line 40

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

#valid_name?(node, name) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rubocop/cop/mixin/configurable_numbering.rb', line 25

def valid_name?(node, name)
  pattern =
    case style
    when :snake_case
      SNAKE_CASE
    when :normalcase
      NORMAL_CASE
    when :non_integer
      NON_INTEGER
    end
  name.match(pattern) || class_emitter_method?(node, name)
end