/
parse.go
174 lines (149 loc) · 3.54 KB
/
parse.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
package parse
import (
"go/ast"
"go/parser"
"go/token"
"sort"
"strings"
)
const C = "\"C\""
type GciImports struct {
// original index of import group, include doc, name, path and comment
Start, End int
Name, Path string
}
type ImportList []*GciImports
func (l ImportList) Len() int {
return len(l)
}
func (l ImportList) Less(i, j int) bool {
if strings.Compare(l[i].Path, l[j].Path) == 0 {
return strings.Compare(l[i].Name, l[j].Name) < 0
}
return strings.Compare(l[i].Path, l[j].Path) < 0
}
func (l ImportList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
/*
* AST considers a import block as below:
* ```
* Doc
* Name Path Comment
* ```
* An example is like below:
* ```
* // test
* test "fmt" // test
* ```
* getImports return a import block with name, start and end index
*/
func getImports(imp *ast.ImportSpec) (start, end int, name string) {
if imp.Doc != nil {
// doc poc need minus one to get the first index of comment
start = int(imp.Doc.Pos()) - 1
} else {
if imp.Name != nil {
// name pos need minus one too
start = int(imp.Name.Pos()) - 1
} else {
// path pos start without quote, need minus one for it
start = int(imp.Path.Pos()) - 1
}
}
if imp.Name != nil {
name = imp.Name.Name
}
if imp.Comment != nil {
end = int(imp.Comment.End())
} else {
end = int(imp.Path.End())
}
return
}
func ParseFile(src []byte, filename string) (ImportList, int, int, error) {
fileSet := token.NewFileSet()
f, err := parser.ParseFile(fileSet, filename, src, parser.ParseComments)
if err != nil {
return nil, 0, 0, err
}
if len(f.Imports) == 0 {
return nil, 0, 0, NoImportError{}
}
var (
// headEnd means the start of import block
headEnd int
// tailStart means the end + 1 of import block
tailStart int
// lastImportStart means the start of last import block
lastImportStart int
data ImportList
)
for i, d := range f.Decls {
switch d.(type) {
case *ast.GenDecl:
dd := d.(*ast.GenDecl)
if dd.Tok == token.IMPORT {
// there are two cases, both end with linebreak:
// 1.
// import (
// "xxxx"
// )
// 2.
// import "xxx"
if headEnd == 0 {
headEnd = int(d.Pos()) - 1
}
tailStart = int(d.End())
lastImportStart = i
}
}
}
if len(f.Decls) > lastImportStart+1 {
tailStart = int(f.Decls[lastImportStart+1].Pos() - 1)
}
for _, imp := range f.Imports {
if imp.Path.Value == C {
if imp.Comment != nil {
headEnd = int(imp.Comment.End())
} else {
headEnd = int(imp.Path.End())
}
continue
}
start, end, name := getImports(imp)
data = append(data, &GciImports{
Start: start,
End: end,
Name: name,
Path: strings.Trim(imp.Path.Value, `"`),
})
}
sort.Sort(data)
return data, headEnd, tailStart, nil
}
// IsGeneratedFileByComment reports whether the source file is generated code.
// Using a bit laxer rules than https://golang.org/s/generatedcode to
// match more generated code.
// Taken from https://github.com/golangci/golangci-lint.
func IsGeneratedFileByComment(in string) bool {
const (
genCodeGenerated = "code generated"
genDoNotEdit = "do not edit"
genAutoFile = "autogenerated file" // easyjson
)
markers := []string{genCodeGenerated, genDoNotEdit, genAutoFile}
in = strings.ToLower(in)
for _, marker := range markers {
if strings.Contains(in, marker) {
return true
}
}
return false
}
type NoImportError struct{}
func (n NoImportError) Error() string {
return "No imports"
}
func (i NoImportError) Is(err error) bool {
_, ok := err.(NoImportError)
return ok
}