Class: RuboCop::Cop::Lint::ConstantReassignment

Inherits:
Base
  • Object
show all
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.

Examples:

# bad
X = :foo
X = :bar

# bad
class A
  X = :foo
  X = :bar
end

# bad
module A
  X = :foo
  X = :bar
end

# good - keep only one assignment
X = :bar

class A
  X = :bar
end

module A
  X = :bar
end

# good - use OR assignment
X = :foo
X ||= :bar

# good - use conditional assignment
X = :foo
X = :bar unless defined?(X)

# good - remove the assigned constant first
class A
  X = :foo
  remove_const :X
  X = :bar
end

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

#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_gem_version, #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

#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