forked from sourcegraph/go-template-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint.go
289 lines (258 loc) · 6.87 KB
/
lint.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
package main
import (
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"log"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"text/template"
"text/template/parse"
"sourcegraph.com/sourcegraph/go-template-lint/tmplwalk"
)
var (
verbose = flag.Bool("v", false, "show verbose output")
funcmapFile = flag.String("f", "", "Go source file with FuncMap literal (commas separate multiple files)")
tmplSetsFile = flag.String("t", "", "Go source file containing template set (a [][]string literal)")
tmplDir = flag.String("td", "", "base path of templates (prepended to template set filenames)")
fset = token.NewFileSet()
)
func main() {
log.SetFlags(0)
flag.Parse()
if *funcmapFile == "" {
log.Fatal("-f is required (run with -h for usage info)")
}
if *tmplSetsFile == "" {
log.Fatal("-t is required (run with -h for usage info)")
}
var allDefinedFuncs []string
for _, f := range strings.Split(*funcmapFile, ",") {
definedFuncs, err := parseFuncDefs(f)
if err != nil {
log.Fatalf("Error parsing FuncMap names from %s: %s", f, err)
}
if len(definedFuncs) == 0 {
log.Fatalf("No func definitions in a FuncMap found in %s.", f)
}
allDefinedFuncs = append(allDefinedFuncs, definedFuncs...)
}
if *verbose {
log.Printf("# Found %d template functions:", len(allDefinedFuncs))
for _, name := range allDefinedFuncs {
log.Printf("# - %s", name)
}
}
tmplSets, err := parseTmplSet(*tmplSetsFile)
if err != nil {
log.Fatalf("Error parsing template sets (a [][]string literal) from %s: %s", *tmplSetsFile, err)
}
if len(tmplSets) == 0 {
log.Fatal("No template files found.")
}
if *verbose {
log.Printf("# Found %d template file sets:", len(tmplSets))
for _, ts := range tmplSets {
log.Printf("# - %s", strings.Join(ts, " "))
}
}
invokedFuncs, err := findInvokedFuncs(tmplSets, allDefinedFuncs)
if err != nil {
log.Fatalf("Error parsing templates to find invoked template functions: %s", err)
}
if len(invokedFuncs) == 0 {
log.Fatal("No func invocations in templates found.")
}
if *verbose {
log.Printf("# Found %d functions invoked in templates:", len(invokedFuncs))
for _, name := range invokedFuncs {
log.Printf("# - %s", name)
}
}
definedFuncMap := sliceToMap(allDefinedFuncs)
invokedFuncMap := sliceToMap(invokedFuncs)
fail := false
unusedFuncs := subtract(definedFuncMap, invokedFuncMap)
for _, f := range unusedFuncs {
fmt.Println("unused template func", f)
fail = true
}
addPredefs(definedFuncMap)
undefinedFuncs := subtract(invokedFuncMap, definedFuncMap)
for _, f := range undefinedFuncs {
fmt.Println("undefined template func", f)
fail = true
}
if fail {
os.Exit(1)
}
}
// parseFuncDefs extracts and returns function names from a Go source
// file containing a FuncMap literal.
func parseFuncDefs(filename string) ([]string, error) {
f, err := parser.ParseFile(fset, filename, nil, parser.AllErrors)
if err != nil {
return nil, err
}
var funcNames []string
ast.Walk(visitFn(func(n ast.Node) bool {
switch n := n.(type) {
case *ast.CompositeLit:
if isFuncMap(n.Type) {
for _, e := range n.Elts {
kv := e.(*ast.KeyValueExpr)
name, err := strconv.Unquote(kv.Key.(*ast.BasicLit).Value)
if err != nil {
log.Fatal(err)
}
funcNames = append(funcNames, name)
}
}
}
return true
}), f)
return funcNames, nil
}
func isFuncMap(x ast.Expr) bool {
switch x := x.(type) {
case *ast.Ident:
return x.Name == "FuncMap"
case *ast.SelectorExpr:
return isFuncMap(x.Sel)
}
return false
}
func parseTmplSet(filename string) ([][]string, error) {
f, err := parser.ParseFile(fset, filename, nil, parser.AllErrors)
if err != nil {
return nil, err
}
var tmplSets [][]string
ast.Walk(visitFn(func(n ast.Node) bool {
switch n := n.(type) {
case *ast.CompositeLit:
switch {
case isTmplSet(n.Type):
for _, e := range n.Elts {
tmplSets = append(tmplSets, astStringSlice(e.(*ast.CompositeLit)))
}
case isLayoutSet(n.Type):
tmplSets = append(tmplSets, astStringSlice(n))
}
}
return true
}), f)
return tmplSets, nil
}
func isTmplSet(x ast.Expr) bool {
if sx, ok := x.(*ast.ArrayType); ok && sx.Len == nil {
if sx2, ok := sx.Elt.(*ast.ArrayType); ok && sx2.Len == nil {
if t, ok := sx2.Elt.(*ast.Ident); ok && t.Name == "string" {
return true
}
}
}
return false
}
func isLayoutSet(x ast.Expr) bool {
if sx, ok := x.(*ast.ArrayType); ok && sx.Len == nil {
if t, ok := sx.Elt.(*ast.Ident); ok && t.Name == "string" {
return true
}
}
return false
}
func astStringSlice(cl *ast.CompositeLit) []string {
var ss []string
for _, e := range cl.Elts {
s, err := strconv.Unquote(e.(*ast.BasicLit).Value)
if err != nil {
log.Fatal(err)
}
ss = append(ss, s)
}
return ss
}
// visitFn is a wrapper for traversing nodes in the AST
type visitFn func(node ast.Node) (descend bool)
func (v visitFn) Visit(node ast.Node) ast.Visitor {
descend := v(node)
if descend {
return v
}
return nil
}
// findInvokedFuncs returns a list of all functions (including
// predefined functions) invoked in Go templates in templateDir
// (recursively).
func findInvokedFuncs(tmplSets [][]string, definedFuncs []string) ([]string, error) {
definedFuncMap := template.FuncMap{}
for _, f := range definedFuncs {
definedFuncMap[f] = func() interface{} { return nil }
}
invoked := map[string]struct{}{}
for _, tmplSet := range tmplSets {
tt := template.New("")
tt.Funcs(definedFuncMap)
_, err := tt.ParseFiles(joinTemplateDir(*tmplDir, tmplSet)...)
if err != nil {
return nil, fmt.Errorf("template set %v: %s", tmplSet, err)
}
for _, t := range tt.Templates() {
if t.Tree == nil {
log.Printf("No template root for %v", t.Name())
continue
}
tmplwalk.Inspect(t.Tree.Root, func(n parse.Node) bool {
switch n := n.(type) {
case *parse.IdentifierNode:
invoked[n.Ident] = struct{}{}
}
return true
})
}
}
var invokedList []string
for f := range invoked {
invokedList = append(invokedList, f)
}
sort.Strings(invokedList)
return invokedList, nil
}
func joinTemplateDir(base string, files []string) []string {
result := make([]string, len(files))
for i := range files {
result[i] = filepath.Join(base, files[i])
}
return result
}
var predefFuncs = []string{"and", "call", "html", "index", "js", "len", "not", "or", "print", "printf", "println", "urlquery", "eq", "ne", "lt", "le", "gt", "ge"}
// addPredefs adds predefined template functions to fm.
func addPredefs(fm map[string]struct{}) {
for _, f := range predefFuncs {
fm[f] = struct{}{}
}
}
// subtract returns a list of keys in a that are not in b.
func subtract(a, b map[string]struct{}) []string {
var d []string
for k := range a {
if _, inB := b[k]; !inB {
d = append(d, k)
}
}
sort.Strings(d)
return d
}
func sliceToMap(ss []string) map[string]struct{} {
m := make(map[string]struct{}, len(ss))
for _, s := range ss {
m[s] = struct{}{}
}
return m
}