-
Notifications
You must be signed in to change notification settings - Fork 2
/
unimport.go
221 lines (186 loc) · 4.91 KB
/
unimport.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"flag"
"fmt"
"go/ast"
"go/build"
"go/parser"
"go/token"
"log"
"os"
"path"
"path/filepath"
"strings"
)
const (
pwd = "./"
)
func init() {
build.Default.UseAllFiles = true
}
func usage() {
log.Printf("Usage of %s:\n", os.Args[0])
log.Printf("\nunimport # runs on package in current directory\n")
log.Printf("\nunimport [packages]\n")
//TODO add back when flags are supporrted
// log.Printf("Flags:\n")
// flag.PrintDefaults()
}
type returnsVisitor struct {
f *token.FileSet
}
func main() {
// Remove log timestamp
log.SetFlags(0)
flag.Usage = usage
flag.Parse()
if err := checkImports(flag.Args()); err != nil {
log.Println(err)
}
}
func checkImports(args []string) error {
fset := token.NewFileSet()
files, err := parseInput(args, fset)
if err != nil {
return fmt.Errorf("could not parse input %v", err)
}
retVis := &returnsVisitor{
f: fset,
}
for _, f := range files {
ast.Walk(retVis, f)
}
return nil
}
func parseInput(args []string, fset *token.FileSet) ([]*ast.File, error) {
var directoryList []string
var fileMode bool
files := make([]*ast.File, 0)
if len(args) == 0 {
directoryList = append(directoryList, pwd)
} else {
for _, arg := range args {
if strings.HasSuffix(arg, "/...") && isDir(arg[:len(arg)-len("/...")]) {
for _, dirname := range allPackagesInFS(arg) {
directoryList = append(directoryList, dirname)
}
} else if isDir(arg) {
directoryList = append(directoryList, arg)
} else if exists(arg) {
if strings.HasSuffix(arg, ".go") {
fileMode = true
f, err := parser.ParseFile(fset, arg, nil, 0)
if err != nil {
return nil, err
}
files = append(files, f)
} else {
return nil, fmt.Errorf("invalid file %v specified", arg)
}
} else {
//TODO clean this up a bit
imPaths := importPaths([]string{arg})
for _, importPath := range imPaths {
pkg, err := build.Import(importPath, ".", 0)
if err != nil {
return nil, err
}
var stringFiles []string
stringFiles = append(stringFiles, pkg.GoFiles...)
// files = append(files, pkg.CgoFiles...)
stringFiles = append(stringFiles, pkg.TestGoFiles...)
if pkg.Dir != "." {
for i, f := range stringFiles {
stringFiles[i] = filepath.Join(pkg.Dir, f)
}
}
fileMode = true
for _, stringFile := range stringFiles {
f, err := parser.ParseFile(fset, stringFile, nil, 0)
if err != nil {
return nil, err
}
files = append(files, f)
}
}
}
}
}
// if we're not in file mode, then we need to grab each and every package in each directory
// we can to grab all the files
if !fileMode {
for _, fpath := range directoryList {
pkgs, err := parser.ParseDir(fset, fpath, nil, 0)
if err != nil {
return nil, err
}
for _, pkg := range pkgs {
for _, f := range pkg.Files {
files = append(files, f)
}
}
}
}
return files, nil
}
func isDir(filename string) bool {
fi, err := os.Stat(filename)
return err == nil && fi.IsDir()
}
func exists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
func (v *returnsVisitor) Visit(node ast.Node) ast.Visitor {
file, ok := node.(*ast.File)
if !ok {
return v
}
type importAlias struct {
importSpec *ast.ImportSpec
index int
}
var importAliases []importAlias
for i, pkgImport := range file.Imports {
if pkgImport.Name != nil && pkgImport.Name.Name != "_" {
alias := importAlias{
importSpec: pkgImport,
index: i,
}
importAliases = append(importAliases, alias)
}
}
switch len(importAliases) {
case 0:
default:
// verify that each alias is needed by making a second pass through the imports
for _, importAlias := range importAliases {
var aliasNeeded bool
for i, pkgImport := range file.Imports {
// Since we know the index of the import alias in file.Imports from our first pass, we can save a string comparison
if i == importAlias.index {
continue
}
if pkgImport.Path != nil && strings.Replace(path.Base(pkgImport.Path.Value), `"`, "", -1) == strings.Replace(path.Base(importAlias.importSpec.Path.Value), `"`, "", -1) {
// this alias is needed, continue
aliasNeeded = true
break
}
}
if !aliasNeeded {
file := v.f.File(importAlias.importSpec.Pos())
lineNumber := file.Position(importAlias.importSpec.Pos()).Line
// dot imports inside of tests are okay
if importAliases[0].importSpec.Name.Name == "." && strings.HasSuffix(file.Name(), "_test.go") {
continue
}
// If the alias path contains a dash or dot, it's likely importing a specific revision - ignore these
if strings.Contains(importAliases[0].importSpec.Path.Value, "-") || strings.Contains(importAliases[0].importSpec.Path.Value, ".") {
continue
}
log.Printf("%v:%v unnecessary import alias %v\n", file.Name(), lineNumber, importAlias.importSpec.Name.Name)
}
}
}
return v
}