fmt: fix import aliases in type declarations (#9483)

pull/9486/head
Lukas Neubert 2021-03-27 11:33:41 +01:00 committed by GitHub
parent cbf4715804
commit cd74273869
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 6 deletions

View File

@ -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

View File

@ -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

View File

@ -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
}