Class: RuboCop::Cop::Naming::ConstantName

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/naming/constant_name.rb

Overview

Checks whether constant names are written using SCREAMING_SNAKE_CASE.

To avoid false positives, it ignores cases in which we cannot know for certain the type of value that would be assigned to a constant.

Examples:

# bad
InchInCm = 2.54
INCHinCM = 2.54
Inch_In_Cm = 2.54

# good
INCH_IN_CM = 2.54

Constant Summary collapse

MSG =
'Use SCREAMING_SNAKE_CASE for constants.'
SNAKE_CASE =

Use POSIX character classes, so we allow accented characters rather than just standard ASCII characters

/^[[:digit:][:upper:]_]+$/.freeze

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#class_or_struct_return_method?(node) ⇒ Object



27
28
29
30
31
# File 'lib/rubocop/cop/naming/constant_name.rb', line 27

def_node_matcher :class_or_struct_return_method?, <<~PATTERN
  (send
    (const _ {:Class :Struct}) :new
    ...)
PATTERN

#on_casgn(node) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rubocop/cop/naming/constant_name.rb', line 33

def on_casgn(node)
  value = if node.parent&.or_asgn_type?
            node.parent.expression
          else
            node.expression
          end

  # We cannot know the result of method calls like
  # NewClass = something_that_returns_a_class
  # It's also ok to assign a class constant another class constant,
  # `Class.new(...)` or `Struct.new(...)`
  # SomeClass = SomeOtherClass
  # SomeClass = Class.new(...)
  # SomeClass = Struct.new(...)
  return if allowed_assignment?(value)
  return if SNAKE_CASE.match?(node.name)

  add_offense(node.loc.name)
end