I have around 100 .js files in my application. I need to find the unused functions from these files.
Which editor or tool can help me?
I have around 100 .js files in my application. I need to find the unused functions from these files.
Which editor or tool can help me?
You can do this using Jsure, a Javascript lint implementation. You'll be looking for the -unused-funs flag
Necromancing for the sake of completeness:
On the frontend you can now use modern browsers' devtools, which now have the "Coverage" tab (at least Chromium-based do).
This tab allows you to "record" user activity for some time and then inspect which parts of JS files haven't been called/executed. Works for CSS too.
In dev tools press Ctrl + Shif + P to bring up the command bar, start typing "Cover.." and the "enable Coverage tab" will pop up. Then press "record" at the bottom area.
I just created the following BASH script for listing all the JavaScript functions which are never called (I'm counting all the occurrences of the given JavaScript function name and if it occurs only once then I assume it's never used):
# the only shell parameter is the path to all ".js" files
echo "Processing content of \"`pwd`/$1\" folder..."
cd "$1"
echo "List of JavaScript functions which are defined but never used:"
JS_FUNCTIONS=`mktemp`
# assume that all JavaScript functions are in files with extension ".js" or ".Js" or ".JS"
# let's assume that all JavaScript functions' headers are on a single line, always starting with keyword "function" at the very beginning of the line
cat `find . -type f | grep -i '\.js'` | grep '^function' | sed 's/^function[ \t]\+//' | sed 's/[ \t]*(.*//' > $JS_FUNCTIONS
# if something is different in your case feel free to change the line above... ;)
MAX=`cat $JS_FUNCTIONS | wc -l`
for (( i = 1 ; i <= MAX ; i++ ))
do
JS_ITEM=`cat $JS_FUNCTIONS | head -$i | tail -1`
ITEM_COUNT=`grep -rn $JS_ITEM | grep -v '://' | wc -l` # use --exclude-dir for "grep" if you want to refine the search
if [ $ITEM_COUNT -lt 2 ]
then
echo " - $JS_ITEM()"
fi
done
rm $JS_FUNCTIONS
Just put it into a list_js.sh file, give execution flag (chmod 744 list_js.sh) and run it with a single parameter which contains all your JavaScript content: ./list_js.sh <relative_folder>