Module: RuboCop::Cop::ConfigurableNaming

Includes:
ConfigurableEnforcedStyle
Included in:
Style::MethodName, Style::VariableName
Defined in:
lib/rubocop/cop/mixin/configurable_naming.rb

Overview

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

Constant Summary collapse

SNAKE_CASE =
/^@{0,2}[\da-z_]+[!?=]?$/
CAMEL_CASE =
/^@{0,2}_?[a-z][\da-zA-Z]+[!?=]?$/

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



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

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)) do
      opposite_style_detected
    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.

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/mixin/configurable_naming.rb', line 33

def class_emitter_method?(node, name)
  return false unless node.defs_type?
  # a class emitter method may be defined inside `def self.included`,
  # `def self.extended`, etc.
  node = node.parent while node.parent && node.parent.defs_type?
  return false unless node.parent

  node.parent.children.compact.any? do |c|
    c.class_type? && c.loc.name.is?(name.to_s)
  end
end

#valid_name?(node, name) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/rubocop/cop/mixin/configurable_naming.rb', line 26

def valid_name?(node, name)
  pattern = (style == :snake_case ? SNAKE_CASE : CAMEL_CASE)
  name.match(pattern) || class_emitter_method?(node, name)
end