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 =
/^@?[\da-z_]+[!?=]?$/
CAMEL_CASE =
/^@?[a-z][\da-zA-Z]+[!?=]?$/

Instance Method Summary collapse

Methods included from ConfigurableEnforcedStyle

#alternative_style, #both_styles_detected, #correct_style_detected, #opposite_style_detected, #parameter_name, #style, #unrecognized_style_detected

Instance Method Details

#after_dot(node, method_name_length, regexp) ⇒ Object

Returns a range containing the method name after the given regexp and a dot.



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

def after_dot(node, method_name_length, regexp)
  expr = node.loc.expression
  match = /\A#{regexp}\s*\.\s*/.match(expr.source)
  return unless match
  offset = match[0].length
  begin_pos = expr.begin_pos + offset
  Parser::Source::Range.new(expr.source_buffer, begin_pos,
                            begin_pos + method_name_length)
end

#check(node, range) ⇒ Object



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

def check(node, range)
  return unless range

  name = range.source.to_sym
  return if operator?(name)

  if matches_config?(name)
    correct_style_detected
  else
    add_offense(node, range, message(style)) do
      opposite_style_detected
    end
  end
end

#matches_config?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def matches_config?(name)
  name =~ (style == :snake_case ? SNAKE_CASE : CAMEL_CASE)
end