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

#columnObject (readonly)

Returns the value of attribute column.



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

def column
  @column
end

#lineObject (readonly)

Returns the value of attribute line.



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

def line
  @line
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#ruleObject (readonly)

Returns the value of attribute rule.



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

def rule
  @rule
end

#scopeObject (readonly)

Returns the value of attribute scope.



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

def scope
  @scope
end

#snippetObject (readonly)

Returns the value of attribute snippet.



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

def snippet
  @snippet
end

#traceObject (readonly)

Returns the value of attribute trace.



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

def trace
  @trace
end

Instance Method Details

#idObject



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

def id = rule.id

#keyObject



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

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

#locationObject



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

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

#messageObject



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

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

#severityObject



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

def severity = rule.severity

#severity_rankObject



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

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

#sort_keyObject



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

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

#to_hObject



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