fmt: do not generate `import` statements for auto imports (#7966)

pull/7971/head
Uwe Krüger 2021-01-08 17:42:40 +01:00 committed by GitHub
parent 9291fb5e0c
commit cbe7740d97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 19 additions and 23 deletions

View File

@ -2228,8 +2228,6 @@ a property of the individual channel object. Channels can be passed to coroutine
variables:
```v
import sync
fn f(ch chan int) {
// ...
}
@ -2331,8 +2329,6 @@ if select {
For special purposes there are some builtin properties and methods:
```v nofmt
import sync
struct Abc{x int}
a := 2.13

View File

@ -1,5 +1,3 @@
import sync
fn test_pointer() {
mut arr := []&int{}
a := 1

View File

@ -461,6 +461,7 @@ pub mut:
scope &Scope
stmts []Stmt // all the statements in the source file
imports []Import // all the imports
auto_imports []string // imports that were implicitely added
imported_symbols map[string]string // used for `import {symbol}`, it maps symbol => module.symbol
errors []errors.Error // all the checker errors in the file
warnings []errors.Warning // all the checker warings in the file

View File

@ -38,7 +38,7 @@ pub mut:
file ast.File
did_imports bool
is_assign bool
auto_imports []string // automatically inserted imports that the user forgot to specify
auto_imports []string // automatically inserted imports that the user does not need to specify
import_pos int // position of the imports in the resulting string for later autoimports insertion
used_imports []string // to remove unused imports
is_debug bool
@ -83,6 +83,7 @@ pub fn (mut f Fmt) process_file_imports(file &ast.File) {
f.mod2alias[sym.name] = sym.name
}
}
f.auto_imports = file.auto_imports
}
pub fn (mut f Fmt) write(s string) {
@ -201,8 +202,8 @@ pub fn (mut f Fmt) imports(imports []ast.Import) {
if f.did_imports || imports.len == 0 {
return
}
// f.import_pos = f.out.len
f.did_imports = true
mut num_imports := 0
/*
if imports.len == 1 {
imp_stmt_str := f.imp_stmt_str(imports[0])
@ -215,12 +216,16 @@ pub fn (mut f Fmt) imports(imports []ast.Import) {
// TODO bring back once only unused imports are removed
// continue
}
// f.out_imports.write('\t')
// f.out_imports.writeln(f.imp_stmt_str(imp))
if imp.mod in f.auto_imports && imp.mod !in f.used_imports {
continue
}
f.out_imports.write('import ')
f.out_imports.writeln(f.imp_stmt_str(imp))
num_imports++
}
if num_imports > 0 {
f.out_imports.writeln('')
}
f.out_imports.writeln('')
// f.out_imports.writeln(')\n')
// }
}
@ -1678,12 +1683,9 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
// 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 f.auto_imports {
f.auto_imports << node.left.name
f.file.imports << ast.Import{
mod: node.left.name
alias: node.left.name
}
f.file.imports << ast.Import{
mod: node.left.name
alias: node.left.name
}
// for imp in f.file.imports {
// println(imp.mod)

View File

@ -1,5 +1,3 @@
import sync
struct FSMEvent {
x int
}

View File

@ -1,4 +1,3 @@
import sync
import time
struct St {

View File

@ -42,6 +42,9 @@ fn (mut p Parser) register_auto_import(alias string) {
}
p.ast_imports << node
}
if alias !in p.auto_imports {
p.auto_imports << alias
}
p.register_used_import(alias)
}

View File

@ -53,6 +53,7 @@ mut:
imports map[string]string // alias => mod_name
ast_imports []ast.Import // mod_names
used_imports []string // alias
auto_imports []string // imports, the user does not need to specify
imported_symbols map[string]string
is_amp bool // for generating the right code for `&Foo{}`
returns bool
@ -232,6 +233,7 @@ pub fn (mut p Parser) parse() ast.File {
mod: module_decl
imports: p.ast_imports
imported_symbols: p.imported_symbols
auto_imports: p.auto_imports
stmts: stmts
scope: p.scope
global_scope: p.global_scope

View File

@ -2,7 +2,6 @@ module websocket
import net
import time
import sync
// socket_read reads from socket into the provided buffer
fn (mut ws Client) socket_read(mut buffer []byte) ?int {

View File

@ -7,7 +7,6 @@ import x.openssl
import net.urllib
import time
import log
import sync
import rand
const (

View File

@ -3,7 +3,6 @@ module websocket
import net
import x.openssl
import log
import sync
import time
import rand