-
Notifications
You must be signed in to change notification settings - Fork 113
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
Showing
3 changed files
with
56 additions
and
31 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
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).flat_map { |file| list(file) } | ||
end | ||
|
||
sig { params(relative_file: String).returns(T::Array[Packwerk::Reference]) } | ||
def list(relative_file) | ||
references_result = @run_context.references_from_file(relative_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 |
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