Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unused styles translation script #26969

Merged
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6baddfe
adding script to find unused keys
gedu Aug 16, 2023
d798ea6
Separated into 2 functions to find and parse the object names styles …
gedu Aug 18, 2023
4ff465f
Going through the codebase and get keys from the files
gedu Aug 28, 2023
fd39606
showing unused style keys
gedu Aug 29, 2023
d779eec
removed translation code
gedu Sep 1, 2023
c04114b
getting keys from utilities folder to find unused styles
gedu Sep 6, 2023
c2ea419
Code clean up + improvements to the output
gedu Sep 6, 2023
3042c9a
Clean up, used builtins and improved code readability
gedu Sep 7, 2023
6fa51d0
Fixed lint issues
gedu Sep 7, 2023
3424140
typo fixed
gedu Sep 7, 2023
ac8aea2
fixed some lint errors + clean ups
gedu Sep 8, 2023
640ea4b
Merge branch 'main' into edu/22212_unused_styles_translation_script
gedu Sep 8, 2023
ff2174b
removed unused variable
gedu Sep 8, 2023
261adb0
moved unused style script into workflow actions
gedu Sep 11, 2023
82cf626
added find unused styles into lint.yml workflow
gedu Sep 11, 2023
d47feee
fixed some lint issues
gedu Sep 11, 2023
5bf6fec
Merge branch 'main' into edu/22212_unused_styles_translation_script
gedu Sep 12, 2023
ebf2e66
Improved script find objects and fields with arrow function on differ…
gedu Sep 12, 2023
21d380b
Merge branch 'main' into edu/22212_unused_styles_translation_script
gedu Sep 15, 2023
6756074
Updated script to handle styles with theme function, and obj spreadin…
gedu Sep 18, 2023
54e786c
fixed some lint issues
gedu Sep 18, 2023
e1cad0a
Merge branch 'main' into edu/22212_unused_styles_translation_script
gedu Sep 18, 2023
24e4db5
removed unused style
gedu Sep 18, 2023
89f3c2b
Merge branch 'main' into edu/22212_unused_styles_translation_script
gedu Sep 21, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
375 changes: 375 additions & 0 deletions .github/scripts/findUnusedKeys.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,375 @@
#!/bin/bash

# Configurations
declare LIB_PATH
LIB_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd ../../ && pwd)"

readonly SRC_DIR="${LIB_PATH}/src"
readonly STYLES_DIR="${LIB_PATH}/src/styles"
readonly STYLES_FILE="${LIB_PATH}/src/styles/styles.js"
readonly UTILITIES_STYLES_FILE="${LIB_PATH}/src/styles/utilities"
readonly STYLES_KEYS_FILE="${LIB_PATH}/scripts/style_keys_list_temp.txt"
readonly UTILITY_STYLES_KEYS_FILE="${LIB_PATH}/scripts/utility_keys_list_temp.txt"
readonly REMOVAL_KEYS_FILE="${LIB_PATH}/scripts/removal_keys_list_temp.txt"
readonly AMOUNT_LINES_TO_SHOW=3

readonly FILE_EXTENSIONS=('-name' '*.js' '-o' '-name' '*.jsx' '-o' '-name' '*.ts' '-o' '-name' '*.tsx')

source scripts/shellUtils.sh

# trap ctrl-c and call ctrl_c()
trap ctrl_c INT

delete_temp_files() {
find "${LIB_PATH}/scripts" -name "*keys_list_temp*" -type f -exec rm -f {} \;
}

# shellcheck disable=SC2317 # Don't warn about unreachable commands in this function
ctrl_c() {
delete_temp_files
exit 1
}

count_lines() {
local file=$1
if [[ -e "$file" ]]; then
wc -l < "$file"
else
echo "File not found: $file"
fi
}

# Read the style file with unused keys
show_unused_style_keywords() {
while IFS=: read -r key file line_number; do
title "File: $file:$line_number"

# Get lines before and after the error line
local lines_before=$((line_number - AMOUNT_LINES_TO_SHOW))
local lines_after=$((line_number + AMOUNT_LINES_TO_SHOW))

# Read the lines into an array
local lines=()
while IFS= read -r line; do
lines+=("$line")
done < "$file"

# Loop through the lines
for ((i = lines_before; i <= lines_after; i++)); do
local line="${lines[i]}"
# Print context of the error line
echo "$line"
done
error "Unused key: $key"
echo "--------------------------------"
done < "$STYLES_KEYS_FILE"
}

# Function to remove a keyword from the temp file
remove_keyword() {
local keyword="$1"
if grep -q "$keyword" "$STYLES_KEYS_FILE"; then
grep -v "$keyword" "$STYLES_KEYS_FILE" > "$REMOVAL_KEYS_FILE"
mv "$REMOVAL_KEYS_FILE" "$STYLES_KEYS_FILE"

return 0 # Keyword was removed
else
return 1 # Keyword was not found
fi
}

lookfor_unused_keywords() {
# Loop through all files in the src folder
while read -r file; do

# Search for keywords starting with "styles"
while IFS= read -r keyword; do

# Remove any [ ] characters from the keyword
local clean_keyword="${keyword//[\[\]]/}"
# skip styles. keyword that might be used in comments
if [[ "$clean_keyword" == "styles." ]]; then
continue
fi

if ! remove_keyword "$clean_keyword" ; then
# In case of a leaf of the styles object is being used, it means the parent objects is being used
# we need to mark it as used.
if [[ "$clean_keyword" =~ ^styles\.[a-zA-Z0-9_-]+\.[a-zA-Z0-9_-]+$ ]]; then
# Keyword has more than two words, remove words after the second word
local keyword_prefix="${clean_keyword%.*}"
remove_keyword "$keyword_prefix"
fi
fi
done < <(grep -E -o '\bstyles\.[a-zA-Z0-9_.]*' "$file" | grep -v '\/\/' | grep -vE '\/\*.*\*\/')
done < <(find "${SRC_DIR}" -type f \( "${FILE_EXTENSIONS[@]}" \))
}


# Function to find and store keys from a file
find_styles_object_and_store_keys() {
local file="$1"
local base_name="${2:-styles}" # Set styles as default
local line_number=0
local inside_arrow_function=false

while IFS= read -r line; do
((line_number++))

# Check if we are inside an arrow function and we find a closing curly brace
if [[ "$inside_arrow_function" == true ]]; then
if [[ "$line" =~ ^[[:space:]]*\}\) ]]; then
inside_arrow_function=false
fi
continue
fi

# Check if we are inside an arrow function
if [[ "$line" =~ ^[[:space:]]*([a-zA-Z0-9_-])+:[[:space:]]*\(.*\)[[:space:]]*'=>'[[:space:]]*\(\{ || "$line" =~ ^[[:space:]]*(const|let|var)[[:space:]]+([a-zA-Z0-9_-]+)[[:space:]]*=[[:space:]]*\(.*\)[[:space:]]*'=>' ]]; then
inside_arrow_function=true
continue
fi

# Skip lines that are not key-related
if [[ ! "$line" =~ ^[[:space:]]*(const|let|var)[[:space:]]+([a-zA-Z0-9_-]+)[[:space:]]*=[[:space:]]*\{|^[[:space:]]*([a-zA-Z0-9_-]+\.)?[a-zA-Z0-9_-]+:[[:space:]]*\{|^[[:space:]]*\} ]]; then
continue
fi

if [[ "$line" =~ ^[[:space:]]*(const|let|var)[[:space:]]+([a-zA-Z0-9_-]+)[[:space:]]*=[[:space:]]*\{ ]]; then
key="${BASH_REMATCH[2]%%:*{*)}"
echo "styles.${key}|...${key}|${base_name}.${key}:${file}:${line_number}" >> "$STYLES_KEYS_FILE"
fi
done < "$file"
}

find_styles_functions_and_store_keys() {
local file="$1"
local line_number=0
local inside_object=false
local inside_arrow_function=false
local key=""

while IFS= read -r line; do
((line_number++))

# Skip lines that are not key-related
if [[ "${line}" == *styles* ]]; then
continue
fi

# Check if we are inside an arrow function and we find a closing curly brace
if [[ "$inside_arrow_function" == true ]]; then
if [[ "$line" =~ ^[[:space:]]*\}\) ]]; then
inside_arrow_function=false
fi
continue
fi

# Check if we are inside an arrow function
if [[ "${line}" =~ ^[[:space:]]*([a-zA-Z0-9_-])+:[[:space:]]*\(.*\)[[:space:]]*'=>'[[:space:]]*\( ]]; then
inside_arrow_function=true
key="${line%%:*}"
key="${key// /}" # Trim spaces
echo "styles.${key}|...${key}:${file}:${line_number}" >> "$STYLES_KEYS_FILE"
continue
fi

if [[ "$line" =~ ^[[:space:]]*(const|let|var)[[:space:]]+([a-zA-Z0-9_-]+)[[:space:]]*=[[:space:]]*\(.*\)[[:space:]]*'=>' ]]; then
inside_arrow_function=true
key="${BASH_REMATCH[2]}"
key="${key// /}" # Trim spaces
echo "styles.${key}|...${key}:${file}:${line_number}" >> "$STYLES_KEYS_FILE"
continue
fi

done < "$file"
}

find_theme_style_and_store_keys() {
local file="$1"
local start_line_number="$2"
local base_name="${3:-styles}" # Set styles as default
local parent_keys=()
local root_key=""
local line_number=0
local inside_arrow_function=false

while IFS= read -r line; do
((line_number++))

if [ ! "$line_number" -ge "$start_line_number" ]; then
continue
fi

# Check if we are inside an arrow function and we find a closing curly brace
if [[ "$inside_arrow_function" == true ]]; then
if [[ "$line" =~ ^[[:space:]]*\}\) ]]; then
inside_arrow_function=false
fi
continue
fi

# Check if we are inside an arrow function
if [[ "$line" =~ ^[[:space:]]*([a-zA-Z0-9_-])+:[[:space:]]*\(.*\)[[:space:]]*'=>'[[:space:]]*\(\{ || "$line" =~ ^[[:space:]]*(const|let|var)[[:space:]]+([a-zA-Z0-9_-]+)[[:space:]]*=[[:space:]]*\(.*\)[[:space:]]*'=>' ]]; then
inside_arrow_function=true
continue
fi

# Skip lines that are not key-related
if [[ ! "$line" =~ ^[[:space:]]*(const|let|var)[[:space:]]+([a-zA-Z0-9_-]+)[[:space:]]*=[[:space:]]*\{|^[[:space:]]*([a-zA-Z0-9_-]+\.)?[a-zA-Z0-9_-]+:[[:space:]]*\{|^[[:space:]]*\} ]]; then

continue
fi

if [[ "$line" =~ ^[[:space:]]*([a-zA-Z0-9_-]+\.)?[a-zA-Z0-9_-]+:[[:space:]]*\{|^[[:space:]]*([a-zA-Z0-9_-])+:[[:space:]]*\(.*\)[[:space:]]*'=>'[[:space:]]*\(\{ ]]; then
# Removing all the extra lines after the ":"
local key="${line%%:*}"
key="${key// /}" # Trim spaces

if [[ ${#parent_keys[@]} -gt 0 ]]; then
local parent_key_trimmed="${parent_keys[${#parent_keys[@]}-1]// /}" # Trim spaces
key="$parent_key_trimmed.$key"
elif [[ -n "$root_key" ]]; then
local parent_key_trimmed="${root_key// /}" # Trim spaces
key="$parent_key_trimmed.$key"
fi

echo "styles.${key}|...${key}|${base_name}.${key}:${file}:${line_number}" >> "$STYLES_KEYS_FILE"
parent_keys+=("$key")
elif [[ "$line" =~ ^[[:space:]]*\} ]]; then
parent_keys=("${parent_keys[@]:0:${#parent_keys[@]}-1}")
fi
done < "$file"
}

# Given that all the styles are inside of a function, we need to find the function and then look for the styles
collect_theme_keys_from_styles() {
local file="$1"
local line_number=0
local inside_styles=false

while IFS= read -r line; do
((line_number++))

if [[ "$inside_styles" == false ]]; then
if [[ "$line" =~ ^[[:space:]]*(const|let|var)[[:space:]]+([a-zA-Z0-9_-]+)[[:space:]]*=[[:space:]]*\(.*\)[[:space:]]*'=>' ]]; then
key="${BASH_REMATCH[2]}"
key="${key// /}" # Trim spaces
if [[ "$key" == "styles"* ]]; then
inside_styles=true
# Need to start within the style function
((line_number++))
find_theme_style_and_store_keys "$STYLES_FILE" "$line_number"
fi
continue
fi
fi
done < "$file"
}

lookfor_unused_spread_keywords() {
local inside_object=false
local key=""

while IFS= read -r line; do
# Detect the start of an object
if [[ "$line" =~ ^[[:space:]]*([a-zA-Z0-9_-]+\.)?[a-zA-Z0-9_-]+:[[:space:]]*\{ ]]; then
inside_object=true
fi

# Detect the end of an object
if [[ "$line" =~ ^[[:space:]]*\},?$ ]]; then
inside_object=false
fi

# If we're inside an object and the line starts with '...', capture the key
if [[ "$inside_object" == true && "$line" =~ ^[[:space:]]*\.\.\.([a-zA-Z0-9_]+)(\(.+\))?,?$ ]]; then
key="${BASH_REMATCH[1]}"
remove_keyword "...${key}"
fi
done < "$STYLES_FILE"
}

find_utility_styles_store_prefix() {
# Loop through all files in the src folder
while read -r file; do
# Search for keywords starting with "styles"
while IFS= read -r keyword; do
local variable="${keyword##*/}"
local variable_trimmed="${variable// /}" # Trim spaces

echo "$variable_trimmed" >> "$UTILITY_STYLES_KEYS_FILE"
done < <(grep -E -o './utilities/[a-zA-Z0-9_-]+' "$file" | grep -v '\/\/' | grep -vE '\/\*.*\*\/')
done < <(find "${STYLES_DIR}" -type f \( "${FILE_EXTENSIONS[@]}" \))

# Sort and remove duplicates from the temporary file
sort -u -o "${UTILITY_STYLES_KEYS_FILE}" "${UTILITY_STYLES_KEYS_FILE}"
}

find_utility_usage_as_styles() {
while read -r file; do
local root_key
local parent_dir

# Get the folder name, given this utility files are index.js
parent_dir=$(dirname "$file")
root_key=$(basename "${parent_dir}")

if [[ "${root_key}" == "utilities" ]]; then
continue
fi

find_theme_style_and_store_keys "${file}" 0 "${root_key}"
done < <(find "${UTILITIES_STYLES_FILE}" -type f \( "${FILE_EXTENSIONS[@]}" \))
}

lookfor_unused_utilities() {
# Read each utility keyword from the file
while read -r keyword; do
# Creating a copy so later the replacement can reference it
local original_keyword="${keyword}"

# Iterate through all files in "src/styles"
while read -r file; do
# Find all words that match "$keyword.[a-zA-Z0-9_-]+"
while IFS= read -r match; do
# Replace the utility prefix with "styles"
local variable="${match/#$original_keyword/styles}"
# Call the remove_keyword function with the variable
remove_keyword "${variable}"
remove_keyword "${match}"
done < <(grep -E -o "$original_keyword\.[a-zA-Z0-9_-]+" "$file" | grep -v '\/\/' | grep -vE '\/\*.*\*\/')
done < <(find "${STYLES_DIR}" -type f \( "${FILE_EXTENSIONS[@]}" \))
done < "$UTILITY_STYLES_KEYS_FILE"
}

echo "🔍 Looking for styles."
# Find and store the name of the utility files as keys
find_utility_styles_store_prefix
find_utility_usage_as_styles

# Find and store keys from styles.js
find_styles_object_and_store_keys "$STYLES_FILE"
find_styles_functions_and_store_keys "$STYLES_FILE"
collect_theme_keys_from_styles "$STYLES_FILE"

echo "🗄️ Now going through the codebase and looking for unused keys."

# Look for usages of utilities into src/styles
lookfor_unused_utilities
lookfor_unused_spread_keywords
lookfor_unused_keywords

final_styles_line_count=$(count_lines "$STYLES_KEYS_FILE")

if [[ $final_styles_line_count -eq 0 ]]; then
# Exit successfully (status code 0)
delete_temp_files
success "Styles are in a good shape"
exit 0
else
show_unused_style_keywords
delete_temp_files
error "Unused keys: $final_styles_line_count"
exit 1
fi
gedu marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ jobs:
echo 'Error: Prettier diff detected! Please run `npm run prettier` and commit the changes.'
exit 1
fi

- name: Run unused style searcher
shell: bash
run: ./.github/scripts/findUnusedKeys.sh
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"symbolicate:android": "npx metro-symbolicate android/app/build/generated/sourcemaps/react/release/index.android.bundle.map",
"symbolicate:ios": "npx metro-symbolicate main.jsbundle.map",
"test:e2e": "node tests/e2e/testRunner.js --development",
"gh-actions-unused-styles": "./.github/scripts/findUnusedKeys.sh",
"workflow-test": "./workflow_tests/scripts/runWorkflowTests.sh",
"workflow-test:generate": "node workflow_tests/utils/preGenerateTest.js"
},
Expand Down
Loading
Loading