Skip to content

Commit 423f103

Browse files
authored
checkers: add enable/disable flags to ruleguard checker (#1181)
1 parent b7720e1 commit 423f103

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

checkers/boolExprSimplify_checker.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ import (
66
"go/token"
77
"strconv"
88

9-
"github.com/go-critic/go-critic/checkers/internal/astwalk"
10-
"github.com/go-critic/go-critic/checkers/internal/lintutil"
11-
"github.com/go-critic/go-critic/framework/linter"
129
"github.com/go-toolsmith/astcast"
1310
"github.com/go-toolsmith/astcopy"
1411
"github.com/go-toolsmith/astequal"
1512
"github.com/go-toolsmith/astp"
1613
"github.com/go-toolsmith/typep"
1714
"golang.org/x/tools/go/ast/astutil"
15+
16+
"github.com/go-critic/go-critic/checkers/internal/astwalk"
17+
"github.com/go-critic/go-critic/checkers/internal/lintutil"
18+
"github.com/go-critic/go-critic/framework/linter"
1819
)
1920

2021
func init() {

checkers/ruleguard_checker.go

+46-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import (
1212
"sort"
1313
"strings"
1414

15-
"github.com/go-critic/go-critic/framework/linter"
1615
"github.com/quasilyte/go-ruleguard/ruleguard"
16+
17+
"github.com/go-critic/go-critic/framework/linter"
1718
)
1819

1920
func init() {
@@ -41,6 +42,14 @@ If flag is set, the value must be a comma-separated list of error conditions.
4142
* 'import': rule refers to a package that cannot be loaded.
4243
* 'dsl': gorule file does not comply with the ruleguard DSL.`,
4344
},
45+
"enable": {
46+
Value: "<all>",
47+
Usage: "comma-separated list of enabled groups or skip empty to enable everything",
48+
},
49+
"disable": {
50+
Value: "",
51+
Usage: "comma-separated list of disabled groups or skip empty to enable everything",
52+
},
4453
}
4554
info.Summary = "Runs user-defined rules using ruleguard linter"
4655
info.Details = "Reads a rules file and turns them into go-critic checkers."
@@ -124,13 +133,43 @@ func newRuleguardChecker(info *linter.CheckerInfo, ctx *linter.CheckerContext) (
124133
fset := token.NewFileSet()
125134
filePatterns := strings.Split(rulesFlag, ",")
126135

136+
enabledGroups := make(map[string]bool)
137+
disabledGroups := make(map[string]bool)
138+
139+
for _, g := range strings.Split(info.Params.String("disable"), ",") {
140+
g = strings.TrimSpace(g)
141+
disabledGroups[g] = true
142+
}
143+
flagEnable := info.Params.String("enable")
144+
if flagEnable != "<all>" {
145+
for _, g := range strings.Split(flagEnable, ",") {
146+
g = strings.TrimSpace(g)
147+
enabledGroups[g] = true
148+
}
149+
}
127150
ruleguardDebug := os.Getenv("GOCRITIC_RULEGUARD_DEBUG") != ""
128151

129152
loadContext := &ruleguard.LoadContext{
130153
Fset: fset,
131154
DebugImports: ruleguardDebug,
132-
DebugPrint: func(s string) {
133-
fmt.Println("debug:", s)
155+
DebugPrint: debugPrint,
156+
GroupFilter: func(g string) bool {
157+
whyDisabled := ""
158+
enabled := flagEnable == "<all>" || enabledGroups[g]
159+
switch {
160+
case !enabled:
161+
whyDisabled = "not enabled by -enabled flag"
162+
case disabledGroups[g]:
163+
whyDisabled = "disabled by -disable flag"
164+
}
165+
if ruleguardDebug {
166+
if whyDisabled != "" {
167+
debugPrint(fmt.Sprintf("(-) %s is %s", g, whyDisabled))
168+
} else {
169+
debugPrint(fmt.Sprintf("(+) %s is enabled", g))
170+
}
171+
}
172+
return whyDisabled == ""
134173
},
135174
}
136175

@@ -236,3 +275,7 @@ func runRuleguardEngine(ctx *linter.CheckerContext, f *ast.File, e *ruleguard.En
236275
}
237276
}
238277
}
278+
279+
func debugPrint(s string) {
280+
fmt.Println("debug:", s)
281+
}

0 commit comments

Comments
 (0)