class Packwerk::ReferenceOffense

Attributes

reference[R]
violation_type[R]

Public Class Methods

new(reference:, violation_type:, location: nil) click to toggle source
Calls superclass method Packwerk::Offense::new
# File lib/packwerk/reference_offense.rb, line 19
def initialize(reference:, violation_type:, location: nil)
  super(file: reference.relative_path, message: build_message(reference, violation_type), location: location)
  @reference = reference
  @violation_type = violation_type
end

Private Instance Methods

build_message(reference, violation_type) click to toggle source
# File lib/packwerk/reference_offense.rb, line 27
    def build_message(reference, violation_type)
      violation_message = case violation_type
      when ViolationType::Privacy
        source_desc = reference.source_package ? "'#{reference.source_package}'" : "here"
        "Privacy violation: '#{reference.constant.name}' is private to '#{reference.constant.package}' but " \
        "referenced from #{source_desc}.\n" \
        "Is there a public entrypoint in '#{reference.constant.package.public_path}' that you can use instead?"
      when ViolationType::Dependency
        "Dependency violation: #{reference.constant.name} belongs to '#{reference.constant.package}', but " \
        "'#{reference.source_package}' does not specify a dependency on " \
        "'#{reference.constant.package}'.\n" \
        "Are we missing an abstraction?\n" \
        "Is the code making the reference, and the referenced constant, in the right packages?\n"
      end

      <<~EOS
        #{violation_message}
        Inference details: this is a reference to #{reference.constant.name} which seems to be defined in #{reference.constant.location}.
        To receive help interpreting or resolving this error message, see: https://github.com/Shopify/packwerk/blob/main/TROUBLESHOOT.md#Troubleshooting-violations
      EOS
    end