/
formatter.go
487 lines (402 loc) · 11.4 KB
/
formatter.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package imports
import (
"bytes"
"errors"
"fmt"
"go/parser"
"go/token"
"io/fs"
"os"
"path/filepath"
"strings"
"sync"
"github.com/dave/dst"
"github.com/dave/dst/decorator"
"github.com/pkg/diff"
"golang.org/x/mod/modfile"
"golang.org/x/sync/errgroup"
"golang.org/x/tools/go/packages"
)
const (
// Use for group imports
stdImport = "std"
thirdPartyImport = "third-party"
companyImport = "company"
localImport = "local"
)
var (
ErrEmptyPaths = errors.New("empty paths")
ErrNotGoFile = errors.New("not go file")
ErrGoGeneratedFile = errors.New("go generated file")
ErrAlreadyFormatted = errors.New("already formatted")
ErrEmptyImport = errors.New("empty import")
ErrGoModNotExist = errors.New("go mod not exist")
ErrGoModEmptyModule = errors.New("go mod empty module")
ErrNotBytesBuffer = errors.New("not bytes.Buffer")
ErrNotDSTGenDecl = errors.New("not dst.GenDecl")
)
// https://pkg.go.dev/sync#Pool
var bufPool = sync.Pool{
New: func() any {
return new(bytes.Buffer)
},
}
// stdPackages -> save std packages for later search.
// moduleNames -> map path to its go.mod module name.
// formattedPaths -> make sure we not format path more than 1 time.
type Formatter struct {
stdPackages map[string]struct{}
moduleNames map[string]string
formattedPaths map[string]struct{}
companyPrefixes map[string]struct{}
eg errgroup.Group
muModuleNames sync.RWMutex
muFormattedPaths sync.RWMutex
isList bool
isWrite bool
isDiff bool
isVerbose bool
isStock bool
}
func NewFormmater(opts ...FormatterOptionFn) (*Formatter, error) {
ft := &Formatter{}
for _, opt := range opts {
opt(ft)
}
stdPackages, err := packages.Load(nil, "std")
if err != nil {
return nil, fmt.Errorf("packages: failed to load std: %w", err)
}
ft.stdPackages = make(map[string]struct{})
for _, stdPackage := range stdPackages {
ft.stdPackages[stdPackage.PkgPath] = struct{}{}
}
ft.moduleNames = make(map[string]string)
ft.formattedPaths = make(map[string]struct{})
return ft, nil
}
// Accept a list of files or directories
func (ft *Formatter) Format(paths ...string) error {
if len(paths) == 0 {
return ErrEmptyPaths
}
// Logic switch case copy from goimports, gofumpt
for _, path := range paths {
path := strings.TrimSpace(path)
if path == "" {
continue
}
switch dir, err := os.Stat(path); {
case err != nil:
return fmt.Errorf("os: failed to stat: [%s] %w", path, err)
case dir.IsDir():
if err := ft.formatDir(path); err != nil {
return err
}
default:
ft.eg.Go(func() error {
if err := ft.formatFile(path); err != nil {
if ft.isIgnoreError(err) {
return nil
}
return err
}
return nil
})
}
}
if err := ft.eg.Wait(); err != nil {
return err
}
return nil
}
// Copy from gofumpt
func (ft *Formatter) formatDir(path string) error {
if err := filepath.WalkDir(path, func(path string, dirEntry fs.DirEntry, err error) error {
if filepath.Base(path) == "vendor" {
return filepath.SkipDir
}
if err != nil {
return err
}
if dirEntry.IsDir() {
// Get module name ASAP to cache it
moduleName, err := ft.moduleName(path)
if err != nil {
return err
}
ft.log("formatFile: moduleName: [%s]\n", moduleName)
return nil
}
ft.eg.Go(func() error {
if err := ft.formatFile(path); err != nil {
if ft.isIgnoreError(err) {
return nil
}
return err
}
return nil
})
return nil
}); err != nil {
return fmt.Errorf("filepath: failed to walk dir: [%s] %w", path, err)
}
return nil
}
func (ft *Formatter) formatFile(path string) error {
ft.muFormattedPaths.RLock()
if _, ok := ft.formattedPaths[path]; ok {
ft.muFormattedPaths.RUnlock()
return nil
}
ft.muFormattedPaths.RUnlock()
// Return if not go file
if !isGoFile(filepath.Base(path)) {
return ErrNotGoFile
}
// Read file first
pathBytes, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("os: failed to read file: [%s] %w", path, err)
}
// Get module name of path
moduleName, err := ft.moduleName(path)
if err != nil {
return err
}
ft.log("formatFile: moduleName: [%s]\n", moduleName)
formattedBytes, err := ft.formatImports(path, pathBytes, moduleName)
if err != nil {
return err
}
if bytes.Equal(pathBytes, formattedBytes) {
return ErrAlreadyFormatted
}
if ft.isList {
fmt.Println("Formatted: ", path)
}
if ft.isWrite {
if err := os.WriteFile(path, formattedBytes, 0o600); err != nil {
return fmt.Errorf("os: failed to write file: [%s] %w", path, err)
}
}
if ft.isDiff {
if err := diff.Text(path+" before", path+" after", pathBytes, formattedBytes, os.Stdout); err != nil {
return fmt.Errorf("diff: failed to slices: %w", err)
}
}
ft.muFormattedPaths.Lock()
ft.formattedPaths[path] = struct{}{}
ft.muFormattedPaths.Unlock()
return nil
}
// Copy from goimports, gofumpt, goimports-reviser.
// First parse ast.
// Then group imports.
// Then format imports.
// Then print to bytes.
func (ft *Formatter) formatImports(
path string,
pathBytes []byte,
moduleName string,
) ([]byte, error) {
// Parse ast
fset := token.NewFileSet()
// Copy from gofumpt
parserMode := parser.Mode(0)
parserMode |= parser.ParseComments
parserMode |= parser.SkipObjectResolution
astFile, err := parser.ParseFile(fset, path, pathBytes, parserMode)
if err != nil {
return nil, fmt.Errorf("parser: failed to parse file [%s]: %w", path, err)
}
// Ignore generated file
if isGoGenerated(astFile) {
return nil, ErrGoGeneratedFile
}
dec := decorator.NewDecorator(fset)
dstFile, err := dec.DecorateFile(astFile)
if err != nil {
return nil, fmt.Errorf("decorator: failed to parse file [%s]: %w", path, err)
}
if len(dstFile.Imports) == 0 || len(dstFile.Decls) == 0 {
return nil, ErrEmptyImport
}
ft.logDSTImportSpecs("formatImports: dstImportSpecs", dstFile.Imports)
groupedDSTImportSpecs, err := ft.groupDSTImportSpecs(dstFile.Imports, moduleName)
if err != nil {
return nil, err
}
formattedDSTImportSpecs, err := ft.formatDSTImportSpecs(groupedDSTImportSpecs)
if err != nil {
return nil, err
}
ft.logDSTImportSpecs("formatImports: formattedDSTImportSpecs: ", formattedDSTImportSpecs)
// First update
dstFile.Imports = formattedDSTImportSpecs
genDecl, ok := dstFile.Decls[0].(*dst.GenDecl)
if !ok {
return nil, ErrNotDSTGenDecl
}
formattedGenSpecs := make([]dst.Spec, 0, len(genDecl.Specs))
// Append all imports first
for _, importSpec := range formattedDSTImportSpecs {
formattedGenSpecs = append(formattedGenSpecs, importSpec)
}
// Append all non imports later
for _, genSpec := range genDecl.Specs {
if _, ok := genSpec.(*dst.ImportSpec); !ok {
formattedGenSpecs = append(formattedGenSpecs, genSpec)
continue
}
}
// Second update
genDecl.Specs = formattedGenSpecs
b, ok := bufPool.Get().(*bytes.Buffer)
if !ok {
return nil, ErrNotBytesBuffer
}
b.Reset()
defer bufPool.Put(b)
if err := decorator.Fprint(b, dstFile); err != nil {
return nil, fmt.Errorf("decorator: failed to fprint [%s]: %w", path, err)
}
result := make([]byte, b.Len())
copy(result, b.Bytes())
return result, nil
}
func (ft *Formatter) groupDSTImportSpecs(importSpecs []*dst.ImportSpec, moduleName string) (map[string][]*dst.ImportSpec, error) {
result := make(map[string][]*dst.ImportSpec)
result[stdImport] = make([]*dst.ImportSpec, 0, 8)
result[thirdPartyImport] = make([]*dst.ImportSpec, 0, 8)
if !ft.isStock {
// Only split company, local imports if not stock
// Otherwise everything is third party imports
if len(ft.companyPrefixes) != 0 {
result[companyImport] = make([]*dst.ImportSpec, 0, 8)
}
result[localImport] = make([]*dst.ImportSpec, 0, 8)
}
for _, importSpec := range importSpecs {
// "github.com/abc/xyz" -> github.com/abc/xyz
importPath := strings.Trim(importSpec.Path.Value, `"`)
if _, ok := ft.stdPackages[importPath]; ok {
result[stdImport] = append(result[stdImport], importSpec)
continue
}
if !ft.isStock {
if strings.HasPrefix(importPath, moduleName) {
result[localImport] = append(result[localImport], importSpec)
continue
}
if len(ft.companyPrefixes) != 0 {
existImport := false
for companyPrefix := range ft.companyPrefixes {
if strings.HasPrefix(importPath, companyPrefix) {
result[companyImport] = append(result[companyImport], importSpec)
existImport = true
break
}
}
if existImport {
continue
}
}
}
result[thirdPartyImport] = append(result[thirdPartyImport], importSpec)
}
ft.logDSTImportSpecs("groupDSTImportSpecs: stdImport", result[stdImport])
ft.logDSTImportSpecs("groupDSTImportSpecs: thirdPartyImport", result[thirdPartyImport])
if len(ft.companyPrefixes) != 0 {
ft.logDSTImportSpecs("groupDSTImportSpecs: companyImport", result[companyImport])
}
ft.logDSTImportSpecs("groupDSTImportSpecs: localImport", result[localImport])
return result, nil
}
func (ft *Formatter) formatDSTImportSpecs(groupedImportSpecs map[string][]*dst.ImportSpec,
) ([]*dst.ImportSpec, error) {
result := make([]*dst.ImportSpec, 0, 32)
appendToResultFn := func(groupImportType string) {
importSpecs, ok := groupedImportSpecs[groupImportType]
if !ok || len(importSpecs) == 0 {
return
}
for _, importSpec := range importSpecs {
importSpec.Decs.Before = dst.NewLine
importSpec.Decs.After = dst.NewLine
}
importSpecs[len(importSpecs)-1].Decs.After = dst.EmptyLine
result = append(result, importSpecs...)
}
appendToResultFn(stdImport)
appendToResultFn(thirdPartyImport)
appendToResultFn(companyImport)
appendToResultFn(localImport)
if len(result) == 0 {
return result, nil
}
result[len(result)-1].Decs.After = dst.NewLine
return result, nil
}
// Copy from goimports-reviser
// Get module name from go.mod of path
// If current path doesn't have go.mod, recursive find its parent path
func (ft *Formatter) moduleName(path string) (string, error) {
ft.muModuleNames.RLock()
if pkgName, ok := ft.moduleNames[path]; ok {
ft.muModuleNames.RUnlock()
return pkgName, nil
}
ft.muModuleNames.RUnlock()
// Copy from goimports-reviser
// Check path/go.mod first
// If not exist -> check ../go.mod
// Assume path is dir path, maybe wrong but it is ok for now
dirPath := filepath.Clean(path)
var goModPath string
for {
ft.muModuleNames.RLock()
if pkgName, ok := ft.moduleNames[dirPath]; ok {
ft.muModuleNames.RUnlock()
return pkgName, nil
}
ft.muModuleNames.RUnlock()
goModPath = filepath.Join(dirPath, "go.mod")
fileInfo, err := os.Stat(goModPath)
if err == nil && !fileInfo.IsDir() {
break
}
// Check ..
if dirPath == filepath.Dir(dirPath) {
// Reach root
break
}
dirPath = filepath.Dir(dirPath)
}
if goModPath == "" {
return "", ErrGoModNotExist
}
ft.log("moduleName: goModPath: %+v\n", goModPath)
goModPathBytes, err := os.ReadFile(goModPath)
if err != nil {
return "", fmt.Errorf("os: failed to read file: [%s] %w", goModPath, err)
}
goModFile, err := modfile.Parse(goModPath, goModPathBytes, nil)
if err != nil {
return "", fmt.Errorf("modfile: failed to parse: [%s] %w", goModPath, err)
}
result := goModFile.Module.Mod.Path
if result == "" {
return "", ErrGoModEmptyModule
}
ft.muModuleNames.Lock()
ft.moduleNames[path] = result
ft.muModuleNames.Unlock()
return result, nil
}
func (ft *Formatter) isIgnoreError(err error) bool {
return errors.Is(err, ErrNotGoFile) ||
errors.Is(err, ErrGoGeneratedFile) ||
errors.Is(err, ErrAlreadyFormatted) ||
errors.Is(err, ErrEmptyImport)
}