Shopify/packwerk

Separate filtering from reference extraction

wildmaples opened this issue · 0 comments

We currently extract + filter references all in one step, which can sometimes be a point of confusion.

In particular, we do a lot of filtering:

def reference_from_constant(constant_name, node:, ancestors:, file_path:)
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)
end

You can see we remove

  • locally referenced constants,
  • references to constants packwerk is unable to resolve, or unable to resolve to a package, and
  • references to constants in the same package as the source package of the reference.

There could be a light refactor here so that we can extract all references, followed by another layer to do the filtering. It should lend itself to easier testing and a lighter set of test suites.