Class: Rubocop::Formatter::OffenceCountFormatter

Inherits:
BaseFormatter show all
Defined in:
lib/rubocop/formatter/offence_count_formatter.rb

Overview

This formatter displays the list of offended cops with a count of how many offences of their kind were found. Ordered by desc offence count

Here's the format:

(26) LineLength (3) OneLineConditional

Instance Attribute Summary collapse

Attributes inherited from BaseFormatter

#output

Instance Method Summary collapse

Methods inherited from BaseFormatter

#file_started, #initialize

Constructor Details

This class inherits a constructor from Rubocop::Formatter::BaseFormatter

Instance Attribute Details

#offence_countsObject (readonly)

Returns the value of attribute offence_counts.



13
14
15
# File 'lib/rubocop/formatter/offence_count_formatter.rb', line 13

def offence_counts
  @offence_counts
end

Instance Method Details

#file_finished(file, offences) ⇒ Object



20
21
22
# File 'lib/rubocop/formatter/offence_count_formatter.rb', line 20

def file_finished(file, offences)
  offences.each { |o| @offence_counts[o.cop_name] += 1 }
end

#finished(inspected_files) ⇒ Object



24
25
26
27
# File 'lib/rubocop/formatter/offence_count_formatter.rb', line 24

def finished(inspected_files)
  report_summary(inspected_files.count,
                 ordered_offence_counts(@offence_counts))
end

#ordered_offence_counts(offence_counts) ⇒ Object



40
41
42
# File 'lib/rubocop/formatter/offence_count_formatter.rb', line 40

def ordered_offence_counts(offence_counts)
  Hash[offence_counts.sort_by { |k, v| v }.reverse]
end

#report_summary(file_count, offence_counts) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/rubocop/formatter/offence_count_formatter.rb', line 29

def report_summary(file_count, offence_counts)
  output.puts

  offence_count = total_offence_count(offence_counts)
  offence_counts.each do |cop_name, count|
    output.puts "#{count.to_s.ljust(offence_count.to_s.length + 2)}" +
                "#{cop_name}\n"
  end
  output.puts
end

#started(target_files) ⇒ Object



15
16
17
18
# File 'lib/rubocop/formatter/offence_count_formatter.rb', line 15

def started(target_files)
  super
  @offence_counts = Hash.new(0)
end

#total_offence_count(offence_counts = {}) ⇒ Object



44
45
46
# File 'lib/rubocop/formatter/offence_count_formatter.rb', line 44

def total_offence_count(offence_counts = {})
  offence_counts.values.inject(0, :+)
end