Class: RuboCop::Formatter::OffenseCountFormatter

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

Overview

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

Here’s the format:

26 LineLength 3 OneLineConditional – 29 Total

Instance Attribute Summary collapse

Attributes inherited from BaseFormatter

#options, #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

#offense_countsObject (readonly)

Returns the value of attribute offense_counts.



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

def offense_counts
  @offense_counts
end

Instance Method Details

#file_finished(_file, offenses) ⇒ Object



38
39
40
41
# File 'lib/rubocop/formatter/offense_count_formatter.rb', line 38

def file_finished(_file, offenses)
  offenses.each { |o| @offense_counts[o.cop_name] += 1 }
  @progressbar.increment if instance_variable_defined?(:@progressbar)
end

#finished(_inspected_files) ⇒ Object



43
44
45
# File 'lib/rubocop/formatter/offense_count_formatter.rb', line 43

def finished(_inspected_files)
  report_summary(@offense_counts)
end

#ordered_offense_counts(offense_counts) ⇒ Object

rubocop:enable Metrics/AbcSize



65
66
67
# File 'lib/rubocop/formatter/offense_count_formatter.rb', line 65

def ordered_offense_counts(offense_counts)
  Hash[offense_counts.sort_by { |k, v| [-v, k] }]
end

#report_summary(offense_counts) ⇒ Object

rubocop:disable Metrics/AbcSize



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rubocop/formatter/offense_count_formatter.rb', line 48

def report_summary(offense_counts)
  per_cop_counts = ordered_offense_counts(offense_counts)
  total_count = total_offense_count(offense_counts)

  output.puts

  per_cop_counts.each do |cop_name, count|
    output.puts "#{count.to_s.ljust(total_count.to_s.length + 2)}" \
                "#{cop_name}\n"
  end
  output.puts '--'
  output.puts "#{total_count}  Total"

  output.puts
end

#started(target_files) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rubocop/formatter/offense_count_formatter.rb', line 17

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

  return unless output.tty?

  file_phrase = target_files.count == 1 ? 'file' : 'files'

  # 185/407 files |====== 45 ======>                    |  ETA: 00:00:04
  # %c / %C       |       %w       >         %i         |       %e
  bar_format = " %c/%C #{file_phrase} |%w>%i| %e "

  @progressbar = ProgressBar.create(
    output: output,
    total: target_files.count,
    format: bar_format,
    autostart: false
  )
  @progressbar.start
end

#total_offense_count(offense_counts) ⇒ Object



69
70
71
# File 'lib/rubocop/formatter/offense_count_formatter.rb', line 69

def total_offense_count(offense_counts)
  offense_counts.values.inject(0, :+)
end