Class: Herb::Embedded::Diagnostic

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

Overview

A single lint offense. Shaped to match upstream Herb::Diagnostic / Herb::LintOffense (marcoroth/herb#455, unmerged): #to_h's key set mirrors that class's #to_h exactly, so this drops into its socket without rewriting every consumer if/when that PR lands. #file, #line, #column, and #correctable? are this gem's own convenience accessors on top of that shape — upstream has no per-file scope (a single Herb.lint call is per-source) and derives correctability from a rule's static autocorrectable flag rather than a reader.

Constant Summary collapse

SEVERITIES =
%i[error warning info hint].freeze
DEFAULT_SEVERITY =
:error

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file:, rule:, message:, severity:, location:, code: nil, source: nil, correctable: false) ⇒ Diagnostic

rubocop:disable Metrics/ParameterLists -- value object, one flat field per keyword



20
21
22
23
24
25
26
27
28
29
# File 'lib/herb/embedded/diagnostic.rb', line 20

def initialize(file:, rule:, message:, severity:, location:, code: nil, source: nil, correctable: false)
  @file = file
  @rule = rule
  @message = message
  @severity = SEVERITIES.include?(severity) ? severity : DEFAULT_SEVERITY
  @location = location
  @code = code
  @source = source
  @correctable = correctable
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



17
18
19
# File 'lib/herb/embedded/diagnostic.rb', line 17

def code
  @code
end

#fileObject (readonly)

Returns the value of attribute file.



17
18
19
# File 'lib/herb/embedded/diagnostic.rb', line 17

def file
  @file
end

#locationObject (readonly)

Returns the value of attribute location.



17
18
19
# File 'lib/herb/embedded/diagnostic.rb', line 17

def location
  @location
end

#messageObject (readonly)

Returns the value of attribute message.



17
18
19
# File 'lib/herb/embedded/diagnostic.rb', line 17

def message
  @message
end

#ruleObject (readonly)

Returns the value of attribute rule.



17
18
19
# File 'lib/herb/embedded/diagnostic.rb', line 17

def rule
  @rule
end

#severityObject (readonly)

Returns the value of attribute severity.



17
18
19
# File 'lib/herb/embedded/diagnostic.rb', line 17

def severity
  @severity
end

#sourceObject (readonly)

Returns the value of attribute source.



17
18
19
# File 'lib/herb/embedded/diagnostic.rb', line 17

def source
  @source
end

Class Method Details

.from_js(hash, file:) ⇒ Object

rubocop:enable Metrics/ParameterLists



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

def self.from_js(hash, file:)
  new(
    file: file,
    rule: hash["rule"],
    message: hash["message"],
    severity: hash["severity"]&.to_sym,
    location: hash["location"],
    code: hash["code"],
    source: hash["source"],
    correctable: !hash["autofixContext"].nil?,
  )
end

Instance Method Details

#columnObject



49
50
51
# File 'lib/herb/embedded/diagnostic.rb', line 49

def column
  location&.dig("start", "column")
end

#correctable?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/herb/embedded/diagnostic.rb', line 53

def correctable?
  @correctable
end

#lineObject



45
46
47
# File 'lib/herb/embedded/diagnostic.rb', line 45

def line
  location&.dig("start", "line")
end

#to_hObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/herb/embedded/diagnostic.rb', line 57

def to_h
  {
    message: message,
    location: location,
    severity: severity,
    code: code,
    source: source,
    rule: rule,
  }
end