/
show_cops.rb
76 lines (61 loc) · 2.09 KB
/
show_cops.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
# frozen_string_literal: true
module RuboCop
class CLI
module Command
# Shows the given cops, or all cops by default, and their configurations
# for the current directory.
class ShowCops < Base
self.command_name = :show_cops
def initialize(env)
super
# Load the configs so the require()s are done for custom cops
@config = @config_store.for(Dir.pwd)
end
def run
print_available_cops
end
private
def print_available_cops
registry = Cop::Cop.registry
show_all = @options[:show_cops].empty?
if show_all
puts "# Available cops (#{registry.length}) " \
"+ config for #{Dir.pwd}: "
end
registry.departments.sort!.each do |department|
print_cops_of_department(registry, department, show_all)
end
end
def print_cops_of_department(registry, department, show_all)
selected_cops = if show_all
cops_of_department(registry, department)
else
selected_cops_of_department(registry, department)
end
puts "# Department '#{department}' (#{selected_cops.length}):" if show_all
print_cop_details(selected_cops)
end
def print_cop_details(cops)
cops.each do |cop|
puts '# Supports --auto-correct' if cop.new(@config).support_autocorrect?
puts "#{cop.cop_name}:"
puts config_lines(cop)
puts
end
end
def selected_cops_of_department(cops, department)
cops_of_department(cops, department).select do |cop|
@options[:show_cops].include?(cop.cop_name)
end
end
def cops_of_department(cops, department)
cops.with_department(department).sort!
end
def config_lines(cop)
cnf = @config.for_cop(cop)
cnf.to_yaml.lines.to_a.drop(1).map { |line| ' ' + line }
end
end
end
end
end