/
vmissdoc.v
296 lines (277 loc) · 8.21 KB
/
vmissdoc.v
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
// Copyright (c) 2020 Lars Pontoppidan. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
import os
import flag
const (
tool_name = 'v missdoc'
tool_version = '0.1.0'
tool_description = 'Prints all V functions in .v files under PATH/, that do not yet have documentation comments.'
work_dir_prefix = normalise_path(os.real_path(os.wd_at_startup) + os.path_separator)
)
struct UndocumentedFN {
file string
line int
signature string
tags []string
}
struct Options {
show_help bool
collect_tags bool
deprecated bool
private bool
js bool
no_line_numbers bool
exclude []string
relative_paths bool
mut:
verify bool
diff bool
additional_args []string
}
fn (opt Options) collect_undocumented_functions_in_dir(directory string) []UndocumentedFN {
mut files := []string{}
collect(directory, mut files, fn (npath string, mut accumulated_paths []string) {
if !npath.ends_with('.v') {
return
}
if npath.ends_with('_test.v') {
return
}
accumulated_paths << npath
})
mut undocumented_fns := []UndocumentedFN{}
for file in files {
if !opt.js && file.ends_with('.js.v') {
continue
}
if opt.exclude.len > 0 && opt.exclude.any(file.contains(it)) {
continue
}
undocumented_fns << opt.collect_undocumented_functions_in_file(file)
}
return undocumented_fns
}
fn (opt &Options) collect_undocumented_functions_in_file(nfile string) []UndocumentedFN {
file := os.real_path(nfile)
contents := os.read_file(file) or { panic(err) }
lines := contents.split('\n')
mut list := []UndocumentedFN{}
mut comments := []string{}
mut tags := []string{}
for i, line in lines {
line_trimmed := line.trim_space()
if line.starts_with('//') {
comments << line
} else if line_trimmed.starts_with('@[') || line_trimmed.starts_with('[') {
tags << collect_tags(line)
} else if line.starts_with('pub fn')
|| (opt.private && (line.starts_with('fn ') && !(line.starts_with('fn C.')
|| line.starts_with('fn main')))) {
if comments.len == 0 {
clean_line := line.all_before_last(' {')
list << UndocumentedFN{
line: i + 1
signature: clean_line
tags: tags
file: file
}
}
tags = []
comments = []
} else {
tags = []
comments = []
}
}
return list
}
fn (opt &Options) collect_undocumented_functions_in_path(path string) []UndocumentedFN {
mut undocumented_functions := []UndocumentedFN{}
if os.is_file(path) {
undocumented_functions << opt.collect_undocumented_functions_in_file(path)
} else {
undocumented_functions << opt.collect_undocumented_functions_in_dir(path)
}
return undocumented_functions
}
fn (opt &Options) report_undocumented_functions_in_path(path string) int {
list := opt.collect_undocumented_functions_in_path(path)
return opt.report_undocumented_functions(list)
}
fn (opt &Options) report_undocumented_functions(list []UndocumentedFN) int {
mut nreports := 0
if list.len > 0 {
for undocumented_fn in list {
mut line_numbers := '${undocumented_fn.line}:0:'
if opt.no_line_numbers {
line_numbers = ''
}
tags_str := if opt.collect_tags && undocumented_fn.tags.len > 0 {
'${undocumented_fn.tags}'
} else {
''
}
file := undocumented_fn.file
ofile := if opt.relative_paths {
file.replace(work_dir_prefix, '')
} else {
os.real_path(file)
}
if opt.deprecated {
println('${ofile}:${line_numbers}${undocumented_fn.signature} ${tags_str}')
nreports++
} else {
mut has_deprecation_tag := false
for tag in undocumented_fn.tags {
if tag.starts_with('deprecated') {
has_deprecation_tag = true
break
}
}
if !has_deprecation_tag {
println('${ofile}:${line_numbers}${undocumented_fn.signature} ${tags_str}')
nreports++
}
}
}
}
return nreports
}
fn (opt &Options) diff_undocumented_functions_in_paths(path_old string, path_new string) []UndocumentedFN {
old := os.real_path(path_old)
new := os.real_path(path_new)
mut old_undocumented_functions := opt.collect_undocumented_functions_in_path(old)
mut new_undocumented_functions := opt.collect_undocumented_functions_in_path(new)
mut differs := []UndocumentedFN{}
if new_undocumented_functions.len > old_undocumented_functions.len {
for new_undoc_fn in new_undocumented_functions {
new_relative_file := new_undoc_fn.file.replace(new, '').trim_string_left(os.path_separator)
mut found := false
for old_undoc_fn in old_undocumented_functions {
old_relative_file := old_undoc_fn.file.replace(old, '').trim_string_left(os.path_separator)
if new_relative_file == old_relative_file
&& new_undoc_fn.signature == old_undoc_fn.signature {
found = true
break
}
}
if !found {
differs << new_undoc_fn
}
}
}
differs.sort_with_compare(sort_undoc_fns)
return differs
}
fn sort_undoc_fns(a &UndocumentedFN, b &UndocumentedFN) int {
if a.file < b.file {
return -1
}
if a.file > b.file {
return 1
}
// same file sort by signature
else {
if a.signature < b.signature {
return -1
}
if a.signature > b.signature {
return 1
}
return 0
}
}
fn normalise_path(path string) string {
return path.replace('\\', '/')
}
fn collect(path string, mut l []string, f fn (string, mut []string)) {
if !os.is_dir(path) {
return
}
mut files := os.ls(path) or { return }
for file in files {
p := normalise_path(os.join_path_single(path, file))
if os.is_dir(p) && !os.is_link(p) {
collect(p, mut l, f)
} else if os.exists(p) {
f(p, mut l)
}
}
return
}
fn collect_tags(line string) []string {
mut cleaned := line.all_before('/')
cleaned = cleaned.replace_each(['@[', '', '[', '', ']', '', ' ', ''])
return cleaned.split(',')
}
fn main() {
mut fp := flag.new_flag_parser(os.args[1..]) // skip the "v" command.
fp.application(tool_name)
fp.version(tool_version)
fp.description(tool_description)
fp.arguments_description('PATH [PATH]...')
fp.skip_executable() // skip the "missdoc" command.
// Collect tool options
mut opt := Options{
show_help: fp.bool('help', `h`, false, 'Show this help text.')
deprecated: fp.bool('deprecated', `d`, false, 'Include deprecated functions in output.')
private: fp.bool('private', `p`, false, 'Include private functions in output.')
js: fp.bool('js', 0, false, 'Include JavaScript functions in output.')
no_line_numbers: fp.bool('no-line-numbers', `n`, false, 'Exclude line numbers in output.')
collect_tags: fp.bool('tags', `t`, false, 'Also print function tags if any is found.')
exclude: fp.string_multi('exclude', `e`, '')
relative_paths: fp.bool('relative-paths', `r`, false, 'Use relative paths in output.')
diff: fp.bool('diff', 0, false, 'exit(1) and show difference between two PATH inputs, return 0 otherwise.')
verify: fp.bool('verify', 0, false, 'exit(1) if documentation is missing, 0 otherwise.')
}
opt.additional_args = fp.finalize() or { panic(err) }
if opt.show_help {
println(fp.usage())
exit(0)
}
if opt.additional_args.len == 0 {
println(fp.usage())
eprintln('Error: ${tool_name} is missing PATH input')
exit(1)
}
// Allow short-long versions to prevent false positive situations, should
// the user miss a `-`. E.g.: the `-verify` flag would be ignored and missdoc
// will return 0 for success plus a list of any undocumented functions.
if '-verify' in opt.additional_args {
opt.verify = true
}
if '-diff' in opt.additional_args {
opt.diff = true
}
if opt.diff {
if opt.additional_args.len < 2 {
println(fp.usage())
eprintln('Error: ${tool_name} --diff needs two valid PATH inputs')
exit(1)
}
path_old := opt.additional_args[0]
path_new := opt.additional_args[1]
if !(os.is_file(path_old) || os.is_dir(path_old)) || !(os.is_file(path_new)
|| os.is_dir(path_new)) {
println(fp.usage())
eprintln('Error: ${tool_name} --diff needs two valid PATH inputs')
exit(1)
}
list := opt.diff_undocumented_functions_in_paths(path_old, path_new)
nreports := opt.report_undocumented_functions(list)
if nreports > 0 {
exit(1)
}
exit(0)
}
mut total := 0
for path in opt.additional_args {
if os.is_file(path) || os.is_dir(path) {
total += opt.report_undocumented_functions_in_path(path)
}
}
if opt.verify && total > 0 {
exit(1)
}
}