This repository has been archived by the owner on Jan 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_import.go
394 lines (303 loc) · 8.66 KB
/
group_import.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package fixers
import (
"errors"
"go/ast"
"go/token"
"strconv"
"strings"
"golang.org/x/tools/go/ast/astutil"
)
func init() {
AddFixer("group_import", func(options FixerOptions) (CsFixer, error) {
lintText, err := options.extractString("lintText", "")
if err != nil {
return nil, err
}
groupStdLib, err := options.extractBool("stdLib", false)
if err != nil {
return nil, err
}
return &GroupImportFixer{lintText: lintText, groupStdLib: groupStdLib}, nil
})
}
type GroupImportFixer struct {
lintText string
groupStdLib bool
}
func (l *GroupImportFixer) Lint(content string) (Problems, error) {
problems := Problems{}
if !l.groupStdLib {
return problems, nil
}
fset, file, err := ContentToAst(content)
if err != nil {
return problems, err
}
lines := strings.Split(content, "\n")
importList := astutil.Imports(fset, file)
// astutil.Imports think that imports that have docs start new group, we consider it as one group, so, we need merge them
importList = l.mergeDocGroups(fset, importList)
firstGroupProblematic := false
if len(importList) > 0 {
firstGroupProblematic = l.mixedGroup(importList[0])
// Check that it is not "alone" 'import "stdlib"' that for us is invalid in any case
if !firstGroupProblematic && len(importList[0]) == 1 && l.stdLibImport(file.Imports[0].Path.Value) {
firstImportDecl := l.firstImportDec(file)
firstGroupProblematic = !firstImportDecl.Lparen.IsValid()
}
}
for k, importSpecs := range importList {
for _, importSpec := range importSpecs {
if l.stdLibImport(importSpec.Path.Value) && (firstGroupProblematic || k != 0) {
line := fset.Position(importSpec.Pos()).Line
problems = append(problems, &Problem{
Position: NewPosition(line),
Text: l.String(),
LineText: lines[line],
})
}
}
}
return problems, nil
}
func (l *GroupImportFixer) Fix(content string) (string, error) {
if !l.groupStdLib {
return content, nil
}
fset, file, err := ContentToAst(content)
if err != nil {
return "", err
}
var stdLibImports []*ast.ImportSpec
for _, importSpec := range file.Imports {
if l.stdLibImport(importSpec.Path.Value) {
stdLibImports = append(stdLibImports, importSpec)
}
}
// Delete found stdlib imports from document, we will add it later in right order
for _, importSpec := range stdLibImports {
path, err := strconv.Unquote(importSpec.Path.Value)
if err != nil {
return "", err
}
// If it is import block with doc and one import, attach this doc to import
importDec := l.findImportDec(file, importSpec)
if importDec.Doc != nil && len(importDec.Specs) == 1 {
if importSpec.Doc == nil {
importSpec.Doc = importDec.Doc
} else {
importSpec.Doc.List = append(importDec.Doc.List, importSpec.Doc.List...)
}
importDec.Doc = nil
}
if importSpec.Name != nil {
if !astutil.DeleteNamedImport(fset, file, importSpec.Name.Name, path) {
return "", errors.New("Can't delete named " + importSpec.Name.Name + " import " + importSpec.Path.Value)
}
} else {
if !astutil.DeleteImport(fset, file, path) {
return "", errors.New("Can't delete import " + importSpec.Path.Value)
}
}
// Delete comments and docs as well, we will restore it later with same imports
if importSpec.Comment != nil {
l.removeComment(file, importSpec.Comment)
}
if importSpec.Doc != nil {
l.removeComment(file, importSpec.Doc)
}
}
if len(stdLibImports) > 0 {
f := file
// Mostly taken from astutil/imports.go::AddNamedImport
impDecl := &ast.GenDecl{
Tok: token.IMPORT,
}
impDecl.TokPos = f.Package
ffile := fset.File(f.Package)
pkgLine := ffile.Line(f.Package)
for _, c := range f.Comments {
if ffile.Line(c.Pos()) > pkgLine {
break
}
impDecl.TokPos = c.End()
}
firstImportDecl := l.firstImportDec(file)
if firstImportDecl != nil && firstImportDecl.Doc != nil && firstImportDecl.Lparen.IsValid() {
impDecl.Doc = firstImportDecl.Doc
l.removeComment(file, firstImportDecl.Doc)
}
f.Decls = append(f.Decls, nil)
copy(f.Decls[1:], f.Decls[0:])
f.Decls[0] = impDecl
// Insert imports
for _, newImport := range stdLibImports {
// Mostly taken from astutil/imports.go::AddNamedImport
impDecl.Specs = append(impDecl.Specs, nil)
copy(impDecl.Specs[1:], impDecl.Specs[0:])
impDecl.Specs[0] = newImport
pos := impDecl.Pos()
if newImport.Name != nil {
newImport.Name.NamePos = pos
}
newImport.Path.ValuePos = pos
newImport.EndPos = pos
file.Imports = append(file.Imports, newImport)
}
impDecl.Lparen = impDecl.Specs[0].Pos()
// Restore doc
if impDecl.Doc != nil {
content = AstToContent(fset, file)
fset, file, err = ContentToAst(content)
if err != nil {
return "", err
}
firstImplDec := l.firstImportDec(file)
firstImplDec.Doc = impDecl.Doc
for _, cmt := range firstImplDec.Doc.List {
cmt.Slash = firstImplDec.Pos() - 1
}
}
}
res := AstToContent(fset, file)
res = strings.Replace(res, ")\n\nimport (", "", 1)
if len(stdLibImports) > 0 {
// Restore comment and doc blocks
fset, file, err = ContentToAst(res)
if err != nil {
return "", err
}
for _, impSpec := range stdLibImports {
if impSpec.Comment == nil && impSpec.Doc == nil {
continue
}
for _, newImportSpec := range file.Imports {
if !l.sameImportSpec(newImportSpec, impSpec) {
continue
}
if impSpec.Comment != nil {
newImportSpec.Comment = impSpec.Comment
for _, cmt := range newImportSpec.Comment.List {
cmt.Slash = newImportSpec.Path.End()
}
file.Comments = append(file.Comments, impSpec.Comment)
}
if impSpec.Doc != nil {
newImportSpec.Doc = impSpec.Doc
for _, cmt := range newImportSpec.Doc.List {
cmt.Slash = newImportSpec.Pos() - 1
}
file.Comments = append(file.Comments, impSpec.Doc)
}
break
}
}
// Sort comments, looks like it is important for printer
cmts := file.Comments
file.Comments = []*ast.CommentGroup{}
for len(cmts) > 0 {
var minCmt *ast.CommentGroup
var minCmtI int
for i, cmt := range cmts {
if minCmt == nil || minCmt.Pos() > cmt.Pos() {
minCmt = cmt
minCmtI = i
}
}
cmts = append(cmts[:minCmtI], cmts[minCmtI+1:]...)
file.Comments = append(file.Comments, minCmt)
}
res = AstToContent(fset, file)
}
return res, nil
}
func (l *GroupImportFixer) stdLibImport(importPath string) bool {
// Taken from imports.go::isThirdParty
return !strings.Contains(importPath, ".")
}
func (l *GroupImportFixer) mixedGroup(specs []*ast.ImportSpec) bool {
haveStdImport := false
haveNotStdImport := false
for _, importSpec := range specs {
if l.stdLibImport(importSpec.Path.Value) {
haveStdImport = true
} else {
haveNotStdImport = true
}
if haveStdImport && haveNotStdImport {
return true
}
}
return false
}
func (l *GroupImportFixer) mergeDocGroups(fset *token.FileSet, importGroups [][]*ast.ImportSpec) [][]*ast.ImportSpec {
var res [][]*ast.ImportSpec
for i, importGroup := range importGroups {
if i == 0 || importGroup[0].Doc == nil {
res = append(res, importGroup)
continue
}
prevImportGroup := importGroups[i-1]
if fset.Position(prevImportGroup[len(prevImportGroup)-1].End()).Line == fset.Position(importGroup[0].Doc.Pos()).Line-1 {
res[len(res)-1] = append(res[len(res)-1], importGroup...)
} else {
res = append(res, importGroup)
}
}
return res
}
func (l *GroupImportFixer) removeComment(file *ast.File, commentGroup *ast.CommentGroup) {
for i, cg := range file.Comments {
if cg == commentGroup {
copy(file.Comments[i:], file.Comments[i+1:])
file.Comments = file.Comments[:len(file.Comments)-1]
break
}
}
}
func (l *GroupImportFixer) firstImportDec(file *ast.File) *ast.GenDecl {
for i := 0; i < len(file.Decls); i++ {
decl := file.Decls[i]
gen, ok := decl.(*ast.GenDecl)
if ok && gen.Tok == token.IMPORT {
return gen
}
}
return nil
}
func (l *GroupImportFixer) findImportDec(file *ast.File, spec *ast.ImportSpec) *ast.GenDecl {
for i := 0; i < len(file.Decls); i++ {
decl := file.Decls[i]
gen, ok := decl.(*ast.GenDecl)
if !ok || gen.Tok != token.IMPORT {
continue
}
for _, importSpec := range gen.Specs {
if importSpec == spec {
return gen
}
}
}
return nil
}
func (l *GroupImportFixer) sameImportSpec(a *ast.ImportSpec, b *ast.ImportSpec) bool {
if a.Path.Value != b.Path.Value {
return false
}
if a.Name == nil && b.Name == nil {
return true
}
if (a.Name != nil && b.Name == nil) || (a.Name == nil && b.Name != nil) {
return false
}
if a.Name.Name != b.Name.Name {
return false
}
return true
}
func (l *GroupImportFixer) String() string {
if l.lintText != "" {
return l.lintText
}
return "Group import"
}