Skip to content

Commit

Permalink
fmt: fix import aliases in type declarations (#9483)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Neubert committed Mar 27, 2021
1 parent cbf4715 commit cd74273
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
5 changes: 1 addition & 4 deletions vlib/v/fmt/struct.v
Expand Up @@ -118,10 +118,7 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
mut default_expr_aligns := []CommentAndExprAlignInfo{}
mut field_types := []string{cap: node.fields.len}
for i, field in node.fields {
mut ft := f.no_cur_mod(f.table.type_to_str_using_aliases(field.typ, f.mod2alias))
if !ft.contains('C.') && !ft.contains('JS.') && !ft.contains('fn (') && !ft.contains('chan') {
ft = f.short_module(ft)
}
ft := f.no_cur_mod(f.table.type_to_str_using_aliases(field.typ, f.mod2alias))
field_types << ft
attrs_len := inline_attrs_len(field.attrs)
end_pos := field.pos.pos + field.pos.len
Expand Down
3 changes: 3 additions & 0 deletions vlib/v/fmt/tests/import_with_alias_keep.vv
@@ -1,5 +1,8 @@
import os
import time as t
import some.library as slib

type my_alias = fn (t slib.MyType)

struct Foo {
bar t.Time
Expand Down
12 changes: 10 additions & 2 deletions vlib/v/table/types.v
Expand Up @@ -922,8 +922,16 @@ fn (t Table) shorten_user_defined_typenames(originalname string, import_aliases
} else {
// types defined by the user
// mod.submod.submod2.Type => submod2.Type
parts := res.split('.')
res = if parts.len > 1 { parts[parts.len - 2..].join('.') } else { parts[0] }
mut parts := res.split('.')
if parts.len > 1 {
ind := parts.len - 2
if parts[ind] in import_aliases {
parts[ind] = import_aliases[parts[ind]]
}
res = parts[ind..].join('.')
} else {
res = parts[0]
}
}
return res
}
Expand Down

0 comments on commit cd74273

Please sign in to comment.