Class: RemLint::Formatter
- Inherits:
-
Object
- Object
- RemLint::Formatter
- Defined in:
- lib/remlint/formatter.rb
Overview
Renders offences for a terminal, an editor, or a CI annotation.
The default line is path:line:column: severity: [Rule] message, which
vim's default errorformat already parses and which most CI annotators
recognise. --log-format overrides it with the same %{...} placeholders
puppet-lint uses, so a project that needs GitHub's ::error file=...
shape can have it without a new formatter class.
Constant Summary collapse
- DEFAULT_TEMPLATE =
"%{path}:%{line}:%{column}: %{severity}: [%{rule}] %{message}"
Instance Attribute Summary collapse
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Instance Method Summary collapse
-
#initialize(template: DEFAULT_TEMPLATE) ⇒ Formatter
constructor
A new instance of Formatter.
- #lines(offenses) ⇒ Object
-
#render(offenses) ⇒ Object
One line per offence, then a count.
- #summary(offenses) ⇒ Object
Constructor Details
#initialize(template: DEFAULT_TEMPLATE) ⇒ Formatter
Returns a new instance of Formatter.
18 19 20 |
# File 'lib/remlint/formatter.rb', line 18 def initialize(template: DEFAULT_TEMPLATE) @template = template end |
Instance Attribute Details
#template ⇒ Object (readonly)
Returns the value of attribute template.
16 17 18 |
# File 'lib/remlint/formatter.rb', line 16 def template @template end |
Instance Method Details
#lines(offenses) ⇒ Object
22 23 24 |
# File 'lib/remlint/formatter.rb', line 22 def lines(offenses) offenses.map { |offense| offense.format_with(template) } end |
#render(offenses) ⇒ Object
One line per offence, then a count. Empty output for a clean run: a linter that prints "0 offences" on every commit trains people to stop reading its output.
29 30 31 32 33 34 35 |
# File 'lib/remlint/formatter.rb', line 29 def render(offenses) if offenses.empty? "" else "#{(lines(offenses) + [summary(offenses)]).join("\n")}\n" end end |
#summary(offenses) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/remlint/formatter.rb', line 37 def summary(offenses) counts = SEVERITIES.filter_map do |severity| count = offenses.count { |offense| offense.severity == severity } if count.positive? "#{count} #{severity}#{count == 1 ? '' : 's'}" end end "#{offenses.length} offence#{offenses.length == 1 ? '' : 's'} (#{counts.join(', ')})" end |