Created
October 31, 2018 03:06
-
-
Save swarad07/4108656a7aa4f994fa6a981cd182102f to your computer and use it in GitHub Desktop.
Find unused sass variables. Inspired from https://gist.github.com/badsyntax/6193491
This file contains 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
#!/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 | |
# Usage: | |
# Create a file (e.g. unused-variables.sh) having the below code, place it outside your sass folder. | |
# Run chmod +x ./unused-variables.sh to give execute permission to your script. | |
# Run ./unused-variables.sh sass | |
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 -h '^$[a-zA-Z0-9_-][^:]*' {} \; | sed 's/$\([a-zA-Z0-9_-][^:]*\).*/\1/') | |
#for var in $vars; do | |
# echo -n "Occurrences of \"\$$var\":" | |
# find "$1" -type f -name "*.scss" -exec grep --color=never -h "$var" "{}" \; | wc -l | |
#done | grep ' 1$' | |
varNameChars='A-Za-z0-9_-' | |
vars=$(find "$1" -type f -name "*.scss" -exec grep -o "\$[$varNameChars]*:" {} \; | grep -o '[^:]*') | |
for var in $vars; do | |
echo -n $var | |
find "$1" -type f -name "*.scss" -exec grep -o $var"[^$varNameChars]" {} \; | wc -l | |
done | grep ' 1$' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment