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 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
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
76 77 78 79 80 81 82 |
# File 'lib/rubocop/cop/lint/constant_reassignment.rb', line 76 def on_casgn(node) return unless fixed_constant_path?(node) return unless simple_assignment?(node) return if constant_names.add?(fully_qualified_constant_name(node)) add_offense(node, message: format(MSG, constant: node.name)) end |
#on_send(node) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rubocop/cop/lint/constant_reassignment.rb', line 84 def on_send(node) constant = remove_constant(node) return unless constant namespaces = ancestor_namespaces(node) return if namespaces.none? constant_names.delete(fully_qualified_name_for(namespaces, constant)) end |
#remove_constant(node) ⇒ Object
71 72 73 74 |
# File 'lib/rubocop/cop/lint/constant_reassignment.rb', line 71 def_node_matcher :remove_constant, <<~PATTERN (send _ :remove_const ({sym str} $_)) PATTERN |