Skip to content

Instantly share code, notes, and snippets.

@JoshMock
Created April 16, 2014 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshMock/10920029 to your computer and use it in GitHub Desktop.
Save JoshMock/10920029 to your computer and use it in GitHub Desktop.
Unused functions
# A terrible attempt at listing function names that appear to not be used. Lots
# of false positives, especially for functions whose names are used as
# callbacks into other functions.
codepath=/path/to/javascript
tmpfile=/tmp/allfunctions
tmpfile2=/tmp/filteredfunctions
# search for function names
ack --type=js --output='$1' -h "(\w{3,})\:\s*function" $codepath > $tmpfile
ack --type=js --output='$1' -h "(\w{3,})\s*\=\s*function\s+" $codepath >> $tmpfile
ack --type=js --output='$1' -h "function\s+(\w{3,})\(" $codepath >> $tmpfile
# clean up dupes, remove Backbone/jQuery UI/plugin functions that are known to be called implicitly
cat $tmpfile | sort -u | grep --invert-match 'initialize|success|error|_bind|_create|_delay|_destroy|_focusable|_getCreateEventData|_getCreateOptions|_hide|_hoverable|_init|_off|_on|_setOption|_setOptions|_show|_super|_superApply|_trigger|destroy|disable|enable|option|widget|_renderItem|_renderMenu|_resizeMenu|beforeStop' > $tmpfile2
# find functions that are (potentially) referred to zero times
while read fname
do
uses=`ack --type=js -h --count "$fname\s*\(|this\.$fname|that\.$fname|$fname\.apply|$fname\.bind|$fname\.call|setTimeout\(\w*$fname|setInterval\(\w*$fname|\"$fname\"|\'$fname\'" $codepath`
if [ $uses = '0' ]; then
echo "$fname"
fi
done < $tmpfile2
rm $tmpfile $tmpfile2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment