@@ -12,8 +12,9 @@ import (
12
12
"sort"
13
13
"strings"
14
14
15
- "github.com/go-critic/go-critic/framework/linter"
16
15
"github.com/quasilyte/go-ruleguard/ruleguard"
16
+
17
+ "github.com/go-critic/go-critic/framework/linter"
17
18
)
18
19
19
20
func init () {
@@ -41,6 +42,14 @@ If flag is set, the value must be a comma-separated list of error conditions.
41
42
* 'import': rule refers to a package that cannot be loaded.
42
43
* 'dsl': gorule file does not comply with the ruleguard DSL.` ,
43
44
},
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
+ },
44
53
}
45
54
info .Summary = "Runs user-defined rules using ruleguard linter"
46
55
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) (
124
133
fset := token .NewFileSet ()
125
134
filePatterns := strings .Split (rulesFlag , "," )
126
135
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
+ }
127
150
ruleguardDebug := os .Getenv ("GOCRITIC_RULEGUARD_DEBUG" ) != ""
128
151
129
152
loadContext := & ruleguard.LoadContext {
130
153
Fset : fset ,
131
154
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 == ""
134
173
},
135
174
}
136
175
@@ -236,3 +275,7 @@ func runRuleguardEngine(ctx *linter.CheckerContext, f *ast.File, e *ruleguard.En
236
275
}
237
276
}
238
277
}
278
+
279
+ func debugPrint (s string ) {
280
+ fmt .Println ("debug:" , s )
281
+ }
0 commit comments