From c8a2bac5f19de8bd53a13b3a2e4271a6ef3ec5a3 Mon Sep 17 00:00:00 2001 From: Hau Nguyen Date: Sun, 27 Nov 2022 00:51:18 +0700 Subject: [PATCH] fix: not copy import spec directly but use basic lit --- internal/imports/formatter.go | 57 +++++++++++++------------------ internal/imports/formatter_log.go | 34 ++++++++++++++++++ 2 files changed, 58 insertions(+), 33 deletions(-) create mode 100644 internal/imports/formatter_log.go diff --git a/internal/imports/formatter.go b/internal/imports/formatter.go index 8c3f608..72b8403 100644 --- a/internal/imports/formatter.go +++ b/internal/imports/formatter.go @@ -8,7 +8,6 @@ import ( "go/parser" "go/printer" "go/token" - "log" "os" "path/filepath" "strings" @@ -290,7 +289,12 @@ func (ft *Formatter) formatImportSpecs( if stdImportSpecs, ok := groupedImportSpecs[stdImport]; ok && len(stdImportSpecs) != 0 { for _, importSpec := range stdImportSpecs { - result = append(result, importSpec) + result = append(result, &ast.ImportSpec{ + Path: &ast.BasicLit{ + Value: importSpec.Path.Value, + Kind: token.IMPORT, + }, + }) } } @@ -305,7 +309,12 @@ func (ft *Formatter) formatImportSpecs( } for _, importSpec := range thirdPartImportSpecs { - result = append(result, importSpec) + result = append(result, &ast.ImportSpec{ + Path: &ast.BasicLit{ + Value: importSpec.Path.Value, + Kind: token.IMPORT, + }, + }) } } @@ -321,7 +330,12 @@ func (ft *Formatter) formatImportSpecs( } for _, importSpec := range companyImportSpecs { - result = append(result, importSpec) + result = append(result, &ast.ImportSpec{ + Path: &ast.BasicLit{ + Value: importSpec.Path.Value, + Kind: token.IMPORT, + }, + }) } } } @@ -337,7 +351,12 @@ func (ft *Formatter) formatImportSpecs( } for _, importSpec := range localImportSpecs { - result = append(result, importSpec) + result = append(result, &ast.ImportSpec{ + Path: &ast.BasicLit{ + Value: importSpec.Path.Value, + Kind: token.IMPORT, + }, + }) } } @@ -401,31 +420,3 @@ func (ft *Formatter) moduleName(path string) (string, error) { return result, nil } - -// Wrap log.Printf with verbose flag -func (ft *Formatter) log(format string, v ...any) { - if ft.isVerbose { - log.Printf(format, v...) - } -} - -func (ft *Formatter) logImportSpecs(logPrefix string, importSpecs []*ast.ImportSpec) { - if ft.isVerbose { - for _, importSpec := range importSpecs { - log.Printf("%s: importSpec: %+v %+v\n", logPrefix, importSpec.Name.String(), importSpec.Path.Value) - } - } -} - -func (ft *Formatter) mustLogImportSpecs(logPrefix string, importSpecs []ast.Spec) { - if ft.isVerbose { - for _, importSpec := range importSpecs { - importSpec, ok := importSpec.(*ast.ImportSpec) - if !ok { - continue - } - - log.Printf("%s: importSpec: %+v %+v\n", logPrefix, importSpec.Name.String(), importSpec.Path.Value) - } - } -} diff --git a/internal/imports/formatter_log.go b/internal/imports/formatter_log.go new file mode 100644 index 0000000..01aaa61 --- /dev/null +++ b/internal/imports/formatter_log.go @@ -0,0 +1,34 @@ +package imports + +import ( + "go/ast" + "log" +) + +// Wrap log.Printf with verbose flag +func (ft *Formatter) log(format string, v ...any) { + if ft.isVerbose { + log.Printf(format, v...) + } +} + +func (ft *Formatter) logImportSpecs(logPrefix string, importSpecs []*ast.ImportSpec) { + if ft.isVerbose { + for _, importSpec := range importSpecs { + log.Printf("%s: importSpec: %+v %+v\n", logPrefix, importSpec.Name.String(), importSpec.Path.Value) + } + } +} + +func (ft *Formatter) mustLogImportSpecs(logPrefix string, importSpecs []ast.Spec) { + if ft.isVerbose { + for _, importSpec := range importSpecs { + importSpec, ok := importSpec.(*ast.ImportSpec) + if !ok { + continue + } + + log.Printf("%s: importSpec: %+v %+v\n", logPrefix, importSpec.Name.String(), importSpec.Path.Value) + } + } +}