From 57238a1c6da6e94fbe5373add35e273d4430d34c Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 23 Apr 2026 21:35:49 +0300 Subject: [PATCH] tools: fix vdoc does not generate enum description immediately following $if statement (fixes #23338) --- cmd/tools/vdoc/document/doc.v | 8 +++++ cmd/tools/vdoc/document/doc_test.v | 57 ++++++++++++++++++++++++++++++ cmd/tools/vdoc/vdoc_test.v | 26 ++++++++++++++ 3 files changed, 91 insertions(+) diff --git a/cmd/tools/vdoc/document/doc.v b/cmd/tools/vdoc/document/doc.v index 323d7c245..be460dc0e 100644 --- a/cmd/tools/vdoc/document/doc.v +++ b/cmd/tools/vdoc/document/doc.v @@ -358,6 +358,14 @@ pub fn (mut d Doc) file_ast(mut file_ast ast.File) map[string]DocNode { preceding_comments << comment } continue + } else if stmt.expr is ast.IfExpr && stmt.expr.is_comptime { + comments := ast_comments_to_doc_comments(stmt.expr.post_comments) + if collect_post_module_comments { + post_module_comments << comments + } else { + preceding_comments << comments + } + continue } } // TODO: Fetch head comment once diff --git a/cmd/tools/vdoc/document/doc_test.v b/cmd/tools/vdoc/document/doc_test.v index c6cf89037..ca5633b40 100644 --- a/cmd/tools/vdoc/document/doc_test.v +++ b/cmd/tools/vdoc/document/doc_test.v @@ -1,4 +1,5 @@ // vtest build: tinyc && !musl? && !sanitized_job? +import os import document as doc // fn test_generate_with_pos() {} @@ -113,3 +114,59 @@ written in pure V. ] assert doc.merge_doc_comments(comments) == readme } + +fn test_enum_comments_after_top_level_comptime_if_are_documented() { + mod_dir := os.join_path(os.vtmp_dir(), 'vdoc_issue_23338_${os.getpid()}') + os.rmdir_all(mod_dir) or {} + os.mkdir_all(mod_dir)! + defer { + os.rmdir_all(mod_dir) or {} + } + os.write_file(os.join_path(mod_dir, 'issue_23338.v'), 'module issue_23338 + +\$if macos { +} + +// Foo lorem ipsum foo. +pub enum Foo { + foo +} + +// Bar ipsum lorem bar. +pub enum Bar { + bar +} +')! + mod_doc := doc.generate(mod_dir, false, true, .auto) or { + eprintln(err) + assert false + doc.Doc{} + } + assert mod_doc.contents['Foo']!.merge_comments_without_examples() == 'Foo lorem ipsum foo.' + assert mod_doc.contents['Bar']!.merge_comments_without_examples() == 'Bar ipsum lorem bar.' +} + +fn test_module_comments_after_top_level_comptime_if_stay_on_module() { + mod_dir := os.join_path(os.vtmp_dir(), 'vdoc_issue_23338_module_${os.getpid()}') + os.rmdir_all(mod_dir) or {} + os.mkdir_all(mod_dir)! + defer { + os.rmdir_all(mod_dir) or {} + } + os.write_file(os.join_path(mod_dir, 'issue_23338.v'), 'module issue_23338 + +\$if macos { +} + +// `issue_23338` module overview. + +pub fn foo() {} +')! + mod_doc := doc.generate(mod_dir, false, true, .auto) or { + eprintln(err) + assert false + doc.Doc{} + } + assert mod_doc.head.merge_comments_without_examples() == '`issue_23338` module overview.' + assert mod_doc.contents['foo']!.comments.len == 0 +} diff --git a/cmd/tools/vdoc/vdoc_test.v b/cmd/tools/vdoc/vdoc_test.v index 154039e60..c952507e8 100644 --- a/cmd/tools/vdoc/vdoc_test.v +++ b/cmd/tools/vdoc/vdoc_test.v @@ -219,6 +219,32 @@ pub fn greet() string { fn greet() string' } +fn test_html_keeps_enum_comment_after_top_level_comptime_if() { + mod_dir := 'issue_23338' + os.mkdir(mod_dir)! + os.write_file(os.join_path(mod_dir, 'issue_23338.v'), 'module issue_23338 + +\$if macos { +} + +// Foo lorem ipsum foo. +pub enum Foo { + foo +} + +// Bar ipsum lorem bar. +pub enum Bar { + bar +} +')! + res := os.execute_opt('${vexe_} doc -no-timestamp -m -f html -o - -html-only-contents ${os.quoted_path( + './' + mod_dir)}') or { panic(err) } + assert res.exit_code == 0 + output := res.output.replace('\r\n', '\n') + assert output.contains('Foo lorem ipsum foo.') + assert output.contains('Bar ipsum lorem bar.') +} + fn test_doc_generates_for_modules_without_public_symbols() { mod_dir := 'module_without_public_symbols' os.mkdir(mod_dir)! -- 2.39.5