Skip to content

Commit 386bd77

Browse files
authored
ast, fmt: improve submodule type alias lookup; fix formatting of modules in $VMODULES (#20989)
1 parent e3140cb commit 386bd77

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

vlib/v/ast/types.v

+3-4
Original file line numberDiff line numberDiff line change
@@ -1473,10 +1473,6 @@ fn (t Table) shorten_user_defined_typenames(original_name string, import_aliases
14731473
}
14741474
mut parts := original_name.split('.')
14751475
if parts.len > 1 {
1476-
// cur_mod.Type => Type
1477-
if t.cmod_prefix != '' && original_name.starts_with(t.cmod_prefix) {
1478-
return original_name.all_after(t.cmod_prefix)
1479-
}
14801476
// mod.submod.submod2.Type => submod2.Type
14811477
if !parts[..parts.len - 1].any(it.contains('[')) {
14821478
mod_idx := parts.len - 2
@@ -1485,6 +1481,9 @@ fn (t Table) shorten_user_defined_typenames(original_name string, import_aliases
14851481
}
14861482
if alias := import_aliases[parts[mod_idx]] {
14871483
parts[mod_idx] = alias
1484+
} else if t.cmod_prefix != '' && original_name.starts_with(t.cmod_prefix) {
1485+
// cur_mod.Type => Type
1486+
return original_name.all_after(t.cmod_prefix)
14881487
}
14891488
return parts[mod_idx..].join('.')
14901489
}

vlib/v/fmt/fmt_vmodules_test.v

+34-5
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,54 @@ import os
22

33
const vexe = os.quoted_path(@VEXE)
44
const vmodules_tdir = os.join_path(os.vtmp_dir(), 'fmt_vmodules_test')
5-
const module_tdir = os.join_path(vmodules_tdir, 'foo')
65

76
fn testsuite_begin() {
8-
os.mkdir_all(module_tdir) or {}
7+
os.mkdir_all(vmodules_tdir) or {}
98
os.setenv('VMODULES', vmodules_tdir, true)
109
}
1110

1211
fn testsuite_end() {
1312
os.rmdir_all(vmodules_tdir) or {}
1413
}
1514

16-
fn test_fmt_vmodules() {
15+
fn test_fmt_imports() {
16+
mod_tdir := os.join_path(vmodules_tdir, @FN)
17+
os.mkdir_all(mod_tdir)!
1718
tfile_content := [
1819
'import x.json2 as json',
1920
'import datatypes { Stack }',
2021
'',
2122
'const foo = Stack[string]{}',
2223
'',
2324
].join_lines()
24-
os.write_file(os.join_path(module_tdir, 'main.v'), tfile_content)!
25-
os.execute_opt('${vexe} fmt -c ${module_tdir}') or { assert false, err.msg() }
25+
os.write_file(os.join_path(mod_tdir, 'main.v'), tfile_content)!
26+
os.execute_opt('${vexe} fmt -c ${mod_tdir}') or { assert false, err.msg() }
27+
}
28+
29+
fn test_fmt_submod_type_alias() {
30+
mod_tdir := os.join_path(vmodules_tdir, @FN)
31+
mod_src_tdir := os.join_path(mod_tdir, 'src')
32+
submod_tdir := os.join_path(mod_tdir, 'bar', 'baz')
33+
os.mkdir_all(mod_src_tdir)!
34+
os.mkdir_all(submod_tdir)!
35+
tfile_content := [
36+
'module ${@FN}',
37+
'',
38+
'import bar.baz',
39+
'',
40+
'type MyAlias = baz.Baz',
41+
'',
42+
].join_lines()
43+
submod_tfile_content := [
44+
'module baz',
45+
'',
46+
'enum BarBaz {',
47+
' bar',
48+
' baz',
49+
'}',
50+
'',
51+
].join_lines()
52+
os.write_file(os.join_path(mod_src_tdir, 'foo.v'), tfile_content)!
53+
os.write_file(os.join_path(submod_tdir, 'baz.v'), submod_tfile_content)!
54+
os.execute_opt('${vexe} fmt -c ${mod_tdir}') or { assert false, err.msg() }
2655
}

0 commit comments

Comments
 (0)