lib/packwerk/run_context.rb in packwerk-2.1.0 vs lib/packwerk/run_context.rb in packwerk-2.1.1
- old
+ new
@@ -1,30 +1,26 @@
-# typed: true
+# typed: strict
# frozen_string_literal: true
require "constant_resolver"
module Packwerk
# Holds the context of a Packwerk run across multiple files.
class RunContext
extend T::Sig
- attr_reader(
- :root_path,
- :load_paths,
- :package_paths,
- :inflector,
- :custom_associations,
- :checker_classes,
- )
+ DEFAULT_CHECKERS = T.let([
+ ::Packwerk::ReferenceChecking::Checkers::DependencyChecker.new,
+ ::Packwerk::ReferenceChecking::Checkers::PrivacyChecker.new,
+ ], T::Array[ReferenceChecking::Checkers::Checker])
- DEFAULT_CHECKERS = [
- ::Packwerk::ReferenceChecking::Checkers::DependencyChecker,
- ::Packwerk::ReferenceChecking::Checkers::PrivacyChecker,
- ]
-
class << self
+ extend T::Sig
+
+ sig do
+ params(configuration: Configuration).returns(RunContext)
+ end
def from_configuration(configuration)
inflector = ActiveSupport::Inflector
new(
root_path: configuration.root_path,
@@ -37,55 +33,75 @@
config_path: configuration.config_path,
)
end
end
+ sig do
+ params(
+ root_path: String,
+ load_paths: T::Array[String],
+ inflector: T.class_of(ActiveSupport::Inflector),
+ cache_directory: Pathname,
+ config_path: T.nilable(String),
+ package_paths: T.nilable(T.any(T::Array[String], String)),
+ custom_associations: AssociationInspector::CustomAssociations,
+ checkers: T::Array[ReferenceChecking::Checkers::Checker],
+ cache_enabled: T::Boolean,
+ ).void
+ end
def initialize(
root_path:,
load_paths:,
+ inflector:,
+ cache_directory:,
+ config_path: nil,
package_paths: nil,
- inflector: nil,
custom_associations: [],
- checker_classes: DEFAULT_CHECKERS,
- cache_enabled: false,
- cache_directory: nil,
- config_path: nil
+ checkers: DEFAULT_CHECKERS,
+ cache_enabled: false
)
@root_path = root_path
@load_paths = load_paths
@package_paths = package_paths
@inflector = inflector
@custom_associations = custom_associations
- @checker_classes = checker_classes
+ @checkers = checkers
@cache_enabled = cache_enabled
@cache_directory = cache_directory
@config_path = config_path
+
+ @file_processor = T.let(nil, T.nilable(FileProcessor))
+ @context_provider = T.let(nil, T.nilable(ConstantDiscovery))
+ # We need to initialize this before we fork the process, see https://github.com/Shopify/packwerk/issues/182
+ @cache = T.let(
+ Cache.new(enable_cache: @cache_enabled, cache_directory: @cache_directory, config_path: @config_path), Cache
+ )
end
- sig { params(file: String).returns(T::Array[Packwerk::Offense]) }
- def process_file(file:)
- unresolved_references_and_offenses = file_processor.call(file)
+ sig { params(absolute_file: String).returns(T::Array[Packwerk::Offense]) }
+ def process_file(absolute_file:)
+ unresolved_references_and_offenses = file_processor.call(absolute_file)
references_and_offenses = ReferenceExtractor.get_fully_qualified_references_and_offenses_from(
unresolved_references_and_offenses,
context_provider
)
- reference_checker = ReferenceChecking::ReferenceChecker.new(checkers)
+ reference_checker = ReferenceChecking::ReferenceChecker.new(@checkers)
references_and_offenses.flat_map { |reference| reference_checker.call(reference) }
end
private
sig { returns(FileProcessor) }
def file_processor
- @file_processor ||= FileProcessor.new(node_processor_factory: node_processor_factory, cache: cache)
+ @file_processor ||= FileProcessor.new(node_processor_factory: node_processor_factory, cache: @cache)
end
sig { returns(NodeProcessorFactory) }
def node_processor_factory
NodeProcessorFactory.new(
context_provider: context_provider,
- root_path: root_path,
+ root_path: @root_path,
constant_name_inspectors: constant_name_inspectors
)
end
sig { returns(ConstantDiscovery) }
@@ -97,35 +113,25 @@
end
sig { returns(ConstantResolver) }
def resolver
ConstantResolver.new(
- root_path: root_path,
- load_paths: load_paths,
- inflector: inflector,
+ root_path: @root_path,
+ load_paths: @load_paths,
+ inflector: @inflector,
)
end
- sig { returns(Cache) }
- def cache
- @cache ||= Cache.new(enable_cache: @cache_enabled, cache_directory: @cache_directory, config_path: @config_path)
- end
-
sig { returns(PackageSet) }
def package_set
- ::Packwerk::PackageSet.load_all_from(root_path, package_pathspec: package_paths)
+ ::Packwerk::PackageSet.load_all_from(@root_path, package_pathspec: @package_paths)
end
- sig { returns(T::Array[ReferenceChecking::Checkers::Checker]) }
- def checkers
- checker_classes.map(&:new)
- end
-
sig { returns(T::Array[ConstantNameInspector]) }
def constant_name_inspectors
[
::Packwerk::ConstNodeInspector.new,
- ::Packwerk::AssociationInspector.new(inflector: inflector, custom_associations: custom_associations),
+ ::Packwerk::AssociationInspector.new(inflector: @inflector, custom_associations: @custom_associations),
]
end
end
end