-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdocumentfunctions.go
61 lines (53 loc) · 1.62 KB
/
documentfunctions.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
// Package documentfunctions implements a linter that forces you to document functions.
//
// When you are dealing with code that requires multiple people to collaborate, your
// code is not going to be "immediately obvious based on the implementation". You need
// to document all functions that you make. This can help find untested invariants that
// can lead to customers having a bad time.
//
// To disable this behavior, document your code.
package documentfunctions
import (
"go/ast"
"strings"
"golang.org/x/tools/go/analysis"
)
var Analyzer = &analysis.Analyzer{
Name: "documentfunctions",
Doc: "Prevent the use of time.Sleep in test functions",
Run: run,
}
var ignoreFunctions = []string{
"main", // main functions are probably okay
}
// run runs the analysis pass for documentfunctions. It performs a depth-first
// traversal of all AST nodes in a file, looks for function definitions, and
// then ensures all of them have comments.
func run(pass *analysis.Pass) (interface{}, error) {
for _, file := range pass.Files {
pos := pass.Fset.Position(file.Pos())
if !strings.HasSuffix(".go", pos.Filename) {
continue
}
ast.Inspect(file, func(n ast.Node) bool {
fe, ok := n.(*ast.FuncDecl)
if !ok {
return true
}
for _, ignoreName := range ignoreFunctions {
if fe.Name.Name == ignoreName {
return true
}
}
if fe.Doc == nil {
if fe.Name.Name == "init" {
pass.Reportf(fe.Pos(), "init functions must have side effects documented")
return true
}
pass.Reportf(fe.Pos(), "function %s must have documentation", fe.Name.Name)
}
return true
})
}
return nil, nil
}