Skip to content

Instantly share code, notes, and snippets.

@andyferra
Created March 3, 2011 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save andyferra/852290 to your computer and use it in GitHub Desktop.
Save andyferra/852290 to your computer and use it in GitHub Desktop.
Helps you identify unused functions in your javascript.
js_files = Dir['*.js', 'lib/*.js', 'messages/*.js']
defined_functions = js_files.map {|file|
[ file, File.read(file).scan( /function ([_a-z0-9]+)/i ).flatten.uniq ]
}.reject {|file_name, functions| functions.empty? }
called_functions = js_files.map {|file|
defined_functions.map {|file_name, functions| functions }.flatten.map do |function|
File.read(file).scan /(?<!function )#{function}/
end
}.flatten.uniq
defined_functions.each do |file_name, functions|
unused_functions = functions.reject {|function| called_functions.include? function }
if unused_functions.any?
puts "\n#{file_name} contains #{unused_functions.count} unused functions:\n"
puts unused_functions.join(', ')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment