Skip to content

Commit

Permalink
Create privacy protected package
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Evanczuk committed Oct 31, 2022
1 parent cc41029 commit 9fda03d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 37 deletions.
15 changes: 11 additions & 4 deletions lib/packwerk/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,29 @@ def package_path?(path)
# These functions to get information about package privacy concerns will soon be removed
sig { returns(T.nilable(T.any(T::Boolean, T::Array[String]))) }
def enforce_privacy
ReferenceChecking::Checkers::PrivacyChecker.enforce_privacy(self)
privacy_protected_package.enforce_privacy
end

sig { returns(String) }
def public_path
@public_path ||= ReferenceChecking::Checkers::PrivacyChecker.public_path(self)
privacy_protected_package.public_path
end

sig { params(path: String).returns(T::Boolean) }
def public_path?(path)
ReferenceChecking::Checkers::PrivacyChecker.public_path?(self, path)
privacy_protected_package.public_path?(path)
end

sig { returns(T.nilable(String)) }
def user_defined_public_path
ReferenceChecking::Checkers::PrivacyChecker.user_defined_public_path(self)
privacy_protected_package.user_defined_public_path
end

sig { returns(ReferenceChecking::Checkers::PrivacyChecker::PrivacyProtectedPackage) }
def privacy_protected_package
@privacy_protected_package ||= T.let(@privacy_protected_package,
T.nilable(ReferenceChecking::Checkers::PrivacyChecker::PrivacyProtectedPackage))
@privacy_protected_package ||= ReferenceChecking::Checkers::PrivacyChecker::PrivacyProtectedPackage.from(self)
end

sig { params(other: T.untyped).returns(T.nilable(Integer)) }
Expand Down
36 changes: 3 additions & 33 deletions lib/packwerk/reference_checking/checkers/privacy_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,14 @@ module ReferenceChecking
module Checkers
# Checks whether a given reference references a private constant of another package.
class PrivacyChecker
extend ActiveSupport::Autoload
autoload :PrivacyProtectedPackage

extend T::Sig
include Checker

VIOLATION_TYPE = T.let("privacy", String)

class << self
extend T::Sig

sig { params(package: Package).returns(String) }
def public_path(package)
unprefixed_public_path = user_defined_public_path(package) || "app/public/"

if package.root?
unprefixed_public_path
else
File.join(package.name, unprefixed_public_path)
end
end

sig { params(package: Package).returns(T.nilable(String)) }
def user_defined_public_path(package)
return unless package.config["public_path"]
return package.config["public_path"] if package.config["public_path"].end_with?("/")

package.config["public_path"] + "/"
end

sig { params(package: Package, path: String).returns(T::Boolean) }
def public_path?(package, path)
path.start_with?(public_path(package))
end

sig { params(package: Package).returns(T.nilable(T.any(T::Boolean, T::Array[String]))) }
def enforce_privacy(package)
package.config["enforce_privacy"]
end
end

sig { override.returns(String) }
def violation_type
VIOLATION_TYPE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# 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
class PrivacyProtectedPackage < T::Struct
extend T::Sig

const :public_path, String
const :user_defined_public_path, T.nilable(String)
const :enforce_privacy, T.nilable(T.any(T::Boolean, T::Array[String]))

sig { params(path: String).returns(T::Boolean) }
def public_path?(path)
path.start_with?(public_path)
end

class << self
extend T::Sig

sig { params(package: Package).returns(PrivacyProtectedPackage) }
def from(package)
PrivacyProtectedPackage.new(
public_path: public_path_for(package),
user_defined_public_path: user_defined_public_path(package),
enforce_privacy: package.config["enforce_privacy"],
)
end

sig { params(package: Package).returns(T.nilable(String)) }
def user_defined_public_path(package)
return unless package.config["public_path"]
return package.config["public_path"] if package.config["public_path"].end_with?("/")

package.config["public_path"] + "/"
end

sig { params(package: Package).returns(String) }
def public_path_for(package)
unprefixed_public_path = user_defined_public_path(package) || "app/public/"

if package.root?
unprefixed_public_path
else
File.join(package.name, unprefixed_public_path)
end
end
end
end
end
end
end
end

0 comments on commit 9fda03d

Please sign in to comment.