Skip to content

Commit 5160048

Browse files
committed
Move rule definitions into own file
1 parent 50bbc53 commit 5160048

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

rule.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
13+
package gas
14+
15+
import (
16+
"go/ast"
17+
"reflect"
18+
)
19+
20+
// The Rule interface used by all rules supported by GAS.
21+
type Rule interface {
22+
Match(ast.Node, *Context) (*Issue, error)
23+
}
24+
25+
// RuleBuilder is used to register a rule definition with the analyzer
26+
type RuleBuilder func(c Config) (Rule, []ast.Node)
27+
28+
// A RuleSet maps lists of rules to the type of AST node they should be run on.
29+
// The anaylzer will only invoke rules contained in the list associated with the
30+
// type of AST node it is currently visiting.
31+
type RuleSet map[reflect.Type][]Rule
32+
33+
func NewRuleSet() RuleSet {
34+
return make(RuleSet)
35+
}
36+
37+
// Register adds a trigger for the supplied rule for the the
38+
// specified ast nodes.
39+
func (r RuleSet) Register(rule Rule, nodes ...ast.Node) {
40+
for _, n := range nodes {
41+
t := reflect.TypeOf(n)
42+
if rules, ok := r[t]; ok {
43+
r[t] = append(rules, rule)
44+
} else {
45+
r[t] = []Rule{rule}
46+
}
47+
}
48+
}
49+
50+
// RegisteredFor will return all rules that are registered for a
51+
// specified ast node.
52+
func (r RuleSet) RegisteredFor(n ast.Node) []Rule {
53+
if rules, found := r[reflect.TypeOf(n)]; found {
54+
return rules
55+
}
56+
return []Rule{}
57+
}

0 commit comments

Comments
 (0)