Skip to content

Instantly share code, notes, and snippets.

@IrenaYuan
Forked from badsyntax/find-unused-sass-variables.sh
Last active September 7, 2017 06:08
Show Gist options
  • Save IrenaYuan/416d15594a2dd38e2dd0a6185eb8e214 to your computer and use it in GitHub Desktop.
Save IrenaYuan/416d15594a2dd38e2dd0a6185eb8e214 to your computer and use it in GitHub Desktop.
Find unused SCSS variables. Usage: `./find-unused-sass-variables.sh sassDir/`
#!/usr/bin/env bash
#
# Approach:
# 1. Find variable declaration in the form of "$my-var: anyvalue"
# 2. Loop through found variables and find occurrences of each variable in all sass files
# 3. Filter out vars that occurred only once
if [ -z "$1" ]; then
echo "Please specify a directory as the first argument."
exit 1
fi
if [ ! -d "$1" ]; then
echo "Not a valid directory."
exit 1
fi
echo "Finding unused variables. This might take some time..."
vars=$(find "$1" -type f -name "*.scss" -exec grep --color=never -nH '^$[a-zA-Z0-9_-]*[^:]' {} \; | sed 's/$\([a-zA-Z0-9_-]*[^:]\).*/\1/')
#echo $vars
for var in $vars; do
file=$(echo $var | cut -d \: -f 1)
line=$(echo $var | cut -d \: -f 2)
varname=$(echo $var | cut -d \: -f 3)
echo -n "file:\"$file\", [line:$line], \$$varname :"
find "$1" -type f -name "*.scss" -exec cat -n "{}" \; | grep "$varname[^a-z]" | wc -l
done | grep ' 1$'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment