2020-12-05 13:10:54 +03:00
|
|
|
// Copyright (c) 2020 Lars Pontoppidan. All rights reserved.
|
2020-12-11 19:35:25 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2020-12-05 13:10:54 +03:00
|
|
|
import os
|
2020-12-11 19:35:25 +03:00
|
|
|
import flag
|
|
|
|
|
|
|
|
const (
|
2022-04-30 16:09:11 +03:00
|
|
|
tool_name = 'v missdoc'
|
2022-05-27 18:19:06 +03:00
|
|
|
tool_version = '0.1.0'
|
2020-12-11 19:35:25 +03:00
|
|
|
tool_description = 'Prints all V functions in .v files under PATH/, that do not yet have documentation comments.'
|
2022-05-27 18:19:06 +03:00
|
|
|
work_dir_prefix = normalise_path(os.real_path(os.wd_at_startup) + os.path_separator)
|
2020-12-11 19:35:25 +03:00
|
|
|
)
|
2020-12-05 13:10:54 +03:00
|
|
|
|
|
|
|
struct UndocumentedFN {
|
2022-05-27 18:19:06 +03:00
|
|
|
file string
|
2020-12-05 13:10:54 +03:00
|
|
|
line int
|
|
|
|
signature string
|
2020-12-11 19:35:25 +03:00
|
|
|
tags []string
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Options {
|
2022-02-07 14:18:10 +03:00
|
|
|
show_help bool
|
|
|
|
collect_tags bool
|
|
|
|
deprecated bool
|
|
|
|
private bool
|
|
|
|
js bool
|
|
|
|
no_line_numbers bool
|
2022-02-08 12:10:19 +03:00
|
|
|
exclude []string
|
|
|
|
relative_paths bool
|
2022-05-27 18:19:06 +03:00
|
|
|
mut:
|
2022-05-25 19:06:11 +03:00
|
|
|
verify bool
|
2022-05-27 18:19:06 +03:00
|
|
|
diff bool
|
|
|
|
additional_args []string
|
2020-12-05 13:10:54 +03:00
|
|
|
}
|
|
|
|
|
2022-05-27 18:19:06 +03:00
|
|
|
fn (opt Options) collect_undocumented_functions_in_dir(directory string) []UndocumentedFN {
|
2020-12-05 13:10:54 +03:00
|
|
|
mut files := []string{}
|
2022-05-27 18:19:06 +03:00
|
|
|
collect(directory, mut files, fn (npath string, mut accumulated_paths []string) {
|
2022-02-08 12:10:19 +03:00
|
|
|
if !npath.ends_with('.v') {
|
|
|
|
return
|
2020-12-05 13:10:54 +03:00
|
|
|
}
|
2022-02-08 12:10:19 +03:00
|
|
|
if npath.ends_with('_test.v') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
accumulated_paths << npath
|
|
|
|
})
|
2022-05-27 18:19:06 +03:00
|
|
|
mut undocumented_fns := []UndocumentedFN{}
|
2021-01-05 17:13:01 +03:00
|
|
|
for file in files {
|
2022-02-08 12:10:19 +03:00
|
|
|
if !opt.js && file.ends_with('.js.v') {
|
2020-12-05 13:10:54 +03:00
|
|
|
continue
|
|
|
|
}
|
2022-02-08 12:10:19 +03:00
|
|
|
if opt.exclude.len > 0 && opt.exclude.any(file.contains(it)) {
|
2022-02-06 16:44:26 +03:00
|
|
|
continue
|
|
|
|
}
|
2022-05-27 18:19:06 +03:00
|
|
|
undocumented_fns << opt.collect_undocumented_functions_in_file(file)
|
2021-01-05 17:13:01 +03:00
|
|
|
}
|
2022-05-27 18:19:06 +03:00
|
|
|
return undocumented_fns
|
2021-01-05 17:13:01 +03:00
|
|
|
}
|
|
|
|
|
2022-05-27 18:19:06 +03:00
|
|
|
fn (opt &Options) collect_undocumented_functions_in_file(nfile string) []UndocumentedFN {
|
2022-02-08 12:10:19 +03:00
|
|
|
file := os.real_path(nfile)
|
2021-03-01 02:18:14 +03:00
|
|
|
contents := os.read_file(file) or { panic(err) }
|
2021-01-05 17:13:01 +03:00
|
|
|
lines := contents.split('\n')
|
2022-05-27 18:19:06 +03:00
|
|
|
mut list := []UndocumentedFN{}
|
2021-01-05 17:13:01 +03:00
|
|
|
for i, line in lines {
|
2022-02-06 16:44:26 +03:00
|
|
|
if line.starts_with('pub fn') || (opt.private && (line.starts_with('fn ')
|
|
|
|
&& !(line.starts_with('fn C.') || line.starts_with('fn main')))) {
|
2021-01-05 17:13:01 +03:00
|
|
|
// println('Match: $line')
|
|
|
|
if i > 0 && lines.len > 0 {
|
|
|
|
mut line_above := lines[i - 1]
|
|
|
|
if !line_above.starts_with('//') {
|
|
|
|
mut tags := []string{}
|
|
|
|
mut grab := true
|
|
|
|
for j := i - 1; j >= 0; j-- {
|
|
|
|
prev_line := lines[j]
|
|
|
|
if prev_line.contains('}') { // We've looked back to the above scope, stop here
|
|
|
|
break
|
|
|
|
} else if prev_line.starts_with('[') {
|
|
|
|
tags << collect_tags(prev_line)
|
|
|
|
continue
|
|
|
|
} else if prev_line.starts_with('//') { // Single-line comment
|
|
|
|
grab = false
|
|
|
|
break
|
2020-12-05 13:10:54 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-05 17:13:01 +03:00
|
|
|
if grab {
|
|
|
|
clean_line := line.all_before_last(' {')
|
2022-05-27 18:19:06 +03:00
|
|
|
list << UndocumentedFN{
|
|
|
|
line: i + 1
|
|
|
|
signature: clean_line
|
|
|
|
tags: tags
|
|
|
|
file: file
|
|
|
|
}
|
2021-01-05 17:13:01 +03:00
|
|
|
}
|
2020-12-05 13:10:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-05 17:13:01 +03:00
|
|
|
}
|
2022-05-27 18:19:06 +03:00
|
|
|
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 {
|
|
|
|
mut list := opt.collect_undocumented_functions_in_path(path)
|
|
|
|
opt.report_undocumented_functions(list)
|
|
|
|
return list.len
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (opt &Options) report_undocumented_functions(list []UndocumentedFN) {
|
|
|
|
if list.len > 0 {
|
|
|
|
for undocumented_fn in list {
|
2022-02-07 14:18:10 +03:00
|
|
|
mut line_numbers := '$undocumented_fn.line:0:'
|
|
|
|
if opt.no_line_numbers {
|
|
|
|
line_numbers = ''
|
|
|
|
}
|
2021-03-25 00:37:10 +03:00
|
|
|
tags_str := if opt.collect_tags && undocumented_fn.tags.len > 0 {
|
|
|
|
'$undocumented_fn.tags'
|
|
|
|
} else {
|
|
|
|
''
|
|
|
|
}
|
2022-05-27 18:19:06 +03:00
|
|
|
file := undocumented_fn.file
|
2022-02-08 12:10:19 +03:00
|
|
|
ofile := if opt.relative_paths {
|
2022-05-27 18:19:06 +03:00
|
|
|
file.replace(work_dir_prefix, '')
|
2022-02-08 12:10:19 +03:00
|
|
|
} else {
|
2022-05-27 18:19:06 +03:00
|
|
|
os.real_path(file)
|
2022-02-08 12:10:19 +03:00
|
|
|
}
|
2021-01-05 17:13:01 +03:00
|
|
|
if opt.deprecated {
|
2022-02-08 12:10:19 +03:00
|
|
|
println('$ofile:$line_numbers$undocumented_fn.signature $tags_str')
|
2021-01-05 17:13:01 +03:00
|
|
|
} else {
|
2022-04-07 12:20:14 +03:00
|
|
|
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 {
|
2022-02-08 12:10:19 +03:00
|
|
|
println('$ofile:$line_numbers$undocumented_fn.signature $tags_str')
|
2020-12-11 19:35:25 +03:00
|
|
|
}
|
2020-12-05 13:10:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-27 18:19:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-12-05 13:10:54 +03:00
|
|
|
}
|
|
|
|
|
2022-02-08 12:10:19 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-11 19:35:25 +03:00
|
|
|
fn collect_tags(line string) []string {
|
|
|
|
mut cleaned := line.all_before('/')
|
|
|
|
cleaned = cleaned.replace_each(['[', '', ']', '', ' ', ''])
|
|
|
|
return cleaned.split(',')
|
|
|
|
}
|
|
|
|
|
2020-12-05 13:10:54 +03:00
|
|
|
fn main() {
|
2022-05-27 18:19:06 +03:00
|
|
|
mut fp := flag.new_flag_parser(os.args[1..]) // skip the "v" command.
|
2020-12-11 19:35:25 +03:00
|
|
|
fp.application(tool_name)
|
|
|
|
fp.version(tool_version)
|
|
|
|
fp.description(tool_description)
|
|
|
|
fp.arguments_description('PATH [PATH]...')
|
2022-05-27 18:19:06 +03:00
|
|
|
fp.skip_executable() // skip the "missdoc" command.
|
|
|
|
|
2020-12-11 19:35:25 +03:00
|
|
|
// Collect tool options
|
2022-05-27 18:19:06 +03:00
|
|
|
mut opt := Options{
|
2020-12-11 19:35:25 +03:00
|
|
|
show_help: fp.bool('help', `h`, false, 'Show this help text.')
|
|
|
|
deprecated: fp.bool('deprecated', `d`, false, 'Include deprecated functions in output.')
|
2022-02-06 16:44:26 +03:00
|
|
|
private: fp.bool('private', `p`, false, 'Include private functions in output.')
|
|
|
|
js: fp.bool('js', 0, false, 'Include JavaScript functions in output.')
|
2022-02-08 12:10:19 +03:00
|
|
|
no_line_numbers: fp.bool('no-line-numbers', `n`, false, 'Exclude line numbers in output.')
|
2020-12-11 19:35:25 +03:00
|
|
|
collect_tags: fp.bool('tags', `t`, false, 'Also print function tags if any is found.')
|
2022-02-08 12:10:19 +03:00
|
|
|
exclude: fp.string_multi('exclude', `e`, '')
|
|
|
|
relative_paths: fp.bool('relative-paths', `r`, false, 'Use relative paths in output.')
|
2022-05-27 18:19:06 +03:00
|
|
|
diff: fp.bool('diff', 0, false, 'exit(1) and show difference between two PATH inputs, return 0 otherwise.')
|
2022-05-25 19:06:11 +03:00
|
|
|
verify: fp.bool('verify', 0, false, 'exit(1) if documentation is missing, 0 otherwise.')
|
2020-12-11 19:35:25 +03:00
|
|
|
}
|
2022-05-27 18:19:06 +03:00
|
|
|
|
|
|
|
opt.additional_args = fp.finalize() or { panic(err) }
|
|
|
|
|
2020-12-11 19:35:25 +03:00
|
|
|
if opt.show_help {
|
|
|
|
println(fp.usage())
|
|
|
|
exit(0)
|
|
|
|
}
|
2022-05-27 18:19:06 +03:00
|
|
|
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)
|
|
|
|
if list.len > 0 {
|
|
|
|
opt.report_undocumented_functions(list)
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
exit(0)
|
|
|
|
}
|
2022-05-25 19:06:11 +03:00
|
|
|
mut total := 0
|
2022-05-27 18:19:06 +03:00
|
|
|
for path in opt.additional_args {
|
|
|
|
if os.is_file(path) || os.is_dir(path) {
|
2022-05-25 19:06:11 +03:00
|
|
|
total += opt.report_undocumented_functions_in_path(path)
|
2021-01-05 17:13:01 +03:00
|
|
|
}
|
2020-12-05 13:10:54 +03:00
|
|
|
}
|
2022-05-25 19:06:11 +03:00
|
|
|
if opt.verify && total > 0 {
|
|
|
|
exit(1)
|
|
|
|
}
|
2020-12-05 13:10:54 +03:00
|
|
|
}
|