Skip to content

Commit

Permalink
checkers: add enable/disable flags to ruleguard checker (#1181)
Browse files Browse the repository at this point in the history
  • Loading branch information
peakle authored Dec 31, 2021
1 parent b7720e1 commit 423f103
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
7 changes: 4 additions & 3 deletions checkers/boolExprSimplify_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (
"go/token"
"strconv"

"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-critic/go-critic/checkers/internal/lintutil"
"github.com/go-critic/go-critic/framework/linter"
"github.com/go-toolsmith/astcast"
"github.com/go-toolsmith/astcopy"
"github.com/go-toolsmith/astequal"
"github.com/go-toolsmith/astp"
"github.com/go-toolsmith/typep"
"golang.org/x/tools/go/ast/astutil"

"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-critic/go-critic/checkers/internal/lintutil"
"github.com/go-critic/go-critic/framework/linter"
)

func init() {
Expand Down
49 changes: 46 additions & 3 deletions checkers/ruleguard_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
"sort"
"strings"

"github.com/go-critic/go-critic/framework/linter"
"github.com/quasilyte/go-ruleguard/ruleguard"

"github.com/go-critic/go-critic/framework/linter"
)

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

enabledGroups := make(map[string]bool)
disabledGroups := make(map[string]bool)

for _, g := range strings.Split(info.Params.String("disable"), ",") {
g = strings.TrimSpace(g)
disabledGroups[g] = true
}
flagEnable := info.Params.String("enable")
if flagEnable != "<all>" {
for _, g := range strings.Split(flagEnable, ",") {
g = strings.TrimSpace(g)
enabledGroups[g] = true
}
}
ruleguardDebug := os.Getenv("GOCRITIC_RULEGUARD_DEBUG") != ""

loadContext := &ruleguard.LoadContext{
Fset: fset,
DebugImports: ruleguardDebug,
DebugPrint: func(s string) {
fmt.Println("debug:", s)
DebugPrint: debugPrint,
GroupFilter: func(g string) bool {
whyDisabled := ""
enabled := flagEnable == "<all>" || enabledGroups[g]
switch {
case !enabled:
whyDisabled = "not enabled by -enabled flag"
case disabledGroups[g]:
whyDisabled = "disabled by -disable flag"
}
if ruleguardDebug {
if whyDisabled != "" {
debugPrint(fmt.Sprintf("(-) %s is %s", g, whyDisabled))
} else {
debugPrint(fmt.Sprintf("(+) %s is enabled", g))
}
}
return whyDisabled == ""
},
}

Expand Down Expand Up @@ -236,3 +275,7 @@ func runRuleguardEngine(ctx *linter.CheckerContext, f *ast.File, e *ruleguard.En
}
}
}

func debugPrint(s string) {
fmt.Println("debug:", s)
}

0 comments on commit 423f103

Please sign in to comment.