forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindUnusedKeys.sh
executable file
·380 lines (315 loc) · 12.4 KB
/
findUnusedKeys.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/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.ts"
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-Zgv 0-9_-])+:[[:space:]]*\(.*\)[[:space:]]*'=>'[[:space:]]*\(\{ || "$line" =~ ^[[:space:]]*([a-zA-Zgv 0-9_-])+:[[:space:]]*\(.*\)[[:space:]]*'=>' ]]; then
inside_arrow_function=true
continue
fi
if [[ "$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.ts
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