-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alex Evanczuk
committed
Oct 31, 2022
1 parent
cc41029
commit 9fda03d
Showing
3 changed files
with
70 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
lib/packwerk/reference_checking/checkers/privacy_checker/privacy_protected_package.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |