Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignore flag #1181

Merged
merged 3 commits into from Dec 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions checkers/boolExprSimplify_checker.go
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
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)
}