@@ -249,6 +249,7 @@ func formatClass(file *ast.File, pkg string, class string, entry string) {
249
249
}
250
250
var fnEntry * ast.FuncDecl
251
251
var decls []ast.Decl
252
+ var imports []ast.Decl
252
253
var varSpecs []ast.Spec
253
254
for _ , decl := range file .Decls {
254
255
switch v := decl .(type ) {
@@ -265,7 +266,11 @@ func formatClass(file *ast.File, pkg string, class string, entry string) {
265
266
}
266
267
}
267
268
case * ast.GenDecl :
268
- if v .Tok == token .TYPE {
269
+ switch v .Tok {
270
+ case token .IMPORT :
271
+ imports = append (imports , v )
272
+ continue
273
+ case token .TYPE :
269
274
if spec , ok := v .Specs [0 ].(* ast.TypeSpec ); ok && spec .Name .Name == class {
270
275
if st , ok := spec .Type .(* ast.StructType ); ok {
271
276
for _ , fs := range st .Fields .List {
@@ -281,13 +286,15 @@ func formatClass(file *ast.File, pkg string, class string, entry string) {
281
286
}
282
287
decls = append (decls , decl )
283
288
}
289
+
290
+ file .Decls = imports
284
291
if len (varSpecs ) != 0 {
285
- decls = append ([]ast. Decl { & ast.GenDecl {Tok : token .VAR , Specs : varSpecs }}, decls ... )
292
+ file . Decls = append (file . Decls , & ast.GenDecl {Tok : token .VAR , Specs : varSpecs })
286
293
}
294
+ file .Decls = append (file .Decls , decls ... )
287
295
if fnEntry != nil {
288
- decls = append (decls , fnEntry )
296
+ file . Decls = append (file . Decls , fnEntry )
289
297
}
290
- file .Decls = decls
291
298
292
299
for _ , decl := range file .Decls {
293
300
switch v := decl .(type ) {
@@ -321,7 +328,7 @@ func formatClass(file *ast.File, pkg string, class string, entry string) {
321
328
formatFuncDecl (ctx , fn )
322
329
}
323
330
for _ , imp := range ctx .imports {
324
- if imp . pkgPath == "fmt" && ! imp .isUsed {
331
+ if ! imp .isUsed {
325
332
if len (imp .decl .Specs ) == 1 {
326
333
file .Decls = deleteDecl (file .Decls , imp .decl )
327
334
} else {
0 commit comments