fix: not copy import spec directly but use basic lit

main
sudo pacman -Syu 2022-11-27 00:51:18 +07:00
parent 5e87f5eca8
commit c8a2bac5f1
No known key found for this signature in database
GPG Key ID: D6CB5C6C567C47B0
2 changed files with 58 additions and 33 deletions

View File

@ -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)
}
}
}

View File

@ -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)
}
}
}