Class: RuboCop::Cop::Lint::ConstantReassignment
- Defined in:
- lib/rubocop/cop/lint/constant_reassignment.rb
Overview
Checks for constant reassignments.
Emulates Ruby’s runtime warning “already initialized constant X” when a constant is reassigned in the same file and namespace.
The cop tracks constants defined via ‘NAME = value` syntax as well as class/module keyword definitions. It detects reassignment when a constant is first defined one way and then redefined using the `NAME = value` syntax.
The cop cannot catch all offenses, like, for example, when a constant is reassigned in another file, or when using metaprogramming (‘Module#const_set`).
The cop only takes into account constants assigned in a “simple” way: directly inside class/module definition, or within another constant. Other type of assignments (e.g., inside a conditional) are disregarded.
The cop also tracks constant removal using ‘Module#remove_const` with symbol or string argument.
Constant Summary collapse
- MSG =
'Constant `%<constant>s` is already assigned in this namespace.'- RESTRICT_ON_SEND =
i[remove_const].freeze
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #on_casgn(node) ⇒ Object
- #on_class(node) ⇒ Object
- #on_module(node) ⇒ Object
- #on_send(node) ⇒ Object
- #remove_constant(node) ⇒ Object
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_gem_version, #target_rails_version, #target_ruby_version
Methods included from ExcludeLimit
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
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#on_casgn(node) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/rubocop/cop/lint/constant_reassignment.rb', line 99 def on_casgn(node) return unless fixed_constant_path?(node) return unless simple_assignment?(node) name = fully_qualified_constant_name(node) return constant_definitions[name] = :casgn unless constant_definitions.key?(name) add_offense(node, message: format(MSG, constant: constant_display_name(node))) end |
#on_class(node) ⇒ Object
87 88 89 90 91 |
# File 'lib/rubocop/cop/lint/constant_reassignment.rb', line 87 def on_class(node) return unless unconditional_definition?(node) constant_definitions[definition_name(node)] ||= :class end |
#on_module(node) ⇒ Object
93 94 95 96 97 |
# File 'lib/rubocop/cop/lint/constant_reassignment.rb', line 93 def on_module(node) return unless unconditional_definition?(node) constant_definitions[definition_name(node)] ||= :module end |
#on_send(node) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/rubocop/cop/lint/constant_reassignment.rb', line 109 def on_send(node) constant = remove_constant(node) return unless constant namespaces = ancestor_namespaces(node) return if namespaces.none? constant_definitions.delete(fully_qualified_name_for(namespaces, constant)) end |
#remove_constant(node) ⇒ Object
82 83 84 85 |
# File 'lib/rubocop/cop/lint/constant_reassignment.rb', line 82 def_node_matcher :remove_constant, "(send {nil? self} :remove_const\n ({sym str} $_))\n" |