class RuboCop::Cop::Style::ConstantVisibility
This cop checks that constants defined in classes and modules have an explicit visibility declaration. By default, Ruby makes all class- and module constants public, which litters the public API of the class or module. Explicitly declaring a visibility makes intent more clear, and prevents outside actors from touching private state.
@example
# bad class Foo BAR = 42 BAZ = 43 end # good class Foo BAR = 42 private_constant :BAR BAZ = 43 public_constant :BAZ end
Constants
- MSG
Public Instance Methods
on_casgn(node)
click to toggle source
# File lib/rubocop/cop/style/constant_visibility.rb, line 33 def on_casgn(node) return unless class_or_module_scope?(node) return if visibility_declaration?(node) add_offense(node) end
Private Instance Methods
class_or_module_scope?(node)
click to toggle source
# File lib/rubocop/cop/style/constant_visibility.rb, line 48 def class_or_module_scope?(node) node.parent && %i[class module].include?(node.parent.type) end
message(node)
click to toggle source
# File lib/rubocop/cop/style/constant_visibility.rb, line 42 def message(node) _namespace, constant_name, _value = *node format(MSG, constant_name: constant_name) end
visibility_declaration?(node)
click to toggle source
# File lib/rubocop/cop/style/constant_visibility.rb, line 52 def visibility_declaration?(node) _namespace, constant_name, _value = *node node.parent.each_child_node(:send).any? do |child| visibility_declaration_for?(child, constant_name) end end