From 386bd77a3263711f07117366993aeaa5efad1ffc Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Sun, 10 Mar 2024 15:41:52 +0700 Subject: [PATCH] ast, fmt: improve submodule type alias lookup; fix formatting of modules in `$VMODULES` (#20989) --- vlib/v/ast/types.v | 7 +++--- vlib/v/fmt/fmt_vmodules_test.v | 39 +++++++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/vlib/v/ast/types.v b/vlib/v/ast/types.v index 5c0a6ae877ce77..34c4ba5cc41905 100644 --- a/vlib/v/ast/types.v +++ b/vlib/v/ast/types.v @@ -1473,10 +1473,6 @@ fn (t Table) shorten_user_defined_typenames(original_name string, import_aliases } mut parts := original_name.split('.') if parts.len > 1 { - // cur_mod.Type => Type - if t.cmod_prefix != '' && original_name.starts_with(t.cmod_prefix) { - return original_name.all_after(t.cmod_prefix) - } // mod.submod.submod2.Type => submod2.Type if !parts[..parts.len - 1].any(it.contains('[')) { mod_idx := parts.len - 2 @@ -1485,6 +1481,9 @@ fn (t Table) shorten_user_defined_typenames(original_name string, import_aliases } if alias := import_aliases[parts[mod_idx]] { parts[mod_idx] = alias + } else if t.cmod_prefix != '' && original_name.starts_with(t.cmod_prefix) { + // cur_mod.Type => Type + return original_name.all_after(t.cmod_prefix) } return parts[mod_idx..].join('.') } diff --git a/vlib/v/fmt/fmt_vmodules_test.v b/vlib/v/fmt/fmt_vmodules_test.v index 9e740c30ac7215..cc626f344ff0e5 100644 --- a/vlib/v/fmt/fmt_vmodules_test.v +++ b/vlib/v/fmt/fmt_vmodules_test.v @@ -2,10 +2,9 @@ import os const vexe = os.quoted_path(@VEXE) const vmodules_tdir = os.join_path(os.vtmp_dir(), 'fmt_vmodules_test') -const module_tdir = os.join_path(vmodules_tdir, 'foo') fn testsuite_begin() { - os.mkdir_all(module_tdir) or {} + os.mkdir_all(vmodules_tdir) or {} os.setenv('VMODULES', vmodules_tdir, true) } @@ -13,7 +12,9 @@ fn testsuite_end() { os.rmdir_all(vmodules_tdir) or {} } -fn test_fmt_vmodules() { +fn test_fmt_imports() { + mod_tdir := os.join_path(vmodules_tdir, @FN) + os.mkdir_all(mod_tdir)! tfile_content := [ 'import x.json2 as json', 'import datatypes { Stack }', @@ -21,6 +22,34 @@ fn test_fmt_vmodules() { 'const foo = Stack[string]{}', '', ].join_lines() - os.write_file(os.join_path(module_tdir, 'main.v'), tfile_content)! - os.execute_opt('${vexe} fmt -c ${module_tdir}') or { assert false, err.msg() } + os.write_file(os.join_path(mod_tdir, 'main.v'), tfile_content)! + os.execute_opt('${vexe} fmt -c ${mod_tdir}') or { assert false, err.msg() } +} + +fn test_fmt_submod_type_alias() { + mod_tdir := os.join_path(vmodules_tdir, @FN) + mod_src_tdir := os.join_path(mod_tdir, 'src') + submod_tdir := os.join_path(mod_tdir, 'bar', 'baz') + os.mkdir_all(mod_src_tdir)! + os.mkdir_all(submod_tdir)! + tfile_content := [ + 'module ${@FN}', + '', + 'import bar.baz', + '', + 'type MyAlias = baz.Baz', + '', + ].join_lines() + submod_tfile_content := [ + 'module baz', + '', + 'enum BarBaz {', + ' bar', + ' baz', + '}', + '', + ].join_lines() + os.write_file(os.join_path(mod_src_tdir, 'foo.v'), tfile_content)! + os.write_file(os.join_path(submod_tdir, 'baz.v'), submod_tfile_content)! + os.execute_opt('${vexe} fmt -c ${mod_tdir}') or { assert false, err.msg() } }