123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Formatter::WorstOffendersFormatter

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: RuboCop::Formatter::BaseFormatter
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

Class Method Summary

BaseFormatter - Inherited

Instance Attribute Summary

Instance Method Summary

BaseFormatter - Inherited

#file_finished

Invoked at the end of inspecting each files.

#file_started

Invoked at the beginning of inspecting each files.

#finished

Invoked after all files are inspected or interrupted by user.

#started

Invoked once before any files are inspected.

Constructor Details

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

Instance Attribute Details

#offense_counts (readonly)

[ GitHub ]

  
# File 'lib/rubocop/formatter/worst_offenders_formatter.rb', line 17

attr_reader :offense_counts

Instance Method Details

#file_finished(file, offenses)

[ GitHub ]

  
# File 'lib/rubocop/formatter/worst_offenders_formatter.rb', line 24

def file_finished(file, offenses)
  return if offenses.empty?

  path = Pathname.new(file).relative_path_from(Pathname.new(Dir.pwd))
  @offense_counts[path] = offenses.size
end

#finished(_inspected_files)

[ GitHub ]

  
# File 'lib/rubocop/formatter/worst_offenders_formatter.rb', line 31

def finished(_inspected_files)
  report_summary(@offense_counts)
end

#ordered_offense_counts(offense_counts)

[ GitHub ]

  
# File 'lib/rubocop/formatter/worst_offenders_formatter.rb', line 55

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

#report_summary(offense_counts)

[ GitHub ]

  
# 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)
  file_count = per_file_counts.size

  output.puts

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

  output.puts '--'
  output.puts "#{total_count}  Total in #{file_count} files"

  output.puts
end

#started(target_files)

[ GitHub ]

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

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

#total_offense_count(offense_counts)

[ GitHub ]

  
# File 'lib/rubocop/formatter/worst_offenders_formatter.rb', line 59

def total_offense_count(offense_counts)
  offense_counts.values.sum
end