class RuboCop::Cop::Lint::UselessConstantScoping
Checks for useless constant scoping. Private constants must be defined using ‘private_constant`. Even if `private` access modifier is used, it is public scope despite its appearance.
It does not support autocorrection due to behavior change and multiple ways to fix it. Or a public constant may be intended.
@example
# bad class Foo private PRIVATE_CONST = 42 end # good class Foo PRIVATE_CONST = 42 private_constant :PRIVATE_CONST end # good class Foo PUBLIC_CONST = 42 # If private scope is not intended. end
Constants
- MSG
Public Instance Methods
Source
# File lib/rubocop/cop/lint/useless_constant_scoping.rb, line 40 def on_casgn(node) return unless after_private_modifier?(node.left_siblings) return if private_constantize?(node.right_siblings, node.name) add_offense(node) end
Private Instance Methods
Source
# File lib/rubocop/cop/lint/useless_constant_scoping.rb, line 49 def after_private_modifier?(left_siblings) access_modifier_candidates = left_siblings.compact.select do |left_sibling| left_sibling.respond_to?(:send_type?) && left_sibling.send_type? end access_modifier_candidates.any? do |candidate| candidate.command?(:private) && candidate.arguments.none? end end
Source
# File lib/rubocop/cop/lint/useless_constant_scoping.rb, line 59 def private_constantize?(right_siblings, const_value) private_constant_arguments = right_siblings.map { |node| private_constants(node) } private_constant_values = private_constant_arguments.flatten.filter_map do |constant| constant.value.to_sym if constant.respond_to?(:value) end private_constant_values.include?(const_value) end