-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathcheck_all_exposed_funs_tested.roc
More file actions
342 lines (263 loc) · 9.35 KB
/
check_all_exposed_funs_tested.roc
File metadata and controls
342 lines (263 loc) · 9.35 KB
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
app [main!] { pf: platform "../platform/main.roc" }
import pf.Stdout
import pf.Arg exposing [Arg]
import pf.File
import pf.Cmd
import pf.Env
import pf.Path
import pf.Sleep
# This script performs the following tasks:
# 1. Reads the file platform/main.roc and extracts the exposes list.
# 2. For each module in the exposes list, it reads the corresponding file (e.g., platform/Path.roc) and extracts all functions in the module list.
# 3. Checks if each module.function is used in the examples or tests folder using ripgrep
# 4. Prints the functions that are not used anywhere in the examples or tests.
## For convenient string errors
err_s = |err_msg| Err(StrErr(err_msg))
main! : List Arg => Result {} _
main! = |_args|
# Check if ripgrep is installed
_ = Cmd.exec!("rg", ["--version"]) ? RipgrepNotInstalled
cwd = Env.cwd!({}) ? FailedToGetCwd
Stdout.line!("Current working directory: ${Path.display(cwd)}")?
path_to_platform_main = "platform/main.roc"
main_content =
File.read_utf8!(path_to_platform_main)?
exposed_modules =
extract_exposes_list(main_content)?
# Uncomment for debugging
# Stdout.line!("Found exposed modules: ${Str.join_with(exposed_modules, ", ")}")?
module_name_and_functions = List.map_try!(
exposed_modules,
|module_name|
process_module!(module_name),
)?
tagged_functions =
module_name_and_functions
|> List.map_try!(
|{ module_name, exposed_functions }|
List.map_try!(
exposed_functions,
|function_name|
if is_function_unused!(module_name, function_name)? then
Ok(NotFound("${module_name}.${function_name}"))
else
Ok(Found("${module_name}.${function_name}")),
),
)?
not_found_functions =
tagged_functions
|> List.join
|> List.map(
|tagged_function|
when tagged_function is
Found _ -> ""
NotFound qualified_function_name -> qualified_function_name,
)
|> List.keep_if(|s| !Str.is_empty(s))
if List.is_empty(not_found_functions) then
Ok({})
else
Stdout.line!("Functions not used in basic-cli/examples or basic-cli/tests:")?
List.for_each_try!(
not_found_functions,
|function_name|
Stdout.line!(function_name),
)?
# Sleep to fix print order
Sleep.millis!(1000)
Err(Exit(1, "I found untested functions, see above."))
is_function_unused! : Str, Str => Result Bool _
is_function_unused! = |module_name, function_name|
function_pattern = "${module_name}.${function_name}"
search_dirs = ["examples", "tests"]
# Check current working directory
cwd = Env.cwd!({}) ? FailedToGetCwd2
# Check if directories exist
List.for_each_try!(
search_dirs,
|search_dir|
is_dir_res = File.is_dir!(search_dir)
when is_dir_res is
Ok is_dir ->
if !is_dir then
err_s("Error: Path '${search_dir}' inside ${Path.display(cwd)} is not a directory.")
else
Ok({})
Err (PathErr NotFound) ->
err_s("Error: Directory '${search_dir}' does not exist in ${Path.display(cwd)}")
Err err ->
err_s("Error checking directory '${search_dir}': ${Inspect.to_str(err)}")
)?
unused_in_dir =
search_dirs
|> List.map_try!( |search_dir|
# Skip searching if directory doesn't exist
dir_exists = File.is_dir!(search_dir)?
if !dir_exists then
Ok(Bool.true) # Consider unused if we can't search
else
# Use ripgrep to search for the function pattern
cmd_res =
Cmd.exec!("rg", ["-q", function_pattern, search_dir])
when cmd_res is
Ok(_) -> Ok(Bool.false) # Function is used (not unused)
_ -> Ok(Bool.true)
)?
unused_in_dir
|> List.walk!(Bool.true, |state, is_unused_res| state && is_unused_res)
|> Ok
process_module! : Str => Result { module_name : Str, exposed_functions : List Str } _
process_module! = |module_name|
module_path = "platform/${module_name}.roc"
module_source_code =
File.read_utf8!(module_path)?
module_items =
extract_module_list(module_source_code)?
module_functions =
module_items
|> List.keep_if(starts_with_lowercase)
Ok({ module_name, exposed_functions: module_functions })
expect
input =
"""
exposes [
Path,
File,
Http
]
"""
output =
extract_exposes_list(input)
output == Ok(["Path", "File", "Http"])
# extra comma
expect
input =
"""
exposes [
Path,
File,
Http,
]
"""
output = extract_exposes_list(input)
output == Ok(["Path", "File", "Http"])
# single line
expect
input = "exposes [Path, File, Http]"
output = extract_exposes_list(input)
output == Ok(["Path", "File", "Http"])
# empty list
expect
input = "exposes []"
output = extract_exposes_list(input)
output == Ok([])
# multiple spaces
expect
input = "exposes [Path]"
output = extract_exposes_list(input)
output == Ok(["Path"])
extract_exposes_list : Str -> Result (List Str) _
extract_exposes_list = |source_code|
when Str.split_first(source_code, "exposes") is
Ok { after } ->
trimmed_after = Str.trim(after)
if Str.starts_with(trimmed_after, "[") then
list_content = Str.replace_first(trimmed_after, "[", "")
when Str.split_first(list_content, "]") is
Ok { before } ->
modules =
before
|> Str.split_on(",")
|> List.map(Str.trim)
|> List.keep_if(|s| !Str.is_empty(s))
Ok(modules)
Err _ ->
err_s("Could not find closing bracket for exposes list in source code:\n\t${source_code}")
else
err_s("Could not find opening bracket after 'exposes' in source code:\n\t${source_code}")
Err _ ->
err_s("Could not find exposes section in source_code:\n\t${source_code}")
expect
input =
"""
module [
Path,
display,
from_str,
IOErr
]
"""
output = extract_module_list(input)
output == Ok(["Path", "display", "from_str", "IOErr"])
# extra comma
expect
input =
"""
module [
Path,
display,
from_str,
IOErr,
]
"""
output = extract_module_list(input)
output == Ok(["Path", "display", "from_str", "IOErr"])
expect
input =
"module [Path, display, from_str, IOErr]"
output = extract_module_list(input)
output == Ok(["Path", "display", "from_str", "IOErr"])
expect
input =
"module []"
output = extract_module_list(input)
output == Ok([])
# with extra space
expect
input =
"module [Path]"
output = extract_module_list(input)
output == Ok(["Path"])
extract_module_list : Str -> Result (List Str) _
extract_module_list = |source_code|
when Str.split_first(source_code, "module") is
Ok { after } ->
trimmed_after = Str.trim(after)
if Str.starts_with(trimmed_after, "[") then
list_content = Str.replace_first(trimmed_after, "[", "")
when Str.split_first(list_content, "]") is
Ok { before } ->
items =
before
|> Str.split_on(",")
|> List.map(Str.trim)
|> List.keep_if(|s| !Str.is_empty(s))
Ok(items)
Err _ ->
err_s("Could not find closing bracket for module list in source code:\n\t${source_code}")
else
err_s("Could not find opening bracket after 'module' in source code:\n\t${source_code}")
Err _ ->
err_s("Could not find module section in source_code:\n\t${source_code}")
expect starts_with_lowercase("hello") == Bool.true
expect starts_with_lowercase("Hello") == Bool.false
expect starts_with_lowercase("!hello") == Bool.false
expect starts_with_lowercase("") == Bool.false
starts_with_lowercase : Str -> Bool
starts_with_lowercase = |str|
if Str.is_empty(str) then
Bool.false
else
first_char_byte =
str
|> Str.to_utf8
|> List.first
|> impossible_err("We verified that the string is not empty")
# ASCII lowercase letters range from 97 ('a') to 122 ('z')
first_char_byte >= 97 and first_char_byte <= 122
impossible_err = |result, err_msg|
when result is
Ok something ->
something
Err err ->
crash "This should have been impossible: ${err_msg}.\n\tError was: ${Inspect.to_str(err)}"