Class: RuboCop::Formatter::WorstOffendersFormatter

Inherits:
BaseFormatter
  • Object
show all
Defined in:
lib/rubocop/formatter/worst_offenders_formatter.rb

Overview

This formatter displays the list of offensive files, sorted by number of offenses with the worst offenders first.

Here’s the format:

26 this/file/is/really/bad.rb 3 just/ok.rb – 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.



18
19
20
# File 'lib/rubocop/formatter/worst_offenders_formatter.rb', line 18

def offense_counts
  @offense_counts
end

Instance Method Details

#file_finished(file, offenses) ⇒ Object



25
26
27
28
29
30
# File 'lib/rubocop/formatter/worst_offenders_formatter.rb', line 25

def file_finished(file, offenses)
  unless offenses.empty?
    path = Pathname.new(file).relative_path_from(Pathname.new(Dir.pwd))
    @offense_counts[path] = offenses.size
  end
end

#finished(_inspected_files) ⇒ Object



32
33
34
# File 'lib/rubocop/formatter/worst_offenders_formatter.rb', line 32

def finished(_inspected_files)
  report_summary(@offense_counts)
end

#ordered_offense_counts(offense_counts) ⇒ Object



52
53
54
# File 'lib/rubocop/formatter/worst_offenders_formatter.rb', line 52

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

#report_summary(offense_counts) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rubocop/formatter/worst_offenders_formatter.rb', line 36

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

  output.puts

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

  output.puts
end

#started(target_files) ⇒ Object



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

def started(target_files)
  super
  @offense_counts = {}
end

#total_offense_count(offense_counts) ⇒ Object



56
57
58
# File 'lib/rubocop/formatter/worst_offenders_formatter.rb', line 56

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