Created
March 3, 2011 03:31
-
-
Save theconektd/852290 to your computer and use it in GitHub Desktop.
Helps you identify unused functions in your javascript.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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