Sha256: 737aef975ad7c6b6e724ea418c2b8398d3d2cc4dd41dbe2cae320077cb06dff9

Contents?: true

Size: 1.7 KB

Versions: 5

Compression:

Stored size: 1.7 KB

Contents

# typed: strict
# frozen_string_literal: true

module Packwerk
  module ReferenceChecking
    module Checkers
      # Checks whether a given reference references a private constant of another package.
      class PrivacyChecker
        extend T::Sig
        include Checker

        sig { override.returns(Packwerk::ViolationType) }
        def violation_type
          ViolationType::Privacy
        end

        sig do
          override
            .params(reference: Packwerk::Reference)
            .returns(T::Boolean)
        end
        def invalid_reference?(reference)
          return false if reference.constant.public?

          privacy_option = reference.constant.package.enforce_privacy
          return false if enforcement_disabled?(privacy_option)

          return false unless privacy_option == true ||
            explicitly_private_constant?(reference.constant, explicitly_private_constants: privacy_option)

          true
        end

        private

        sig do
          params(
            constant: ConstantDiscovery::ConstantContext,
            explicitly_private_constants: T::Array[String]
          ).returns(T::Boolean)
        end
        def explicitly_private_constant?(constant, explicitly_private_constants:)
          explicitly_private_constants.include?(constant.name) ||
            # nested constants
            explicitly_private_constants.any? { |epc| constant.name.start_with?(epc + "::") }
        end

        sig do
          params(privacy_option: T.nilable(T.any(T::Boolean, T::Array[String])))
            .returns(T::Boolean)
        end
        def enforcement_disabled?(privacy_option)
          [false, nil].include?(privacy_option)
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
packwerk-2.2.0 lib/packwerk/reference_checking/checkers/privacy_checker.rb
packwerk-2.1.1 lib/packwerk/reference_checking/checkers/privacy_checker.rb
packwerk-2.1.0 lib/packwerk/reference_checking/checkers/privacy_checker.rb
packwerk-2.0.0 lib/packwerk/reference_checking/checkers/privacy_checker.rb
packwerk-1.4.0 lib/packwerk/reference_checking/checkers/privacy_checker.rb