Skip to content

Instantly share code, notes, and snippets.

@zenspider
Last active November 22, 2023 23:36
Show Gist options
  • Save zenspider/9220892 to your computer and use it in GitHub Desktop.
Save zenspider/9220892 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby -w
$: << "../../sexp_processor/dev/lib"
$: << "lib"
require "ruby_parser"
require "sexp_processor"
require "set"
class Callers < MethodBasedSexpProcessor
def self.expand_dirs_to_files *dirs
extensions = %w[rb rake]
dirs.flatten.map { |p|
if File.directory? p then
Dir[File.join(p, '**', "*.{#{extensions.join(',')}}")]
else
p
end
}.flatten.sort
end
attr_accessor :known, :called
def initialize
self.known = Hash.new { |h,k| h[k] = Set.new }
self.called = Set.new
super
end
def process_defn sexp
super do
method_name = self.method_name[1..-1].to_sym
known[method_name] << klass_name
process_until_empty sexp
end
end
def process_defs sexp
super do
method_name = self.method_name[2..-1].to_sym
known[method_name] << klass_name
process_until_empty sexp
end
end
def process_call sexp
method_name = sexp[2]
method_name = :initialize if method_name == :new
called << method_name
sexp
end
def report
not_called = known.keys - called.to_a
by_class = Hash.new { |h,k| h[k] = [] }
not_called.each do |meth|
known[meth].each do |klass|
by_class[klass] << meth
end
end
puts "These methods MIGHT not be called:"
puts
by_class.sort_by(&:to_s).each do |klass, meths| # :main is a symbol, so to_s
puts klass
puts " #{meths.sort.join "\n "}"
end
end
end
if $0 == __FILE__ then
callers = Callers.new
parser = RubyParser.new
Callers.expand_dirs_to_files(ARGV).each do |path|
warn "processing: #{path}"
callers.process parser.process File.read path
end
callers.report
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment