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) ⇒ Diagnostic

Returns a new instance of Diagnostic.



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

def initialize(rule:, message:, location:, evidence:, confidence: :high)
  @rule = rule
  @message = message
  @location = location
  @evidence = evidence
  @confidence = confidence
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

#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.



23
24
25
26
27
28
29
# File 'lib/archspec/diagnostic.rb', line 23

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



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/archspec/diagnostic.rb', line 31

def to_h(root:)
  {
    id: fingerprint(root: root),
    rule: rule,
    message: message,
    path: location.relative_path(root),
    line: location.line,
    column: location.column,
    evidence: evidence,
    confidence: confidence.to_s
  }
end