/
gorule.go
146 lines (119 loc) · 3.12 KB
/
gorule.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
package ruleguard
import (
"fmt"
"go/ast"
"go/types"
"regexp"
"github.com/quasilyte/go-ruleguard/ruleguard/quasigo"
"github.com/quasilyte/gogrep"
"github.com/quasilyte/gogrep/nodetag"
)
type goRuleSet struct {
universal *scopedGoRuleSet
groups map[string]*GoRuleGroup // To handle redefinitions
}
type scopedGoRuleSet struct {
categorizedNum int
rulesByTag [nodetag.NumBuckets][]goRule
commentRules []goCommentRule
}
type goCommentRule struct {
base goRule
pat *regexp.Regexp
captureGroups bool
}
type goRule struct {
group *GoRuleGroup
line int
pat *gogrep.Pattern
msg string
location string
suggestion string
filter matchFilter
}
type matchFilterResult string
func (s matchFilterResult) Matched() bool { return s == "" }
func (s matchFilterResult) RejectReason() string { return string(s) }
type filterFunc func(*filterParams) matchFilterResult
type matchFilter struct {
src string
fn func(*filterParams) matchFilterResult
}
type filterParams struct {
ctx *RunContext
filename string
imports map[string]struct{}
env *quasigo.EvalEnv
importer *goImporter
gogrepSubState *gogrep.MatcherState
match matchData
nodePath *nodePath
nodeText func(n ast.Node) []byte
deadcode bool
currentFunc *ast.FuncDecl
// varname is set only for custom filters before bytecode function is called.
varname string
}
func (params *filterParams) subNode(name string) ast.Node {
n, _ := params.match.CapturedByName(name)
return n
}
func (params *filterParams) subExpr(name string) ast.Expr {
n, _ := params.match.CapturedByName(name)
switch n := n.(type) {
case ast.Expr:
return n
case *ast.ExprStmt:
return n.X
default:
return nil
}
}
func (params *filterParams) typeofNode(n ast.Node) types.Type {
var e ast.Expr
switch n := n.(type) {
case ast.Expr:
e = n
case *ast.Field:
e = n.Type
}
if typ := params.ctx.Types.TypeOf(e); typ != nil {
return typ
}
return types.Typ[types.Invalid]
}
func mergeRuleSets(toMerge []*goRuleSet) (*goRuleSet, error) {
out := &goRuleSet{
universal: &scopedGoRuleSet{},
groups: make(map[string]*GoRuleGroup),
}
for _, x := range toMerge {
out.universal = appendScopedRuleSet(out.universal, x.universal)
for groupName, group := range x.groups {
if prevGroup, ok := out.groups[groupName]; ok {
newRef := fmt.Sprintf("%s:%d", group.Filename, group.Line)
oldRef := fmt.Sprintf("%s:%d", prevGroup.Filename, prevGroup.Line)
return nil, fmt.Errorf("%s: redefinition of %s(), previously defined at %s", newRef, groupName, oldRef)
}
out.groups[groupName] = group
}
}
return out, nil
}
func appendScopedRuleSet(dst, src *scopedGoRuleSet) *scopedGoRuleSet {
for tag, rules := range src.rulesByTag {
dst.rulesByTag[tag] = append(dst.rulesByTag[tag], cloneRuleSlice(rules)...)
dst.categorizedNum += len(rules)
}
dst.commentRules = append(dst.commentRules, src.commentRules...)
return dst
}
func cloneRuleSlice(slice []goRule) []goRule {
out := make([]goRule, len(slice))
for i, rule := range slice {
clone := rule
clone.pat = rule.pat.Clone()
out[i] = clone
}
return out
}