123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Formatter::OffenseCountFormatter

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

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/offense_count_formatter.rb', line 17

attr_reader :offense_counts

Instance Method Details

#cop_information(cop_name)

[ GitHub ]

  
# File 'lib/rubocop/formatter/offense_count_formatter.rb', line 81

def cop_information(cop_name)
  cop = RuboCop::Cop::Registry.global.find_by_cop_name(cop_name).new

  if cop.correctable?
    safety = cop.safe_autocorrect? ? 'Safe' : 'Unsafe'
    correctable = Rainbow(" [#{safety} Correctable]").yellow
  end

  "#{cop_name}#{correctable}#{@style_guide_links[cop_name]}"
end

#file_finished(_file, offenses)

[ GitHub ]

  
# File 'lib/rubocop/formatter/offense_count_formatter.rb', line 42

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
  @offending_files_count += 1 unless offenses.empty?
  @progressbar.increment if instance_variable_defined?(:@progressbar)
end

#finished(_inspected_files)

[ GitHub ]

  
# File 'lib/rubocop/formatter/offense_count_formatter.rb', line 51

def finished(_inspected_files)
  report_summary(@offense_counts, @offending_files_count)
end

#ordered_offense_counts(offense_counts)

[ GitHub ]

  
# File 'lib/rubocop/formatter/offense_count_formatter.rb', line 73

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

#report_summary(offense_counts, offending_files_count)

[ GitHub ]

  
# File 'lib/rubocop/formatter/offense_count_formatter.rb', line 56

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

  output.puts

  column_width = total_count.to_s.length + 2
  per_cop_counts.each do |cop_name, count|
    output.puts "#{count.to_s.ljust(column_width)}#{cop_information(cop_name)}"
  end
  output.puts '--'
  output.puts "#{total_count}  Total in #{offending_files_count} files"

  output.puts
end

#started(target_files)

[ GitHub ]

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

def started(target_files)
  super
  @offense_counts = Hash.new(0)
  @offending_files_count = 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)

[ GitHub ]

  
# File 'lib/rubocop/formatter/offense_count_formatter.rb', line 77

def total_offense_count(offense_counts)
  offense_counts.values.sum
end