diff --git a/pkg/rule/rule.go b/pkg/rule/rule.go index 1f1fe4e4..20a4c103 100644 --- a/pkg/rule/rule.go +++ b/pkg/rule/rule.go @@ -22,14 +22,18 @@ type Rule struct { } func (r *Rule) FindAllStringIndex(text string) [][]int { + // If no terms are provided, this essentially disables the rule + // which is helpful for disabling default rules. Eventually, there should be + // a way to disable a default rule, and then, if a rule has no Terms, it falls back to the Name. + if len(r.Terms) == 0 { + return [][]int{} + } + if r.re == nil { r.re = regexp.MustCompile(fmt.Sprintf(`(?i)\b(%s)\b`, strings.Join(r.Terms, "|"))) } - return r.re.FindAllStringIndex(text, -1) -} -func (r *Rule) String() string { - return r.Name + return r.re.FindAllStringIndex(text, -1) } // Reason returns a human-readable reason for the rule violation diff --git a/pkg/rule/rule_test.go b/pkg/rule/rule_test.go index a95cefd3..b82f4ab3 100644 --- a/pkg/rule/rule_test.go +++ b/pkg/rule/rule_test.go @@ -22,6 +22,9 @@ func TestRule_FindAllStringIndex(t *testing.T) { got := r.FindAllStringIndex(test.text) assert.Equal(t, test.expected, got) } + + e := Rule{Name: "rule1"} + assert.Equal(t, [][]int{}, e.FindAllStringIndex("rule1")) } func TestRule_Reason(t *testing.T) {