Class: ArchSpec::Diagnostic

Inherits:
Object
  • Object
show all
Defined in:
lib/archspec/diagnostic.rb

Overview

One reported violation: the rule id, a message, the source location, the evidence ArchSpec found, and a confidence. Formatters and the todo file read these. Its #fingerprint is the stable id used to match todo entries and suppress specific findings.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rule:, message:, location:, evidence:, confidence: :high, reason: nil) ⇒ Diagnostic

Returns a new instance of Diagnostic.



13
14
15
16
17
18
19
20
# File 'lib/archspec/diagnostic.rb', line 13

def initialize(rule:, message:, location:, evidence:, confidence: :high, reason: nil)
  @rule = rule
  @message = message
  @location = location
  @evidence = evidence
  @confidence = confidence
  @reason = reason
end

Instance Attribute Details

#confidenceObject (readonly)

Returns the value of attribute confidence.



11
12
13
# File 'lib/archspec/diagnostic.rb', line 11

def confidence
  @confidence
end

#evidenceObject (readonly)

Returns the value of attribute evidence.



11
12
13
# File 'lib/archspec/diagnostic.rb', line 11

def evidence
  @evidence
end

#locationObject (readonly)

Returns the value of attribute location.



11
12
13
# File 'lib/archspec/diagnostic.rb', line 11

def location
  @location
end

#messageObject (readonly)

Returns the value of attribute message.



11
12
13
# File 'lib/archspec/diagnostic.rb', line 11

def message
  @message
end

#reasonObject (readonly)

Returns the value of attribute reason.



11
12
13
# File 'lib/archspec/diagnostic.rb', line 11

def reason
  @reason
end

#ruleObject (readonly)

Returns the value of attribute rule.



11
12
13
# File 'lib/archspec/diagnostic.rb', line 11

def rule
  @rule
end

Instance Method Details

#fingerprint(root: nil) ⇒ Object

Line numbers stay out of the fingerprint so todo entries survive edits that only shift code around.



37
38
39
40
41
42
43
# File 'lib/archspec/diagnostic.rb', line 37

def fingerprint(root: nil)
  path = root ? location.relative_path(root) : location.path

  Digest::SHA256.hexdigest(
    [rule, message, path, evidence].join("\0")
  )[0, 24]
end

#to_h(root:) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/archspec/diagnostic.rb', line 45

def to_h(root:)
  hash = {
    id: fingerprint(root: root),
    rule: rule,
    message: message,
    path: location.relative_path(root),
    line: location.line,
    column: location.column,
    end_line: location.end_line,
    end_column: location.end_column,
    evidence: evidence,
    confidence: confidence.to_s
  }
  hash[:reason] = reason if reason
  hash
end

#with_reason(reason) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/archspec/diagnostic.rb', line 22

def with_reason(reason)
  return self unless reason

  self.class.new(
    rule: rule,
    message: message,
    location: location,
    evidence: evidence,
    confidence: confidence,
    reason: reason
  )
end