feat: sort imports using default Go (wip)

main
sudo pacman -Syu 2022-11-27 01:14:51 +07:00
parent 6463559962
commit 57e4bbd4d7
No known key found for this signature in database
GPG Key ID: D6CB5C6C567C47B0
1 changed files with 14 additions and 0 deletions

View File

@ -239,6 +239,20 @@ func (ft *Formatter) formatImports(
return nil, err
}
// Post process to sort imports using default Go
// TODO: find a way to use ast.SortImports without parse ast again
formattedASTFile, err := parser.ParseFile(fset, path, formattedBuffer.Bytes(), parserMode)
if err != nil {
return nil, fmt.Errorf("parser: failed to parse file [%s]: %w", path, err)
}
ast.SortImports(fset, formattedASTFile)
formattedBuffer.Reset()
if err := printer.Fprint(formattedBuffer, fset, astFile); err != nil {
return nil, err
}
return formattedBuffer.Bytes(), nil
}