-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathclean.go
37 lines (34 loc) · 905 Bytes
/
clean.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Package cleanimports provides functionality to clean unused imports from the
// given source code
package cleanimports
import (
"go/ast"
"go/format"
"go/parser"
"go/token"
"io"
"strings"
"golang.org/x/tools/go/ast/astutil"
)
// Clean writes the clean source to io.Writer. The source can be a io.Reader,
// string or []bytes
func Clean(w io.Writer, src interface{}) error {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "clean.go", src, parser.ParseComments)
if err != nil {
return err
}
// Clean unused imports
imports := astutil.Imports(fset, file)
for _, group := range imports {
for _, imp := range group {
path := strings.Trim(imp.Path.Value, `"`)
if !astutil.UsesImport(file, path) {
astutil.DeleteImport(fset, file, path)
}
}
}
ast.SortImports(fset, file)
// Write formatted code without unused imports
return format.Node(w, fset, file)
}