fmt: fix 'strings' name variable call generate auto import (fix #9713) (#14485)

master
yuyi 2022-05-21 20:01:58 +08:00 committed by GitHub
parent 0ceb16f285
commit 50ab2cfd1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 1 deletions

View File

@ -1651,7 +1651,8 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
// `time.now()` without `time imported` is processed as a method call with `time` being
// a `node.left` expression. Import `time` automatically.
// TODO fetch all available modules
if node.left.name in ['time', 'os', 'strings', 'math', 'json', 'base64'] {
if node.left.name in ['time', 'os', 'strings', 'math', 'json', 'base64']
&& !node.left.scope.known_var(node.left.name) {
f.file.imports << ast.Import{
mod: node.left.name
alias: node.left.name

View File

@ -0,0 +1,5 @@
fn main() {
strings := 'hello'
a := strings.repeat(2)
println(a)
}