Class: RemLint::Offense

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

Overview

One thing a rule found, at one place in one file.

The default rendering is path:line:column: severity: [Rule] message, which vim's default errorformat reads, which GitHub Actions' problem matchers read, and which sort orders correctly. The format is also configurable, following puppet-lint, because the one thing every CI system agrees on is that it wants a slightly different format.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, line:, rule:, message:, column: 1, severity: "warning") ⇒ Offense

Returns a new instance of Offense.



29
30
31
# File 'lib/remlint/offense.rb', line 29

def initialize(path:, line:, rule:, message:, column: 1, severity: "warning")
  super
end

Instance Attribute Details

#columnObject

Returns the value of attribute column

Returns:

  • (Object)

    the current value of column



20
21
22
# File 'lib/remlint/offense.rb', line 20

def column
  @column
end

#lineObject

Returns the value of attribute line

Returns:

  • (Object)

    the current value of line



20
21
22
# File 'lib/remlint/offense.rb', line 20

def line
  @line
end

#messageObject

Returns the value of attribute message

Returns:

  • (Object)

    the current value of message



20
21
22
# File 'lib/remlint/offense.rb', line 20

def message
  @message
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



20
21
22
# File 'lib/remlint/offense.rb', line 20

def path
  @path
end

#ruleObject

Returns the value of attribute rule

Returns:

  • (Object)

    the current value of rule



20
21
22
# File 'lib/remlint/offense.rb', line 20

def rule
  @rule
end

#severityObject

Returns the value of attribute severity

Returns:

  • (Object)

    the current value of severity



20
21
22
# File 'lib/remlint/offense.rb', line 20

def severity
  @severity
end

Instance Method Details

#at_least?(level) ⇒ Boolean

At or above the given severity, where "above" means closer to error.

Returns:

  • (Boolean)


42
43
44
# File 'lib/remlint/offense.rb', line 42

def at_least?(level)
  severity_rank <= (SEVERITIES.index(level) || SEVERITIES.length)
end

#error?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/remlint/offense.rb', line 37

def error?
  severity == "error"
end

#format_with(template) ⇒ Object

%{path}, %{line}, %{column}, %{severity}, %{rule}, %{message}.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/remlint/offense.rb', line 51

def format_with(template)
  format(
    template,
    path:     path,
    line:     line,
    column:   column,
    severity: severity,
    rule:     rule,
    message:  message,
  )
end

#severity_rankObject



33
34
35
# File 'lib/remlint/offense.rb', line 33

def severity_rank
  SEVERITIES.index(severity) || SEVERITIES.length
end

#sort_keyObject

File, then position, then severity: the order someone reads them in.



64
65
66
# File 'lib/remlint/offense.rb', line 64

def sort_key
  [path, line, column, severity_rank, rule]
end

#to_sObject



46
47
48
# File 'lib/remlint/offense.rb', line 46

def to_s
  format_with("%{path}:%{line}:%{column}: %{severity}: [%{rule}] %{message}")
end