Class: Txray::Offense

Inherits:
Object
  • Object
show all
Defined in:
lib/txray/offense.rb

Constant Summary collapse

SEVERITIES =
%i[low medium high].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rule:, path:, line:, column:, snippet:, scope:, trace: []) ⇒ Offense

Returns a new instance of Offense.



13
14
15
16
17
18
19
20
21
# File 'lib/txray/offense.rb', line 13

def initialize(rule:, path:, line:, column:, snippet:, scope:, trace: [])
  @rule = rule
  @path = path
  @line = line
  @column = column
  @snippet = snippet
  @scope = scope
  @trace = trace
end

Instance Attribute Details

#column ⇒ Object (readonly)

Returns the value of attribute column.



11
12
13
# File 'lib/txray/offense.rb', line 11

def column
  @column
end

#line ⇒ Object (readonly)

Returns the value of attribute line.



11
12
13
# File 'lib/txray/offense.rb', line 11

def line
  @line
end

#path ⇒ Object (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/txray/offense.rb', line 11

def path
  @path
end

#rule ⇒ Object (readonly)

Returns the value of attribute rule.



11
12
13
# File 'lib/txray/offense.rb', line 11

def rule
  @rule
end

#scope ⇒ Object (readonly)

Returns the value of attribute scope.



11
12
13
# File 'lib/txray/offense.rb', line 11

def scope
  @scope
end

#snippet ⇒ Object (readonly)

Returns the value of attribute snippet.



11
12
13
# File 'lib/txray/offense.rb', line 11

def snippet
  @snippet
end

#trace ⇒ Object (readonly)

Returns the value of attribute trace.



11
12
13
# File 'lib/txray/offense.rb', line 11

def trace
  @trace
end

Instance Method Details

#id ⇒ Object



23
# File 'lib/txray/offense.rb', line 23

def id = rule.id

#key ⇒ Object



29
# File 'lib/txray/offense.rb', line 29

def key = [ rule.id, path, line, column ]

#location ⇒ Object



26
# File 'lib/txray/offense.rb', line 26

def location = "#{path}:#{line}:#{column}"

#message ⇒ Object



27
# File 'lib/txray/offense.rb', line 27

def message = format(rule.message, snippet: snippet, scope: scope.label)

#severity ⇒ Object



24
# File 'lib/txray/offense.rb', line 24

def severity = rule.severity

#severity_rank ⇒ Object



25
# File 'lib/txray/offense.rb', line 25

def severity_rank = SEVERITIES.index(severity) || 0

#sort_key ⇒ Object



31
# File 'lib/txray/offense.rb', line 31

def sort_key = [ -severity_rank, path, line, column ]

#to_h ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/txray/offense.rb', line 33

def to_h
  {
    rule: rule.id,
    severity: severity,
    category: rule.category,
    path: path,
    line: line,
    column: column,
    message: message,
    snippet: snippet,
    scope: { kind: scope.kind, label: scope.label, path: scope.path, line: scope.line },
    trace: trace.map { |frame| { label: frame.label, path: frame.path, line: frame.line } },
    remedy: rule.remedy
  }
end