fmt: fix a bug with an empty module in consts

pull/8401/head
Alexander Medvednikov 2021-01-29 00:26:57 +01:00
parent 1be7cc14d3
commit 608a4ccccd
1 changed files with 5 additions and 3 deletions

View File

@ -32,6 +32,7 @@ pub mut:
array_init_depth int // current level of hierarchie in array init array_init_depth int // current level of hierarchie in array init
single_line_if bool single_line_if bool
cur_mod string cur_mod string
cur_short_mod string // TODO clean this up
file ast.File file ast.File
did_imports bool did_imports bool
is_assign bool is_assign bool
@ -205,6 +206,7 @@ pub fn (mut f Fmt) short_module(name string) string {
pub fn (mut f Fmt) mod(mod ast.Module) { pub fn (mut f Fmt) mod(mod ast.Module) {
f.set_current_module_name(mod.name) f.set_current_module_name(mod.name)
f.cur_short_mod = mod.short_name
if mod.is_skipped { if mod.is_skipped {
return return
} }
@ -1146,12 +1148,12 @@ pub fn (mut f Fmt) ident(node ast.Ident) {
} else { } else {
// Force usage of full path to const in the same module: // Force usage of full path to const in the same module:
// `println(minute)` => `println(time.minute)` // `println(minute)` => `println(time.minute)`
// This allows using the variable `minute` inside time's functions // This makes it clear that a module const is being used
// and also makes it clear that a module const is being used
// (since V's conts are no longer ALL_CAP). // (since V's conts are no longer ALL_CAP).
// ^^^ except for `main`, where consts are allowed to not have a `main.` prefix. // ^^^ except for `main`, where consts are allowed to not have a `main.` prefix.
if !node.name.contains('.') && !f.inside_const { if !node.name.contains('.') && !f.inside_const {
full_name := f.cur_mod + '.' + node.name mod := if f.cur_mod == '' { f.cur_short_mod } else { f.cur_mod } // TODO why is this needed?
full_name := mod + '.' + node.name
if obj := f.file.global_scope.find(full_name) { if obj := f.file.global_scope.find(full_name) {
if obj is ast.ConstField { if obj is ast.ConstField {
// "v.fmt.foo" => "fmt.foo" // "v.fmt.foo" => "fmt.foo"