Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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