fmt: remove duplicated imports (#8433)

pull/8422/head^2
zakuro 2021-01-30 18:04:36 +09:00 committed by GitHub
parent de8df1bbe3
commit 391d62bca5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 2 deletions

View File

@ -257,6 +257,7 @@ pub fn (mut f Fmt) imports(imports []ast.Import) {
f.out_imports.writeln('import ${imp_stmt_str}\n') f.out_imports.writeln('import ${imp_stmt_str}\n')
} else if imports.len > 1 { } else if imports.len > 1 {
*/ */
mut already_imported := map[string]bool{}
for imp in imports { for imp in imports {
if imp.mod !in f.used_imports { if imp.mod !in f.used_imports {
// TODO bring back once only unused imports are removed // TODO bring back once only unused imports are removed
@ -265,8 +266,12 @@ pub fn (mut f Fmt) imports(imports []ast.Import) {
if imp.mod in f.auto_imports && imp.mod !in f.used_imports { if imp.mod in f.auto_imports && imp.mod !in f.used_imports {
continue continue
} }
f.out_imports.write('import ') import_text := 'import ${f.imp_stmt_str(imp)}'
f.out_imports.writeln(f.imp_stmt_str(imp)) if already_imported[import_text] {
continue
}
already_imported[import_text] = true
f.out_imports.writeln(import_text)
num_imports++ num_imports++
} }
if num_imports > 0 { if num_imports > 0 {

View File

@ -1,5 +1,6 @@
import math import math
import os import os
import math
const ( const (
mypi = math.pi mypi = math.pi