Skip to content

Commit

Permalink
proposed cleaner solution
Browse files Browse the repository at this point in the history
  • Loading branch information
exterm committed Mar 19, 2024
1 parent 49f57d1 commit 1a6fc98
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 31 deletions.
1 change: 1 addition & 0 deletions lib/packwerk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module Packwerk
autoload :Reference
autoload :ReferenceOffense
autoload :Validator
autoload :ReferencesFromFile

module OutputStyles
extend ActiveSupport::Autoload
Expand Down
68 changes: 39 additions & 29 deletions lib/packwerk/references_from_file.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
# typed: false
# typed: strict
# frozen_string_literal: true

module Packwerk
# Extracts all static constant references between Ruby files.
# Reuses the packwerk configuration.
# Hackishly hooks into packwerk internals.
class ReferencesFromFile
def initialize(config = Configuration.from_path)
@config = config
# RunContext is a `private_constant` of `Packwerk`
@run_context = RunContext.from_configuration(@config)
end

def list_all(relative_file_paths: [])
# FilesForProcessing is a `private_constant` of `Packwerk`
files = FilesForProcessing.fetch(relative_file_paths:, configuration: @config).files

files.map { |file| list(file) }.flatten(1)
# Extracts all static constant references between Ruby files.
class ReferencesFromFile
extend T::Sig

class FileParserError < RuntimeError
extend T::Sig

sig { params(file: String, offenses: T::Array[Packwerk::Offense]).void }
def initialize(file:, offenses:)
super("Errors while parsing #{file}: #{offenses.map(&:to_s).join("\n")}")
end

def list(relative_file)
file_processor = @run_context.send(:file_processor)
context_provider = @run_context.send(:context_provider)

unresolved_references = file_processor.call(relative_file).unresolved_references

# ReferenceExtractor is a `private_constant` of `Packwerk`
ReferenceExtractor.get_fully_qualified_references_from(
unresolved_references,
context_provider
)
end

sig { params(config: Packwerk::Configuration).void }
def initialize(config = Configuration.from_path)
@config = config
@run_context = T.let(RunContext.from_configuration(@config), RunContext)
end

sig { params(relative_file_paths: T::Array[String]).returns(T::Array[Packwerk::Reference]) }
def list_all(relative_file_paths: [])
files(relative_file_paths: relative_file_paths).map { |file| list(file) }.flatten(1)
end

sig { params(relative_file: String).returns(T::Array[Packwerk::Reference]) }
def list(relative_file)
references_result = @run_context.references_from_file(relative_file:)

if references_result.file_offenses.present?
raise FileParserError.new(file: relative_file, offenses: references_result.file_offenses)
end

references_result.references
end

sig { params(relative_file_paths: T::Array[String]).returns(T::Set[String]) }
def files(relative_file_paths: [])
FilesForProcessing.fetch(relative_file_paths: relative_file_paths, configuration: @config).files
end
end
end
end
18 changes: 16 additions & 2 deletions lib/packwerk/run_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,29 @@ def initialize(

sig { params(relative_file: String).returns(T::Array[Packwerk::Offense]) }
def process_file(relative_file:)
reference_checker = ReferenceChecking::ReferenceChecker.new(@checkers)

references_result = references_from_file(relative_file: relative_file)

references_result.file_offenses +
references_result.references.flat_map { |reference| reference_checker.call(reference) }
end

class FileReferencesResult < T::Struct
const :references, T::Array[Packwerk::Reference]
const :file_offenses, T::Array[Packwerk::Offense]
end

sig { params(relative_file: String).returns(FileReferencesResult) }
def references_from_file(relative_file:)
processed_file = file_processor.call(relative_file)

references = ReferenceExtractor.get_fully_qualified_references_from(
processed_file.unresolved_references,
context_provider
)
reference_checker = ReferenceChecking::ReferenceChecker.new(@checkers)

processed_file.offenses + references.flat_map { |reference| reference_checker.call(reference) }
FileReferencesResult.new(references: references, file_offenses: processed_file.offenses)
end

sig { returns(PackageSet) }
Expand Down

0 comments on commit 1a6fc98

Please sign in to comment.