Sha256: 44432e90af6c26e3858653d8b658f8f5a1515d732128df7fcf84a3fa51058f7c

Contents?: true

Size: 1.79 KB

Versions: 6807

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # 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
      #
      class ConstantVisibility < Cop
        MSG = 'Explicitly make `%<constant_name>s` public or private using ' \
              'either `#public_constant` or `#private_constant`.'.freeze

        def on_casgn(node)
          return unless class_or_module_scope?(node)
          return if visibility_declaration?(node)

          add_offense(node)
        end

        private

        def message(node)
          _namespace, constant_name, _value = *node

          format(MSG, constant_name: constant_name)
        end

        def class_or_module_scope?(node)
          node.parent && %i[class module].include?(node.parent.type)
        end

        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

        def_node_matcher :visibility_declaration_for?, <<-PATTERN
          (send nil? {:public_constant :private_constant} ({sym str} %1))
        PATTERN
      end
    end
  end
end

Version data entries

6,807 entries across 6,803 versions & 24 rubygems

Version Path
ory-kratos-client-0.8.2.alpha1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/constant_visibility.rb
ory-client-0.0.1.alpha31 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/constant_visibility.rb
primary_connect_proto-0.4.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/constant_visibility.rb
primary_connect_proto-0.3.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/constant_visibility.rb
primary_connect_proto-0.2.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/constant_visibility.rb
primary_connect_proto-0.1.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/constant_visibility.rb
ory-client-0.0.1.alpha30 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/constant_visibility.rb
ory-client-0.0.1.alpha29 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/constant_visibility.rb
primary_connect_proto-0.0.6 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/constant_visibility.rb
ory-client-0.0.1.alpha28 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/constant_visibility.rb
primary_connect_proto-0.0.5 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/constant_visibility.rb
ory-client-0.0.1.alpha27 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/constant_visibility.rb
ory-client-0.0.1.alpha24 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/constant_visibility.rb
primary_connect_proto-0.0.4 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/constant_visibility.rb
primary_connect_proto-0.0.3 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/constant_visibility.rb
ory-client-0.0.1.alpha23 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/constant_visibility.rb
ory-kratos-client-0.8.0.alpha2 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/constant_visibility.rb
ory-client-0.0.1.alpha21 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/constant_visibility.rb
ory-keto-client-0.7.0.alpha1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/constant_visibility.rb
ory-keto-client-0.7.0.alpha0 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/constant_visibility.rb