Class: Spoom::Cli::Deadcode
- Inherits:
-
Thor
- Object
- Thor
- Spoom::Cli::Deadcode
show all
- Extended by:
- T::Sig
- Includes:
- Helper
- Defined in:
- lib/spoom/cli/deadcode.rb
Constant Summary
Constants included
from Helper
Helper::HIGHLIGHT_COLOR
Instance Method Summary
collapse
Methods included from Helper
#blue, #color?, #colorize, #context, #context_requiring_sorbet!, #cyan, #exec_path, #gray, #green, #highlight, #red, #say, #say_error, #say_warning, #yellow
#set_color
Instance Method Details
#deadcode(*paths) ⇒ Object
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/spoom/cli/deadcode.rb', line 52
def deadcode(*paths)
context = self.context
paths << exec_path if paths.empty?
$stderr.puts "Collecting files..."
collector = FileCollector.new(
allow_extensions: options[:allowed_extensions],
allow_mime_types: options[:allowed_mime_types],
exclude_patterns: options[:exclude].map { |p| Pathname.new(File.join(exec_path, p, "**")).cleanpath.to_s },
)
collector.visit_paths(paths)
files = collector.files.sort
if options[:show_files]
$stderr.puts "\nCollected #{blue(files.size.to_s)} files for analysis\n"
files.each do |file|
$stderr.puts " #{gray(file)}"
end
$stderr.puts
end
plugins = Spoom::Deadcode.plugins_from_gemfile_lock(context)
if options[:show_plugins]
$stderr.puts "\nLoaded #{blue(plugins.size.to_s)} plugins\n"
plugins.each do |plugin|
$stderr.puts " #{gray(plugin.class.to_s)}"
end
$stderr.puts
end
index = Spoom::Deadcode::Index.new
$stderr.puts "Indexing #{blue(files.size.to_s)} files..."
files.each do |file|
content = File.read(file)
content = ERB.new(content).src if file.end_with?(".erb")
tree = Spoom::Deadcode.parse_ruby(content, file: file)
Spoom::Deadcode.index_node(index, tree, content, file: file, plugins: plugins)
rescue Spoom::Deadcode::ParserError => e
say_error("Error parsing #{file}: #{e.message}")
next
rescue Spoom::Deadcode::IndexerError => e
say_error("Error indexing #{file}: #{e.message}")
next
end
if options[:show_defs]
$stderr.puts "\nDefinitions:"
index.definitions.each do |name, definitions|
$stderr.puts " #{blue(name)}"
definitions.each do |definition|
$stderr.puts " #{yellow(definition.kind.serialize)} #{gray(definition.location.to_s)}"
end
end
$stderr.puts
end
if options[:show_refs]
$stderr.puts "\nReferences:"
index.references.values.flatten.sort_by(&:name).each do |references|
name = references.name
kind = references.kind.serialize
loc = references.location.to_s
$stderr.puts " #{blue(name)} #{yellow(kind)} #{gray(loc)}"
end
$stderr.puts
end
definitions_count = index.definitions.size.to_s
references_count = index.references.size.to_s
$stderr.puts "Analyzing #{blue(definitions_count)} definitions against #{blue(references_count)} references..."
index.finalize!
dead = index.definitions.values.flatten.select(&:dead?)
if options[:sort] == "name"
dead.sort_by!(&:name)
else
dead.sort_by!(&:location)
end
if dead.empty?
$stderr.puts "\n#{green("No dead code found!")}"
else
$stderr.puts "\nCandidates:"
dead.each do |definition|
$stderr.puts " #{red(definition.full_name)} #{gray(definition.location.to_s)}"
end
$stderr.puts "\n"
$stderr.puts red(" Found #{dead.size} dead candidates")
exit(1)
end
end
|
#remove(location_string) ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/spoom/cli/deadcode.rb', line 150
def remove(location_string)
location = Spoom::Deadcode::Location.from_string(location_string)
context = self.context
remover = Spoom::Deadcode::Remover.new(context)
new_source = remover.remove_location(nil, location)
context.write!("PATCH", new_source)
diff = context.exec("diff -u #{location.file} PATCH")
$stderr.puts T.must(diff.out.lines[2..-1]).join
context.remove!("PATCH")
context.write!(location.file, new_source)
rescue Spoom::Deadcode::Remover::Error => e
say_error("Can't remove code at #{location_string}: #{e.message}")
exit(1)
rescue Spoom::Deadcode::Location::LocationError => e
say_error(e.message)
exit(1)
end
|