Skip to content

Commit

Permalink
ast, fmt: improve submodule type alias lookup; fix formatting of modu…
Browse files Browse the repository at this point in the history
…les in `$VMODULES` (#20989)
  • Loading branch information
ttytm authored Mar 10, 2024
1 parent e3140cb commit 386bd77
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
7 changes: 3 additions & 4 deletions vlib/v/ast/types.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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('.')
}
Expand Down
39 changes: 34 additions & 5 deletions vlib/v/fmt/fmt_vmodules_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,54 @@ 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)
}

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 }',
'',
'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() }
}

0 comments on commit 386bd77

Please sign in to comment.