-
Notifications
You must be signed in to change notification settings - Fork 245
/
exprstatementcheck.go
122 lines (105 loc) · 3.21 KB
/
exprstatementcheck.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
package exprstatementcheck
import (
"flag"
"fmt"
"go/ast"
"slices"
"strings"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
func sliceMap(s []string, f func(value string) string) []string {
mapped := make([]string, 0, len(s))
for _, value := range s {
mapped = append(mapped, f(value))
}
return mapped
}
type disallowedExprStatementConfig struct {
fullTypePath string
errorMessage string
ignoredMethodNames []string
}
func Analyzer() *analysis.Analyzer {
flagSet := flag.NewFlagSet("exprstatementcheck", flag.ExitOnError)
disallowedExprTypes := flagSet.String(
"disallowed-expr-statement-types",
"",
`semicolon delimited configuration of the types that should be disallowed as expression statements.
Formats:
- "full type path:error message"
- "full type path:MethodNameUnderWhichToIgnore:error message"
Example: "*github.com/rs/zerolog.Event:MarshalZerologObject:error message here"
`,
)
skip := flagSet.String("skip-pkg", "", "package(s) to skip for linting")
return &analysis.Analyzer{
Name: "exprstatementcheck",
Doc: "reports expression statements that have the disallowed value types",
Run: func(pass *analysis.Pass) (any, error) {
// Check for a skipped package.
if len(*skip) > 0 {
skipped := sliceMap(strings.Split(*skip, ","), strings.TrimSpace)
for _, s := range skipped {
if strings.Contains(pass.Pkg.Path(), s) {
return nil, nil
}
}
}
entries := strings.Split(*disallowedExprTypes, ";")
typePathsAndMessages := map[string]disallowedExprStatementConfig{}
for _, entry := range entries {
if len(entry) == 0 {
continue
}
parts := strings.Split(entry, ":")
if len(parts) == 2 {
typePathsAndMessages[parts[0]] = disallowedExprStatementConfig{
fullTypePath: parts[0],
errorMessage: parts[1],
}
} else if len(parts) == 3 {
typePathsAndMessages[parts[0]] = disallowedExprStatementConfig{
fullTypePath: parts[0],
ignoredMethodNames: strings.Split(parts[1], ","),
errorMessage: parts[2],
}
} else {
return nil, fmt.Errorf("invalid value for disallowed-expr-statement-types: `%s`", entry)
}
}
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{
(*ast.ExprStmt)(nil),
}
inspect.WithStack(nodeFilter, func(n ast.Node, push bool, stack []ast.Node) bool {
switchStatement:
switch s := n.(type) {
case *ast.ExprStmt:
foundType := pass.TypesInfo.TypeOf(s.X)
if foundType == nil {
return false
}
if config, ok := typePathsAndMessages[foundType.String()]; ok {
if len(config.ignoredMethodNames) > 0 {
for _, parent := range stack {
if funcDecl, ok := parent.(*ast.FuncDecl); ok {
if slices.Contains(config.ignoredMethodNames, funcDecl.Name.Name) {
break switchStatement
}
}
}
}
pass.Reportf(s.Pos(), "In package %s: %s", pass.Pkg.Path(), config.errorMessage)
}
return false
}
return true
})
return nil, nil
},
Requires: []*analysis.Analyzer{inspect.Analyzer},
Flags: *flagSet,
}
}