class RuboCop::Formatter::OffenseCountFormatter

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

Attributes

offense_counts[R]

Public Instance Methods

file_finished(_file, offenses) click to toggle source
# File lib/rubocop/formatter/offense_count_formatter.rb, line 41
def file_finished(_file, offenses)
  offenses.each { |o| @offense_counts[o.cop_name] += 1 }
  if options[:display_style_guide]
    offenses.each { |o| @style_guide_links[o.cop_name] ||= o.message[/ \(http\S+\)\Z/] }
  end
  @progressbar.increment if instance_variable_defined?(:@progressbar)
end
finished(_inspected_files) click to toggle source
# File lib/rubocop/formatter/offense_count_formatter.rb, line 49
def finished(_inspected_files)
  report_summary(@offense_counts)
end
ordered_offense_counts(offense_counts) click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/rubocop/formatter/offense_count_formatter.rb, line 71
def ordered_offense_counts(offense_counts)
  offense_counts.sort_by { |k, v| [-v, k] }.to_h
end
report_summary(offense_counts) click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/rubocop/formatter/offense_count_formatter.rb, line 54
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}" \
                "#{@style_guide_links[cop_name]}\n"
  end
  output.puts '--'
  output.puts "#{total_count}  Total"

  output.puts
end
started(target_files) click to toggle source
# File lib/rubocop/formatter/offense_count_formatter.rb, line 19
def started(target_files)
  super
  @offense_counts = Hash.new(0)
  @style_guide_links = {}

  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) click to toggle source
# File lib/rubocop/formatter/offense_count_formatter.rb, line 75
def total_offense_count(offense_counts)
  offense_counts.values.sum
end