tools: add v missdoc --verify mode (#14525)

master
Larpon 2022-05-25 18:06:11 +02:00 committed by GitHub
parent 6c08af63ff
commit 9f5e999b4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 6 deletions

View File

@ -6,7 +6,7 @@ import flag
const ( const (
tool_name = 'v missdoc' tool_name = 'v missdoc'
tool_version = '0.0.4' tool_version = '0.0.5'
tool_description = 'Prints all V functions in .v files under PATH/, that do not yet have documentation comments.' 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) + '/') work_dir_prefix = normalise_path(os.real_path(os.wd_at_startup) + '/')
) )
@ -26,9 +26,10 @@ struct Options {
no_line_numbers bool no_line_numbers bool
exclude []string exclude []string
relative_paths bool relative_paths bool
verify bool
} }
fn (opt Options) report_undocumented_functions_in_path(path string) { fn (opt Options) report_undocumented_functions_in_path(path string) int {
mut files := []string{} mut files := []string{}
collect(path, mut files, fn (npath string, mut accumulated_paths []string) { collect(path, mut files, fn (npath string, mut accumulated_paths []string) {
if !npath.ends_with('.v') { if !npath.ends_with('.v') {
@ -39,6 +40,7 @@ fn (opt Options) report_undocumented_functions_in_path(path string) {
} }
accumulated_paths << npath accumulated_paths << npath
}) })
mut undocumented_fn_total := 0
for file in files { for file in files {
if !opt.js && file.ends_with('.js.v') { if !opt.js && file.ends_with('.js.v') {
continue continue
@ -46,11 +48,12 @@ fn (opt Options) report_undocumented_functions_in_path(path string) {
if opt.exclude.len > 0 && opt.exclude.any(file.contains(it)) { if opt.exclude.len > 0 && opt.exclude.any(file.contains(it)) {
continue continue
} }
opt.report_undocumented_functions_in_file(file) undocumented_fn_total += opt.report_undocumented_functions_in_file(file)
} }
return undocumented_fn_total
} }
fn (opt &Options) report_undocumented_functions_in_file(nfile string) { fn (opt &Options) report_undocumented_functions_in_file(nfile string) int {
file := os.real_path(nfile) file := os.real_path(nfile)
contents := os.read_file(file) or { panic(err) } contents := os.read_file(file) or { panic(err) }
lines := contents.split('\n') lines := contents.split('\n')
@ -116,6 +119,7 @@ fn (opt &Options) report_undocumented_functions_in_file(nfile string) {
} }
} }
} }
return info.len
} }
fn normalise_path(path string) string { fn normalise_path(path string) string {
@ -164,16 +168,21 @@ fn main() {
collect_tags: fp.bool('tags', `t`, false, 'Also print function tags if any is found.') collect_tags: fp.bool('tags', `t`, false, 'Also print function tags if any is found.')
exclude: fp.string_multi('exclude', `e`, '') exclude: fp.string_multi('exclude', `e`, '')
relative_paths: fp.bool('relative-paths', `r`, false, 'Use relative paths in output.') relative_paths: fp.bool('relative-paths', `r`, false, 'Use relative paths in output.')
verify: fp.bool('verify', 0, false, 'exit(1) if documentation is missing, 0 otherwise.')
} }
if opt.show_help { if opt.show_help {
println(fp.usage()) println(fp.usage())
exit(0) exit(0)
} }
mut total := 0
for path in os.args[1..] { for path in os.args[1..] {
if os.is_file(path) { if os.is_file(path) {
opt.report_undocumented_functions_in_file(path) total += opt.report_undocumented_functions_in_file(path)
} else { } else {
opt.report_undocumented_functions_in_path(path) total += opt.report_undocumented_functions_in_path(path)
} }
} }
if opt.verify && total > 0 {
exit(1)
}
} }