lib/packwerk/reference_extractor.rb in packwerk-2.1.0 vs lib/packwerk/reference_extractor.rb in packwerk-2.1.1

- old
+ new

@@ -25,23 +25,30 @@ sig do params( node: Parser::AST::Node, ancestors: T::Array[Parser::AST::Node], - file_path: String + absolute_file: String ).returns(T.nilable(UnresolvedReference)) end - def reference_from_node(node, ancestors:, file_path:) + def reference_from_node(node, ancestors:, absolute_file:) constant_name = T.let(nil, T.nilable(String)) @constant_name_inspectors.each do |inspector| constant_name = inspector.constant_name_from_node(node, ancestors: ancestors) break if constant_name end - reference_from_constant(constant_name, node: node, ancestors: ancestors, file_path: file_path) if constant_name + if constant_name + reference_from_constant( + constant_name, + node: node, + ancestors: ancestors, + absolute_file: absolute_file + ) + end end sig do params( unresolved_references_and_offenses: T::Array[T.any(UnresolvedReference, Offense)], @@ -64,15 +71,19 @@ context_provider.context_for( unresolved_reference.constant_name, current_namespace_path: unresolved_reference.namespace_path ) - next if constant&.package.nil? + next if constant.nil? + package_for_constant = constant.package + + next if package_for_constant.nil? + source_package = context_provider.package_from_path(unresolved_reference.relative_path) - next if source_package == constant.package + next if source_package == package_for_constant fully_qualified_references_and_offenses << Reference.new( source_package, unresolved_reference.relative_path, constant, @@ -88,24 +99,24 @@ sig do params( constant_name: String, node: Parser::AST::Node, ancestors: T::Array[Parser::AST::Node], - file_path: String + absolute_file: String ).returns(T.nilable(UnresolvedReference)) end - def reference_from_constant(constant_name, node:, ancestors:, file_path:) + def reference_from_constant(constant_name, node:, ancestors:, absolute_file:) namespace_path = Node.enclosing_namespace_path(node, ancestors: ancestors) return if local_reference?(constant_name, Node.name_location(node), namespace_path) - relative_path = Pathname.new(file_path).relative_path_from(@root_path).to_s + relative_file = Pathname.new(absolute_file).relative_path_from(@root_path).to_s location = Node.location(node) UnresolvedReference.new( constant_name, namespace_path, - relative_path, + relative_file, location ) end def local_reference?(constant_name, name_location, namespace_path)