Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ast, fmt: improve submodule type alias lookup; fix formatting of modules in $VMODULES #20989

Merged
merged 2 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() }
}