-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmain.go
More file actions
376 lines (339 loc) · 10.1 KB
/
main.go
File metadata and controls
376 lines (339 loc) · 10.1 KB
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
// Copyright 2018 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package main
import (
"bytes"
"flag"
"fmt"
goparser "go/parser"
"go/printer"
"go/token"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/cockroachdb/crlfmt/internal/parser"
"github.com/cockroachdb/crlfmt/internal/render"
"github.com/cockroachdb/gostdlib/go/format"
"github.com/cockroachdb/gostdlib/x/tools/imports"
)
var (
// TODO: wrap doc strings for imports and floating comments.
wrapdoc = flag.Int("wrapdoc", 160, "column at which to wrap doc strings for functions, variables, constants, and types. ignores multiline comments denoted by /*")
wrap = flag.Int("wrap", 100, "column to wrap at")
tab = flag.Int("tab", 2, "tab width for column calculations")
overwrite = flag.Bool("w", false, "overwrite modified files")
fast = flag.Bool("fast", false, "skip running goimports and simplify")
groupImports = flag.Bool("groupimports", true, "group imports by type")
printDiff = flag.Bool("diff", true, "print diffs")
ignore = flag.String("ignore", "", "regex matching files to skip")
localPrefix = flag.String("local", "", "put imports beginning with this string after 3rd-party packages; comma-separated list")
srcDir = flag.String("srcdir", "", "resolve imports as if the source file is from the given directory (if a file is given, the parent directory is used)")
)
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
}
func run() error {
flag.Parse()
if flag.NArg() == 0 {
content, err := io.ReadAll(os.Stdin)
if err != nil {
return err
}
*overwrite = true
*printDiff = false
out, err := checkBuf("<standard input>", content)
if err != nil {
return err
}
_, err = os.Stdout.Write(out)
return err
}
var ignoreRE *regexp.Regexp
var err error
if len(*ignore) > 0 {
ignoreRE, err = regexp.Compile(*ignore)
if err != nil {
return fmt.Errorf("compiling ignore regexp: %s", err)
}
}
visited := make(map[string]struct{})
for _, root := range flag.Args() {
resolved, err := filepath.EvalSymlinks(root)
if err != nil {
return fmt.Errorf("following symlinks in input path: %s", err)
}
err = filepath.Walk(resolved, func(path string, fi os.FileInfo, err error) error {
if _, exists := visited[path]; exists {
return nil
}
visited[path] = struct{}{}
if os.IsNotExist(err) {
return nil
} else if err != nil {
return err
}
if ignoreRE != nil && ignoreRE.MatchString(path) {
return nil
}
if fi.IsDir() {
return nil
}
if !strings.HasSuffix(path, ".go") {
return nil
}
return checkPath(path)
})
if err != nil {
return fmt.Errorf("error during walk: %s", err)
}
}
return nil
}
func checkPath(path string) error {
src, err := os.ReadFile(path)
if err != nil {
return err
}
output, err := checkBuf(path, src)
if err != nil {
return err
}
if !bytes.Equal(src, output) {
if *printDiff {
data, err := diff(src, output, path)
if err != nil {
return fmt.Errorf("computing diff: %s", err)
}
fmt.Printf("diff -u old/%[1]s new/%[1]s\n", filepath.ToSlash(path))
os.Stdout.Write(data)
}
if *overwrite {
err := os.WriteFile(path, output, 0)
if err != nil {
return err
}
}
}
return nil
}
func checkBuf(path string, src []byte) ([]byte, error) {
output := new(bytes.Buffer)
if !*fast {
// Run goimports, which also runs gofmt.
importOpts := imports.Options{
AllErrors: true,
Comments: true,
TabIndent: false,
TabWidth: *tab,
FormatOnly: false,
}
if localPrefix != nil && *localPrefix != "" {
imports.LocalPrefix = *localPrefix
}
pathForImports := path
if *srcDir != "" {
filename := filepath.Base(path)
if isDirectory(*srcDir) {
pathForImports = filepath.Join(*srcDir, filename)
} else {
pathForImports = filepath.Join(filepath.Dir(*srcDir), filename)
}
}
newSrc, err := imports.Process(pathForImports, src, &importOpts)
if err != nil {
return nil, err
}
src = newSrc
// Simplify
{
fileSet := token.NewFileSet()
f, err := goparser.ParseFile(fileSet, path, src, goparser.ParseComments)
if err != nil {
return nil, err
}
render.Simplify(f)
prCfg := &printer.Config{
Tabwidth: *tab,
Mode: printer.UseSpaces | printer.TabIndent,
}
var buf bytes.Buffer
prCfg.Fprint(&buf, fileSet, f)
src = buf.Bytes()
}
}
file, err := parser.ParseFile(path, src)
if err != nil {
return nil, err
}
var importMapping map[*parser.ImportDecl][]render.ImportBlock
if *groupImports {
importMapping = remapImports(file)
}
lastPos := token.NoPos
for _, d := range file.Decls {
if imp, ok := d.(*parser.ImportDecl); ok && *groupImports {
blocks := importMapping[imp]
if blocks == nil {
// This import declaration is meant to be removed. If it's
// surrounded by blank lines, remove those too.
//
// If the import block is surrounded by blank lines, remove the
// blank lines too.
startPos, endPos := imp.Pos, imp.End
if off := file.Offset(startPos); off-2 >= 0 && src[off-1] == '\n' && src[off-2] == '\n' {
startPos = file.Pos(off - 1)
}
if off := file.Offset(endPos); off+1 < len(src) && src[off] == '\n' && src[off+1] == '\n' {
endPos = file.Pos(off + 1)
}
output.Write(file.Slice(lastPos, startPos))
lastPos = endPos
continue
}
var importBuf bytes.Buffer
if imp.Doc != nil && blocks[0].Size() > 1 {
importBuf.Write(file.Slice(imp.Doc.Pos(), imp.Doc.End()))
importBuf.WriteByte('\n')
}
for i, block := range blocks {
if i > 0 {
importBuf.WriteString("\n\n")
}
render.Imports(&importBuf, file, block)
}
newBytes, err := format.Source(importBuf.Bytes())
if err != nil {
return nil, fmt.Errorf("grouping imports for %s: %s", path, err)
}
output.Write(file.Slice(lastPos, imp.Pos))
output.Write(newBytes)
lastPos = imp.End
}
if fn, ok := d.(*parser.FuncDecl); ok {
var curFunc bytes.Buffer
render.Func(&curFunc, file, fn, *tab, *wrap, *wrapdoc, lastPos)
output.Write(curFunc.Bytes())
lastPos = fn.BodyEnd()
}
if cnst, ok := d.(*parser.ConstDecl); ok {
var declBuf bytes.Buffer
render.GenDecl(&declBuf, file, cnst.GenDecl, *wrapdoc, lastPos)
output.Write(declBuf.Bytes())
lastPos = cnst.End()
}
if vr, ok := d.(*parser.VarDecl); ok {
var declBuf bytes.Buffer
render.GenDecl(&declBuf, file, vr.GenDecl, *wrapdoc, lastPos)
output.Write(declBuf.Bytes())
lastPos = vr.End()
}
if typ, ok := d.(*parser.TypeDecl); ok {
var declBuf bytes.Buffer
render.GenDecl(&declBuf, file, typ.GenDecl, *wrapdoc, lastPos)
output.Write(declBuf.Bytes())
lastPos = typ.End()
}
}
output.Write(src[file.Offset(lastPos):])
return output.Bytes(), nil
}
// remapImports maps each existing import declaration in the file to an import
// block that should replace it. An import block can contain multiple import
// declarations, to indicate that the existing single import declaration should
// be replaced with multiple separate import declarations, or nil, to indicate
// that the import declaration should be removed entirely.
//
// The goal is to have just one import declaration, within which imports are
// grouped standard library imports and non-standard library imports. An
// exception is made for cgo, whose "C" psuedo-imports are extracted into
// separate import declarations.
func remapImports(file *parser.File) map[*parser.ImportDecl][]render.ImportBlock {
imports := file.ImportSpecs()
stdlibImports := make([]parser.ImportSpec, 0, len(imports))
otherImports := make([]parser.ImportSpec, 0, len(imports))
localImports := make([]parser.ImportSpec, 0, len(imports))
localPrefixes := []string{}
if localPrefix != nil && *localPrefix != "" {
lps := strings.Split(*localPrefix, ",")
localPrefixes = make([]string, 0, len(lps))
for _, lp := range lps {
if !strings.HasSuffix(lp, "/") {
lp += "/"
}
localPrefixes = append(localPrefixes, lp)
}
}
NEXT_IMPORT:
for _, imp := range imports {
impPath := imp.Path()
if impPath == "C" {
continue NEXT_IMPORT
}
for _, lp := range localPrefixes {
if strings.HasPrefix(impPath, lp) {
localImports = append(localImports, imp)
continue NEXT_IMPORT
}
}
if strings.Contains(impPath, ".") {
otherImports = append(otherImports, imp)
continue NEXT_IMPORT
}
stdlibImports = append(stdlibImports, imp)
}
mainBlock := render.ImportBlock{stdlibImports, otherImports, localImports}
needMainBlock := mainBlock.Size() > 0
mapping := map[*parser.ImportDecl][]render.ImportBlock{}
impDecls := file.ImportDecls()
for _, imp := range impDecls {
var blocks []render.ImportBlock
var cImports []parser.ImportSpec
for _, spec := range imp.Specs {
if spec.Path() == "C" {
cImports = append(cImports, spec)
}
}
if needMainBlock && len(cImports) != len(imp.Specs) {
// The first import declaration we see that contains something other
// than "C" psuedo-imports will be our main import block.
blocks = append(blocks, mainBlock)
needMainBlock = false
}
// If there were any "C" psuedo-imports in this declaration, split them
// out into their own import declarations.
for _, imp := range cImports {
if imp.Doc == nil {
// A cgo import without a doc comment has no effect. Remove it.
continue
}
blocks = append(blocks, render.ImportBlock{{imp}})
}
mapping[imp] = blocks
}
return mapping
}
// isDirectory returns true if the path is a directory. False is
// returned on any error.
func isDirectory(path string) bool {
fileInfo, err := os.Stat(path)
if err != nil {
return false
}
return fileInfo.IsDir()
}