@@ -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
1920func 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