-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Discussion] Put files into buckets of responsibilities/seams #145
base: main
Are you sure you want to change the base?
Changes from all commits
3a8b828
6fcd8a5
e46b253
896797e
ac57f56
009df05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Packwerk::Checkers | ||
|
||
- Checker implements `invalid_reference?(reference)`, which takes a `Packwerk::Reference` and returns Boolean. | ||
|
||
### Packwerk::Reference | ||
|
||
- It contains the information about source package and destination package. | ||
|
||
|
||
## Example: DependencyChecker | ||
|
||
- Dependency means any outgoing dependency from the source package. | ||
- If source package is enforcing dependency, it will check if declared dependency includes the destination package. | ||
|
||
|
||
## Example: PrivacyChecker | ||
|
||
- Privacy means any incoming dependency towards the destination package. | ||
- If destination package is enforcing privacy, it till check if source package references non-public constants. | ||
Comment on lines
+1
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Does current implementation leak too much of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
### Configurations | ||
|
||
Includes validators which are outside of primary `check` runs. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would probably go with the comment in https://github.com/Shopify/packwerk/pull/144/files#diff-5ca9dcd54fd0b9eeef94e00ba6adc7822dc83e5dd2bfb9e8d96fd3c356de8b8aR10-R11 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Extracting references | ||
|
||
- Given a `AST::Node`, this module extracts `Packwerk::Reference`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you put it like this, it sounds like everything here works in the context of a single node. However, |
||
- `Packwerk::Reference` contains `constant` information, that are worked by `Inflector`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
require "ast/node" | ||
|
||
module Packwerk | ||
# from file path to node | ||
class FileProcessor | ||
class UnknownFileTypeResult < Offense | ||
def initialize(file:) | ||
|
@@ -20,6 +21,7 @@ def call(file_path) | |
parser = @parser_factory.for_path(file_path) | ||
return [UnknownFileTypeResult.new(file: file_path)] if parser.nil? | ||
|
||
# path to node | ||
node = File.open(file_path, "r", external_encoding: Encoding::UTF_8) do |file| | ||
parser.call(io: file, file_path: file_path) | ||
rescue Parsers::ParseError => e | ||
|
@@ -31,6 +33,7 @@ def call(file_path) | |
node_processor = @node_processor_factory.for(filename: file_path, node: node) | ||
node_visitor = Packwerk::NodeVisitor.new(node_processor: node_processor) | ||
|
||
# node to offence | ||
node_visitor.visit(node, ancestors: [], result: result) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, the content of this conditional doesn't really seem to fit here. I'd rather just hand off to something that processes the AST instead of instantiating processors and visitors here. |
||
end | ||
result | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
require "constant_resolver" | ||
|
||
module Packwerk | ||
# Packwerk used to run as a part of Rubocop. | ||
# Now that Packwerk is a standalone script, this structure shouldn't be necessary. | ||
class RunContext | ||
extend T::Sig | ||
|
||
|
@@ -52,7 +54,48 @@ def initialize( | |
|
||
sig { params(file: String).returns(T::Array[T.nilable(::Packwerk::Offense)]) } | ||
def process_file(file:) | ||
file_processor.call(file) | ||
# 1. file path to node | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's more like file path to AST, but that contains nodes, so... you're not wrong |
||
# It needs to return ancestors relative to node | ||
node, ancestors = file_processor.call(file) | ||
|
||
# Inside NodeProcessor - @reference_extractor.reference_from_node(node, ancestors: ancestors, file_path: @filename) | ||
# 2. node to constant | ||
@constant_name_inspectors.each do |inspector| | ||
constant_name = inspector.constant_name_from_node(node, ancestors: ancestors) | ||
break if constant_name | ||
end | ||
|
||
# Inside ReferenceExtractor - reference_from_constant(constant_name, node: node, ancestors: ancestors, file_path: file_path) if constant_name | ||
# 3. constant to reference | ||
namespace_path = Node.enclosing_namespace_path(node, ancestors: ancestors) | ||
return if local_reference?(constant_name, Node.name_location(node), namespace_path) | ||
|
||
constant = | ||
@context_provider.context_for( | ||
constant_name, | ||
current_namespace_path: namespace_path | ||
) | ||
return if constant&.package.nil? | ||
|
||
relative_path = Pathname.new(file_path).relative_path_from(@root_path).to_s | ||
|
||
source_package = @context_provider.package_from_path(relative_path) | ||
return if source_package == constant.package | ||
|
||
Reference.new(source_package, relative_path, constant) | ||
|
||
# Inside NodeProcessor | ||
# 4. reference to an offence | ||
@checkers.each_with_object([]) do |checker, violations| | ||
next unless checker.invalid_reference?(reference) | ||
offense = Packwerk::ReferenceOffense.new( | ||
location: Node.location(node), | ||
reference: reference, | ||
violation_type: checker.violation_type | ||
) | ||
violations << offense | ||
end | ||
|
||
end | ||
|
||
private | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: This is accurate, but it duplicates the interface definition in
Packwerk::Checker
, so I think in the final docs we should just point out that the interface exists instead of putting the details here