parser, fmt: fix formatting assign static method to anon fn (#19505)

This commit is contained in:
yuyi 2023-10-04 17:15:06 +08:00 committed by GitHub
parent dd7f3a73f3
commit 2bcbda55ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 10 deletions

View File

@ -1885,6 +1885,16 @@ pub fn (mut f Fmt) at_expr(node ast.AtExpr) {
f.write(node.name)
}
fn (mut f Fmt) write_static_method(name string, short_name string) {
f.mark_import_as_used(name.split('__static__')[0])
if short_name.contains('.') {
indx := short_name.index('.') or { -1 } + 1
f.write(short_name[0..indx] + short_name[indx..].replace('__static__', '.').capitalize())
} else {
f.write(short_name.replace('__static__', '.').capitalize())
}
}
pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
mut is_method_newline := false
if node.is_method {
@ -1922,13 +1932,7 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
} else {
name := f.short_module(node.name)
if node.name.contains('__static__') {
f.mark_import_as_used(node.name.split('__static__')[0])
if name.contains('.') {
indx := name.index('.') or { -1 } + 1
f.write(name[0..indx] + name[indx..].replace('__static__', '.').capitalize())
} else {
f.write(name.replace('__static__', '.').capitalize())
}
f.write_static_method(node.name, name)
} else {
f.mark_import_as_used(name)
f.write(name)
@ -2203,7 +2207,12 @@ pub fn (mut f Fmt) ident(node ast.Ident) {
}
}
name := f.short_module(node.name)
if node.name.contains('__static__') {
f.write_static_method(node.name, name)
} else {
f.mark_import_as_used(name)
f.write(name)
}
if node.concrete_types.len > 0 {
f.write('[')
for i, concrete_type in node.concrete_types {

View File

@ -2843,6 +2843,7 @@ fn (mut p Parser) name_expr() ast.Expr {
info: ast.IdentFn{
typ: fn_type
}
pos: pos
scope: p.scope
}
}

View File

@ -7,9 +7,7 @@ fn Foo.bar() string {
}
fn test_assign_static_method_to_anon_fn() {
// vfmt off
anon_fn := Foo.bar
// vfmt on
ret := anon_fn()
assert ret == 'bar'
}