forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
offense_count_formatter.rb
83 lines (68 loc) · 2.46 KB
/
offense_count_formatter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# frozen_string_literal: true
require 'ruby-progressbar'
module RuboCop
module Formatter
# 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 in 5 files
class OffenseCountFormatter < BaseFormatter
attr_reader :offense_counts
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
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
def finished(_inspected_files)
report_summary(@offense_counts, @offending_files_count)
end
# rubocop:disable Metrics/AbcSize
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_name}" \
"#{@style_guide_links[cop_name]}\n"
end
output.puts '--'
output.puts "#{total_count} Total in #{offending_files_count} files"
output.puts
end
# rubocop:enable Metrics/AbcSize
def ordered_offense_counts(offense_counts)
offense_counts.sort_by { |k, v| [-v, k] }.to_h
end
def total_offense_count(offense_counts)
offense_counts.values.sum
end
end
end
end